i18nviews.module 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * Views support for Internationalization (i18n) package
  5. *
  6. * This module translates some views strings on the fly using i18n string system
  7. *
  8. * @author Miro Dietiker, 2011
  9. * @author Jose A. Reyero, 2007
  10. */
  11. /**
  12. * Implementation of hook_help().
  13. */
  14. function i18nviews_help($path, $arg) {
  15. switch ($path) {
  16. case 'admin/modules#description' :
  17. $output = '<p>'. t('Supports translation for views strings: title, header, footer...') .'</p>';
  18. $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>';
  19. return $output;
  20. }
  21. }
  22. /**
  23. * Returns true if i18nstrings is used as views localization plugin.
  24. */
  25. function i18nviews_localization_plugin_enabled() {
  26. return variable_get('views_localization_plugin', 'core') == 'i18nstrings';
  27. }
  28. /**
  29. * Field handler for taxonomy term fields.
  30. *
  31. * Remake of views_handler_field_allterms with term name translation.
  32. *
  33. * TODO CHECK
  34. */
  35. function i18nviews_views_handler_field_allterms($fieldinfo, $fielddata, $value, $data) {
  36. if ($fieldinfo['vocabulary']) {
  37. $terms = taxonomy_node_get_terms_by_vocabulary($data->nid, $fieldinfo['vocabulary']);
  38. }
  39. else {
  40. $terms = taxonomy_node_get_terms($data->nid);
  41. }
  42. // Translate all these terms.
  43. _i18ntaxonomy_translate_terms($terms);
  44. if ($fielddata['options'] == 'nolink') {
  45. foreach ($terms as $term) {
  46. $links[] = check_plain($term->name);
  47. }
  48. $links = !empty($links) ? implode(' | ', $links) : '';
  49. }
  50. else {
  51. $node = new stdClass();
  52. $node->taxonomy = $terms;
  53. $links = theme('links', taxonomy_link('taxonomy terms', $node));
  54. }
  55. return $links;
  56. }
  57. /**
  58. * Implementation of hook_views_api().
  59. */
  60. function i18nviews_views_api() {
  61. return array(
  62. 'api' => '3.0',
  63. 'path' => drupal_get_path('module', 'i18nviews') . '/includes',
  64. );
  65. }
  66. /**
  67. * Implements hook_menu().
  68. */
  69. function i18nviews_menu() {
  70. $items['admin/structure/views/view/%views_ui_cache/translate'] = array(
  71. 'title' => 'Translate',
  72. 'access callback' => 'i18nviews_translate_tab_access',
  73. 'page callback' => 'i18n_string_object_translate_page',
  74. 'page arguments' => array('views', 4),
  75. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  76. 'weight' => 10,
  77. );
  78. $items['admin/structure/views/view/%views_ui_cache/translate/%'] = array(
  79. 'title' => 'Translate',
  80. 'access callback' => 'i18nviews_translate_tab_access',
  81. 'page callback' => 'i18nviews_object_translate_page',
  82. 'page arguments' => array(4, 6),
  83. 'file' => 'i18nviews.pages.inc',
  84. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  85. );
  86. return $items;
  87. }
  88. /**
  89. * Menu access callback function.
  90. *
  91. * Views editors required to have both views and locale admin.
  92. */
  93. function i18nviews_translate_tab_access() {
  94. return user_access('translate interface') && user_access('administer views');
  95. }
  96. /**
  97. * Find the location source for a certain expression.
  98. */
  99. function i18nviews_locale_source($translation, $textgroup, $language = NULL) {
  100. if (!isset($language)) {
  101. $language = $GLOBALS['language']->language;
  102. }
  103. $select = db_select('locales_source', 'ls');
  104. $select->innerJoin('locales_target', 'lt', '(ls.lid = lt.lid AND lt.translation = :translation AND lt.language = :language AND ls.textgroup = :textgroup )', array(
  105. ':translation' => $translation,
  106. ':language' => $language,
  107. ':textgroup' => $textgroup,
  108. ));
  109. $select->fields('ls', array('source'));
  110. return $select->execute()->fetchObject();
  111. }
  112. /**
  113. * Implements hook_ctools_plugin_post_alter
  114. *
  115. * Register Translate operation in ctools export ui.
  116. *
  117. * @param $plugin
  118. * An associative array defining a plugin.
  119. * @param $info
  120. * An associative array of plugin type info.
  121. */
  122. function i18nviews_ctools_plugin_post_alter(&$plugin, &$info) {
  123. if ($info['type'] == 'export_ui' && $plugin['module'] == 'views_ui' && i18nviews_localization_plugin_enabled()) {
  124. $plugin['allowed operations']['translate']['title'] = t('Translate');
  125. $plugin['menu']['items']['translate'] = array(
  126. 'path' => 'view/%ctools_export_ui/translate',
  127. 'title' => t('Translate'),
  128. );
  129. }
  130. }