3

I want to use Google Translate for translating taxonomy terms. I have the "Talking to Google" part done, but I can't find much documentation on translating terms & their descriptions programmatically.

Please help!

3

I was able to do it using the following code:

<?php
    $enabled_languages = array_shift(language_list('enabled'));
    foreach ($enabled_languages as $language => $object) {
        if ($language != 'en') {
            //Translate description
            i18n_string_translation_update(
                array('taxonomy', 'term', 12, 'description'), // Path where to store source and translation.
                autotranslate_translate($term->description, 'en', substr($language, 0, 2)),
                $language
            );
            //Translate name
            i18n_string_translation_update(
                array('taxonomy', 'term', 12, 'name'), // Path where to store source and translation.
                autotranslate_translate($term->name, 'en', substr($language, 0, 2)),
                $language
            );
        }
    }

?>

| improve this answer | |
1
1. Get / translate strings.

For translating these strings, the module must call this function before displaying them:

<?php
/**
 * Translate or update user defined string.
 * 
 * @param $name
 *   Textgroup and location glued with ':'.
 * @param $string
 *   String in default language. Default language may or may not be English.
 * @param $options
 *  Optional parameters like 'langcode' if language code is different from the page request one.
 * 
 * @return $string
 *   Translated string, $string if not found
 */
function i18n_string_translate($name, $string, $options) {
   .....
}
?>

For strings that have an input format you must use the function i18nstrings_text() that takes care of applying the correct filters if there is a translation.

<?php
/**
 * Get filtered translation
 * 
 * This function is intended to return translations for strings that have an input format. 
 * If no format passed, the default one will be used
 * 
 * @param $name
 *   Full string id
 * @param $default
 *   Default string to return if not found, already filtered
 * @param $options
 *   Optional parameters, like 'format'
 */
function i18n_string_text($name, $default, $options) {
  ....
}
?>
2. Create translation sets (taxonomy example)

To translate a string, you should use two functions, depending on which i18n mode you use:

  • Localize mode: i18n_string_translation_update()

  • Translate mode: i18n_translation_set_create()

    i18n_tsid) ? i18n_translation_set_load($source_term->i18n_tsid) : i18n_translation_set_create('taxonomy_term', $vocabulary->machine_name);
      // 2. Add the full source term in this set.
    
    $translation_set->add_item($source_term, $source_term->language);
      // 3. Add the full translated term in this set.
    
    $translation_set->add_item($translated_term, $translated_term->language); $translation_set->save(TRUE); ?>
Strings for other contents and modules

When a module defines its own strings, it must use some additional functions to allow these string to be tracked and updated.

1. Define the textgroup by implementing hook_i18n_string_info(). This is how it looks (i18n_taxonomy module)
<?php
/**
 * Implements hook_i18n_string_info()
 */
function i18n_taxonomy_i18n_string_info() {
  $groups['taxonomy'] = array(
    'title' => t('Taxonomy'),
    'description' => t('Vocabulary titles and term names for localizable vocabularies.'),
    'format' => FALSE, // This group doesn't have strings with format
    'list' => FALSE, // This group cannot list all strings
    'refresh callback' => 'i18n_taxonomy_i18n_string_refresh',
  );
  return $groups;
}
?>
2. Use i18n_string_update() whenever the source string changes.
<?php
/**
 * Update / create translation source for user defined strings.
 * 
 * @param $name
 *   Textgroup and location glued with ':'.
 * @param $string
 *   Source string in default language. Default language may or may not be English.
 * @param $options
 *   Array with aditional options. The most important one is the 'format' parameter when the string has an input format.
 */
function i18n_string_update($name, $string, $options = array()) {
  ....
}
?>

The format parameter is to be used when the string is to be formatted with Input filters before displaying. If so, filter permissions will be properly handled by i18nstrings. Only users that can access the Input format will be able to translate that strings. These strings must be translated using i18nstrings_text() instead of i18nstrings().

3. Optional refresh callback function as defined in hook_locale()
<?php
/**
 * Update / create translation source for user defined strings. 
 * 
 * @return $bool
 *   TRUE if update successful, FALSE if not.
 */
  function mymodule_locale_refresh() {
    ... 
    // code to call i18n_string_update() for every string used by this module
    ...
    return TRUE; // Update completed with no issues
  }
?>

The refresh function should call i18n_string_update() for every string that is to be translated in your module. The refresh interface will remove all strings defined by your module and only re-list the strings added by the refresh callback function.

Again, all Drupal core textgroups and strings will be defined and handled by i18n_string when edited using the default pages for it. So you just need to implement these other functions when your module uses specific user defined strings.

The easiest way to have the strings translated without adding a new module dependency is to create a wrapper for your module which just translates the strings when i18n_string module is enabled.

<?php
function mymodule_translate($name, $string, $langcode = NULL) {
  return function_exists('i18n_string') ? i18n_string($name, $string, array('langcode' => $langcode)) : $string;  
}
?>

Or, if your module has its own textgroup, we can have a default parameter for that textgroup and same some code. Note we still keep the 'textgroup' parameter just in case we need to translate other module's strings, like for occasionally translating a taxonomy term or a content type name.

<?php
function mymodule_translate($name, $string, $langcode = NULL, $textgroup = 'mytextgroup') {
  return function_exists('i18n_string') ? i18n_string($textgroup . ':' . $name, $string, array('langcode' => $langcode)) : $string;  
}
?>

Read more at: Using the i18n API from other modules

Related:

How to associate programatically the translation of new term to the original existing term?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.