Skip to content
i18nviews.module 4.22 KiB
Newer Older
<?php
// $Id$

/**
 * @file
 * Views support for Internationalization (i18n) package
 *
 * This module translates some views strings on the fly using i18n string system
 * @author Jose A. Reyero, 2007
 */

/**
 * Implementation of hook_help()
 */
function i18nviews_help($section, $arg) {
  switch ($section) {
    case 'admin/modules#description' :
      $output = '<p>' . t('Supports translation for views strings: title, header, footer...') . '</p>';
      $output .= '<p>' . t('To search and translate strings, use the <a href="@translate-interface">translation interface</a> pages.', array('@translate-interface' => url('admin/build/translate'))) . '</p>';
/**
 * Implementation of hook_locale().
 */
function i18nviews_locale($op = 'groups', $group = NULL) {
  switch ($op) {
    case 'groups':
      return array('views' => t('Views'));
    case 'refresh':
      if ($group == 'views') {
        return i18nviews_locale_refresh();
      }
  }
}

/**
 * Refresh views locales
 */
function i18nviews_locale_refresh() {
  // To be implemented
}

/**
 * Implementation of hook_views_pre_view().
 * Views are identified by $view->name
 * @TODO This needs a lot of research and work
 * @TODO Ask Earl
function i18nviews_views_pre_view(&$view, &$display_id, &$args) {
  // Translate all possible display strings, step by step
  // I.e. for a page, there seems to be a 'page' and a 'default' display
  $fields = array('title', 'header', 'footer', 'empty');
  foreach (array($display_id, 'default') as $display) {
    if (!empty($view->display[$display])) {
      _i18nviews_localize_array($view->name, $display, $view->display[$display]->handler->options, $fields, TRUE);
    }
  }
  // Translate taxonomy fields
  // @todo I don think this works at all
  if (module_exists('i18ntaxonomy') && is_array($view->field)) {
    $translate = variable_get('i18ntaxonomy_vocabularies', array());
    foreach ($view->field as $index => $data) {
      $matches = array();
      if($data['id'] == 'term_node.name') {
        // That's a full taxonomy box
        $view->field[$index]['handler'] = 'i18ntaxonomy_views_handler_field_allterms';
      } elseif(preg_match("/term_node_(\d+)\.name/", $data['id'], $matches)) {
        $vid = $matches[1];
        if ($translate[$vid]) {
          // Set new handler for this field
          $view->field[$index]['handler'] = 'i18ntaxonomy_views_handler_field_allterms';
        }
      }
    }
  }

}

 * We get the translated fields out of the array so they are not translated again.
 */
function _i18nviews_localize_array($name, $group, &$data, &$field_names, $trim = FALSE) {
  $translated = array();
  foreach ($field_names as $field) {
    if (!empty($data[$field])) {
      $data[$field] = tt("views:$name:$group:$field", $data[$field]);
      $translated[] = $field;
    }
  }
  if ($trim && $translated) {
    $field_names = array_diff($field_names, $translated);
  }
}

/**
 * Translate a group of fields for an object.
 * We cannot play with object 2 array conversion because some are real typed objects.
 */
function _i18nviews_localize_object($name, $group, &$data, &$field_names, $trim = FALSE) {
  $translated = array();
  foreach ($field_names as $field) {
    if (!empty($data->$field)) {
      $data->$field = tt("views:$name:$group:$field", $data->$field);
    }
  }
  if ($trim && $translated) {
    $field_names = array_diff($field_names, $translated);
  }
}

/**
 * Field handler for taxonomy term fields
 * Remake of views_handler_field_allterms with term name translation
 */
function i18ntaxonomy_views_handler_field_allterms($fieldinfo, $fielddata, $value, $data) {
  if ($fieldinfo['vocabulary']) {
    $terms = taxonomy_node_get_terms_by_vocabulary($data->nid, $fieldinfo['vocabulary']);
  }
  else {
    $terms = taxonomy_node_get_terms($data->nid);
  }
  // Translate all these terms
  _i18ntaxonomy_translate_terms($terms);
  if ($fielddata['options'] == 'nolink') {
    foreach ($terms as $term) {
      $links[] = check_plain($term->name);
    }
    $links = !empty($links) ? implode(' | ', $links) : '';
  }
  else {
    $node = new stdClass();
    $node->taxonomy = $terms;
    $links = theme('links', taxonomy_link('taxonomy terms', $node));
  }
  return $links;
}