Entity view (Content)

Drupal 5 Node Selection: Internationalization Module

By sobayashi
Jun. 19, 2009

My current project involves adding multilingual capability to a Drupal 5 website using the Internationalization (i18n) module. After enabling i18n, I ran into a small issue with contents from Views module.

After selecting a language for a node, that node disappeared from a node list built by Views on other language pages. After adding translations to the node, the translation node was displayed on the page again.

I wanted the option to choose a language, and if it didn't exist, the default language and also no language.

Internationalization module allows you to choose ‘content selection mode’ on Multilingual settings page.

There are 5 options for Drupal 5:
1) Current language / no language
2) Current language / default language / no language
3) Default language / no language
4) Current language
5) All content. No language conditions apply

However, none of the options perfectly matches my need.

I came up with this solution:

Choose option 2) for ‘content selection mode’, and implement hook_views_pre_view() to remove duplicate content. The second option gives you both current language and default language nodes of the same content so you need to filter out the default language node if it has translation.

hook_views_pre_view() could be like this:

/**
* Implementation of hook_views_pre_view()
*/
function example_views_pre_view($view, &$items) {
  $lang = i18n_get_lang();
  $translations = array();

  //Find nodes which have translations
  foreach($items as $key => $val) {
    $result = db_query("SELECT trid, language FROM {i18n_node} WHERE nid = %d AND trid != 0 AND language != '' AND language IS NOT NULL", $val->nid);

    if($obj = db_fetch_object($result)) {
      $translations[$obj->trid][$key] = $obj->language;
    }
  }

  //if a node has a translation of the current language, remove the other translation and English nodes of the same content
  foreach($translations as $key => $val) {
    if((count($val) > 1) && (($index = array_search($lang, $val)) !== FALSE)) {
      foreach($val as $key2 => $val2) {
        if($key2 != $index) {
          unset($items[$key2]);
        }
      }
    }
  }

  return '';
}

Upgrading to Drupal 6 is another option. Internationalization module of D6 provides an option ‘Mixed current language (if available) or default language (if not) and language neutral,’ which works as I wanted.

Feel free to add your better solutions for this kind of issue with Internationalization module.

Post Tags: