5

I have used the Views module to create a view of taxonomy terms.

The taxonomy terms are translated in the Greek language also (apart from English), using the typical i18n modules.

But when I switch the language to Greek (while visiting the site), still the English taxonomy terms are appearing (and not the terms in Greek).

How can I solve this?

4

Make sure to use the Internationalizations Views module, which extends the i18n module with multilingual support for Views.

To make Views results correspond to the language switcher, add a filter for Content Translation: User's Current Language, which is the language that an authenticated user has set in the user settings (it is NOT the same as the current browser language or the site language).

If no option for the User's Current Language is available in the filter criteria form, then verify your configuration at Configuration -> Languages -> detection and selection: The order should be URL, Default, User and not something like Default, URL, User (as mentioned also in https://www.drupal.org/node/1701854#comment-6279474).

If you're using Block visibility settings and indicated to only show that block on page <front>, then be aware that you may have to address yet another translation issue, as illustrated in my answer to the question How to make content translation available for front page?. It is about using the Variable module to correctly translate the Default frontpage value. Without that, Drupal will only show that block on the frontpage of your default value.

Notes:

  1. True, the Internationalizations Views module only has a DEV version for D7, but right now there are over 31K users using this version. Therefor I think "DEV" is not a valid release qualifier for that version anymore, because +31K users can't be wrong, right?

  2. Filtering Views results in a multi language site can sometimes be challenging, as illustrated also in discussions such as in https://groups.drupal.org/node/47478 .

  • Hi,I have a view of taxonomy terms.The site has 2 languages:English and Greek.I want,when I select the greek language,the view to show the greek terms.Currently it only shows english terms,whatever the language is.I will try Internationazation view and I will tell the result – sokratis May 17 '15 at 11:48
  • OK, is the view a page or block? – Pierre.Vriens May 17 '15 at 11:57
  • It is a view block – sokratis May 17 '15 at 11:57
  • Yep,that was it,I had to install the Internationalization views and select Term name:Translated.Thanks guys! – sokratis May 17 '15 at 12:12
4

Instead of adding the term: name, add term: name (localized) to your view.

  • I cannot see such field,is there something I need to do so as this field to appear? – sokratis May 17 '15 at 11:22
  • What i18n modules do you have installed and what translation option did you chose on your vocabulary? – Maarten De Block May 17 '15 at 11:30
  • I have the Internationalization and the Taxonomy Translation modules enabled.Also I tried both " Localize. Terms are common for all languages, but their name and description may be localized." and "Translate. Different terms will be allowed for each language and they can be translated." but still no luck – sokratis May 17 '15 at 11:35
  • You'll probably need drupal.org/project/i18nviews as well. – Maarten De Block May 17 '15 at 11:44
2

You need to add multilingual content for each term in your Taxonomy and inside the Views block use the "Content: Tag (field_tag) (translated)" term as exposed filter. Similarly if you want to display Node Title we need to use "Entity translation: Node Title: translated" fields.

1

You need to install the i18n module, enable taxonomy translation and then do that programatically, for example in a block implemented by a custom module that calls i18n_taxonomy_localize_terms().

0

Assuming you're using Drupal 7 and the Taxonomy Translation module (i18n_taxonomy, part of Internationalization (i18n)...

I wrote the following for one of my clients. It works for the sites it was intended for, but as Drupal configurations can differ massively from one site to another, YMMV :)

  • Works for a vocabulary with translation mode set to Localize. Terms are common for all languages, but their name and description may be localized.
  • Works when the View displays content (base table is node) and displays taxonomy terms via Content: All taxonomy terms.
  • Also works when the View is displaying taxonomy (base table is taxonomy_term_data).

Note use of taxonomy_term_load_multiple for better performance. There are some solutions offered elsewhere that load terms one by one and translate them one by one.

/*
 * Implement hook_views_pre_render(&$view)
 * Here we handle translation of taxonomy terms.
 * Note: i18n_taxonomy_views module has its own _views_pre_render hook 
 * but that does not currently (7.x-1.18) do what we need here.
 * This could probably be done using HOOK_views_post_execute instead.
 */
function mymodule_views_pre_render(&$view) {
  // If the i18n_taxonomy module is not installed then we have nothing to do.
  if (!function_exists('i18n_taxonomy_localize_terms')) {
    return;
  }

  // ----- Processing for content-based (nodes) View -----
  if ($view->base_table == 'node') {
    //dpm($view->field['term_node_tid']->items, 'pre render: field[term_node_tid]->items');

    // If there is no term data then exit now. 
    if (empty($view->field['term_node_tid']->items)) {
      return;
    }

    // Array to track tids used and placeholders for term name translations.
    $term_translations = array();
    // Loop through nodes in the result set, track the term ids used and set a reference to the translation...
    foreach ($view->field['term_node_tid']->items as $nid => &$vterms) {
      // Loop through all terms for this node.
      foreach ($vterms as $tid => &$vterm) {
        // Track this term id.
        $term_translations[$tid] = '';
        // Store a *reference* to the translated term name which will be loaded later (below).  
        $vterm['name'] =& $term_translations[$tid];
      }
    }

    // Load the terms that we need.
    $terms = taxonomy_term_load_multiple(array_keys($term_translations));
    // Fetch the translations of the terms.
    $terms= i18n_taxonomy_localize_terms($terms);
    // Update our $term_translations array with the translated term names. 
    foreach ($terms as $tid => $term) {
      $term_translations[$tid] = $term->name;
    }
  }

  // ----- Processing for taxonomy-based View -----
  elseif ($view->base_table == 'taxonomy_term_data') {
    // Reference the terms array from $view->result.
    $vterms =& $view->result;

    // Build an array of tids...
    $tids = array();
    foreach ($vterms as $vterm) {
      $tids[] = $vterm->tid;
    }

    // Load all terms.
    $terms = taxonomy_term_load_multiple($tids);
    // Translate the terms.
    $terms = i18n_taxonomy_localize_terms($terms);

    // Copy translations for term names and descriptions back into the $view->result.
    foreach($vterms as &$vterm) {
      $vterm->taxonomy_term_data_name = $terms[$vterm->tid]->name;
      $vterm->taxonomy_term_data_description = $terms[$vterm->tid]->description;
    }
  }
}

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.