views_plugin_localization.inc 4.9 KB

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