views_plugin_localization.inc

Contains the base class for views localization plugins.

File

plugins/views_plugin_localization.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Contains the base class for views localization plugins.
  5. */
  6. /**
  7. * @defgroup views_localization_plugins Views localization plugins
  8. * @{
  9. * @todo.
  10. *
  11. * @see hook_views_plugins()
  12. */
  13. /**
  14. * The base plugin to handle localization of Views strings.
  15. */
  16. class views_plugin_localization extends views_plugin {
  17. // Store for exported strings
  18. var $export_strings = array();
  19. var $translate = TRUE;
  20. /**
  21. * Initialize the plugin.
  22. *
  23. * @param $view
  24. * The view object.
  25. */
  26. function init(&$view) {
  27. $this->view = &$view;
  28. }
  29. /**
  30. * Translate a string / text with format
  31. *
  32. * The $source parameter is an array with the following elements:
  33. * - value, source string
  34. * - format, input format in case the text has some format to be applied
  35. * - keys. An array of keys to identify the string. Generally constructed from
  36. * view name, display_id, and a property, e.g., 'header'.
  37. *
  38. * @param $source
  39. * Full data for the string to be translated.
  40. *
  41. * @return string
  42. * Translated string / text
  43. */
  44. function translate($source) {
  45. // Allow other modules to make changes to the string before and after translation
  46. $source['pre_process'] = $this->invoke_translation_process($source, 'pre');
  47. $source['translation'] = $this->translate_string($source['value'], $source['keys'], $source['format']);
  48. $source['post_process'] = $this->invoke_translation_process($source, 'post');
  49. return $source['translation'];
  50. }
  51. /**
  52. * Translate a string.
  53. *
  54. * @param $string
  55. * The string to be translated.
  56. * @param $keys
  57. * An array of keys to identify the string. Generally constructed from
  58. * view name, display_id, and a property, e.g., 'header'.
  59. * @param $format
  60. * The input format of the string. This is optional.
  61. */
  62. function translate_string($string, $keys = array(), $format = '') {}
  63. /**
  64. * Save string source for translation.
  65. *
  66. * @param $source
  67. * Full data for the string to be translated.
  68. */
  69. function save($source) {
  70. // Allow other modules to make changes to the string before saving
  71. $source['pre_process'] = $this->invoke_translation_process($source, 'pre');
  72. $this->save_string($source['value'], $source['keys'], isset($source['format']) ? $source['format'] : '');
  73. }
  74. /**
  75. * Save a string for translation
  76. *
  77. * @param $string
  78. * The string to be translated.
  79. * @param $keys
  80. * An array of keys to identify the string. Generally constructed from
  81. * view name, display_id, and a property, e.g., 'header'.
  82. * @param $format
  83. * The input format of the string. This is optional.
  84. */
  85. function save_string($string, $keys = array(), $format = '') {}
  86. /**
  87. * Delete a string.
  88. *
  89. * @param $source
  90. * Full data for the string to be translated.
  91. */
  92. function delete($source) { }
  93. /**
  94. * Collect strings to be exported to code.
  95. *
  96. * @param $source
  97. * Full data for the string to be translated.
  98. */
  99. function export($source) { }
  100. /**
  101. * Render any collected exported strings to code.
  102. *
  103. * @param $indent
  104. * An optional indentation for prettifying nested code.
  105. */
  106. function export_render($indent = ' ') { }
  107. /**
  108. * Invoke hook_translation_pre_process() or hook_translation_post_process().
  109. *
  110. * Like node_invoke_nodeapi(), this function is needed to enable both passing
  111. * by reference and fetching return values.
  112. */
  113. function invoke_translation_process(&$value, $op) {
  114. $return = array();
  115. $hook = 'translation_' . $op . '_process';
  116. foreach (module_implements($hook) as $module) {
  117. $function = $module . '_' . $hook;
  118. $result = $function($value);
  119. if (isset($result)) {
  120. $return[$module] = $result;
  121. }
  122. }
  123. return $return;
  124. }
  125. function process_locale_strings($op) {
  126. $this->view->init_display();
  127. foreach ($this->view->display as $display_id => $display) {
  128. $translatable = array();
  129. // Special handling for display title.
  130. if (isset($display->display_title)) {
  131. $translatable[] = array('value' => $display->display_title, 'keys' => array('display_title'));
  132. }
  133. // Unpack handlers.
  134. if (is_object($this->view->display[$display_id]->handler)) {
  135. $this->view->display[$display_id]->handler->unpack_translatables($translatable);
  136. }
  137. foreach ($translatable as $data) {
  138. $data['keys'] = array_merge(array($this->view->name, $display_id), $data['keys']);
  139. switch ($op) {
  140. case 'save':
  141. $this->save($data);
  142. break;
  143. case 'delete':
  144. $this->delete($data);
  145. break;
  146. case 'export':
  147. $this->export($data);
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. /**
  155. * @}
  156. */