1

I would like to have custom fields with translatable content in taxonomy terms. Is this possible with existing modules?

I know that this is possible if I use nodes instead of taxonomy terms: I can make fields translatable using the entity translation module and edit the different versions of the field by switching the language on the node edit page.

I am also aware that the taxonomy translation module allows to "localize" a vocabulary in the sense that its terms can have a different name and description for each language. I want not only name and description to be localizable but also custom fields that I have attached to the vocabulary, in particular text and image fields. Is this possible with existing modules?

3 Answers 3

0

Entity translation allows for translation of custom term fields either. You only need to enable taxonomy term as translatable entity type on the Entity translation settings page (admin/config/regional/entity_translation).

But note that once you have enabled Entity translation for taxonomy terms, the name and description of a term cannot by localized anymore unless you have installed and enabled the Title module. It provides an option to replace the core name and description fields with field instances, which will be translatable through Entity translation.

1
  • Yes, I think I was missing this "enable taxonomy as translatable entity type" part. Not sure because I do not have access to that Drupal instance anymore, but on a fresh install it works fine. Thanks! Feb 15, 2016 at 8:51
2

If you use 'Localize' option for taxonomy translation mode, it is possible to add taxonomy custom fields to string translation:

/**
 * Implements hook_i18n_string_list_TEXTGROUP_alter().
 */
function YOUR_MODULE_i18n_string_list_taxonomy_alter(&$strings, $type, $object) {
  if ($type == 'taxonomy_term' && $object) {
    if (isset($object->FIELD_NAME)) {
      $term_FIELD_NAME = field_get_items('taxonomy_term', $object, 'FIELD_NAME');
      $strings['taxonomy']['term'][$object->tid]['FIELD_NAME']['string'] = $term_FIELD_NAME[0]['value'];
    }
  }
}

After clearing cache the field will be available in translation form.

3
  • As said above, I've been using Drupal for so long but didn't know about this hook. You made my day :-)
    – pbonnefoi
    Jul 6, 2016 at 9:30
  • Can I use this translate in panels? ( I override path taxonomy/term/% and I output those fields in different places of panel)
    – Alex
    Aug 18, 2017 at 5:53
  • Can I use this translate in views?
    – Alex
    Aug 18, 2017 at 6:26
2

In regards of andreyjan answer, this is a little more generic code to allow any textfield fields to be translated by using the "localize" mode:

function mymodule_i18n_string_list_taxonomy_alter(&$strings, $type, $object) {

  if ($type == 'taxonomy_term' && $object) {
    // Retrieve vocabulary and potential field instances 
    // for this vocabulary.
    $vid = $object->vid;
    $vocabulary = taxonomy_vocabulary_load($vid);
    $field_instances = field_info_instances($type, $vocabulary->machine_name);

    // Iterate over each fields to check if textfield(s) exists.
    foreach ($field_instances as $field_name => $field_instance) {
      if ($field_instance['widget']['type'] == 'text_textfield') {

        // Retrieve data for the field.
        $term_data = field_get_items($type, $object, $field_name);

        // Add new entry into strings array to reflect
        // values from current processed field.
        $strings['taxonomy']['term'][$object->tid][$field_name] = array(
          'string' => $term_data[0]['value'],
          'title' => $field_instance['label'],
          'format' => NULL,
          'name' => array('taxonomy', 'term', $object->tid, $field_name),
        );
      }
    }
  }
}

You can easily add other tests on widget type to allow other kind of fields to be added in translation process.

1
  • Really nice solution ! I've been using Drupal for so long but didn't know about this hook. You made my day :-)
    – pbonnefoi
    Jul 6, 2016 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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