I've terms which share the same id in stored in field and I'd like to associate translation between them, so they're grouped on the same page at /taxonomy/term/%/translate
So I've existing original English term, then I'm creating manually term with different language, so I'm expecting this term to associate with the original one.
Not sure which hook would be the best, or it doesn't matter:
- hook_taxonomy_term_presave
- hook_taxonomy_term_insert
So in example I've:
/**
* Implements hook_taxonomy_term_presave().
*/
function foo_taxonomy_term_presave($term) {
$bundle = 'countries'; // Example bundle name
$vid = ($voc = taxonomy_vocabulary_machine_name_load($bundle)) ? $voc->vid : NULL;
if ($term->vid == $vid && module_exists('i18n_string') && $term->language <> 'en') {
if ($shared_id = $term->field_shared_id[key($term->field_shared_id)][0]['value']) {
// Find term which points to the same country, but in English
$query = new EntityFieldQuery();
$org_term_res = $query
->entityCondition('entity_type', 'taxonomy_term')
->entityCondition('bundle', $bundle)
->propertyCondition('vid', $vid)
->fieldCondition('field_shared_id', 'value', $shared_id, '=')
->propertyCondition('language', 'en')
->execute();
// Load term in the main language
if ($org_term = taxonomy_term_load(reset(reset($org_term_res))->tid)) {
// If original term is loaded, continue translation
$langcode = $term->language;
$key = i18n_object_info('taxonomy_term', 'key');
$result_translation = i18n_string_translation_update(
array('taxonomy', 'term', $term->{$key}, 'name'), // Array or string concatenated with ':' that contains textgroup and string context where to store source and translation.
$term->name, // Translation string for this language code.
$langcode, // The language code to translate to a language other than what is used to display the page.
$org_term->name // Optional source string, just in case it needs to be created.
);
watchdog('foo', "Added !source translation: !translated in !lang (key: !key)", array('!source' => $org_term->name, '!translated' => $term->name, '!lang' => $term->language, '!key' => $term->{$key}), WATCHDOG_NOTICE);
} // end: if
i18n_string_object_update('taxonomy_term', $term); // not sure if it's needed for something
} // end: if
} // end: if
}
So the main translate part is in i18n_string_translation_update()
.
But it simply doesn't work as expected. The $result_translation returns the right data, but the association is not visible in UI at taxonomy/term/%/translate
There is no much documentation about it apart of Using the i18n API from other modules
So I'm using the right function in the right way?
Related: