By sokratis


2015-05-17 10:53:00 8 Comments

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?

5 comments

@netgenius.co.uk 2017-09-15 12:05:35

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;
    }
  }
}

@Pierre.Vriens 2015-05-17 11:44:52

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 .

@sokratis 2015-05-17 11:48:59

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

@Pierre.Vriens 2015-05-17 11:57:17

OK, is the view a page or block?

@sokratis 2015-05-17 11:57:39

It is a view block

@sokratis 2015-05-17 12:12:11

Yep,that was it,I had to install the Internationalization views and select Term name:Translated.Thanks guys!

@rémy 2015-08-24 07:41:11

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().

@Kapil Ropalekar 2015-08-24 07:58:27

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.

@Maarten De Block 2015-05-17 11:19:31

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

@sokratis 2015-05-17 11:22:39

I cannot see such field,is there something I need to do so as this field to appear?

@Maarten De Block 2015-05-17 11:30:16

What i18n modules do you have installed and what translation option did you chose on your vocabulary?

@sokratis 2015-05-17 11:35:06

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

@Maarten De Block 2015-05-17 11:44:03

You'll probably need drupal.org/project/i18nviews as well.

Related Questions

Sponsored Content

1 Answered Questions

How to show translated taxonomy terms in term listing in content listing style

1 Answered Questions

"Page not found" on translated page

1 Answered Questions

[SOLVED] How can you filter out other languages Taxonomy Terms from an exposed filter?

  • 2018-02-22 21:36:27
  • shanabus
  • 109 View
  • 0 Score
  • 1 Answer
  • Tags:   7 views i18n-l10n

1 Answered Questions

Taxonomy URL incorrectly translated

1 Answered Questions

How do I translate domain:site_name in the view page?

1 Answered Questions

[SOLVED] Displaying translated taxonomy terms

1 Answered Questions

[SOLVED] How to make translated taxonomy terms correctly be shown in a view?

2 Answered Questions

[SOLVED] Views: changing the language for taxonomy terms

Sponsored Content