5 * Contains the base class for views localization plugins.
9 * @defgroup views_localization_plugins Views localization plugins
13 * @see hook_views_plugins()
17 * The base plugin to handle localization of Views strings.
19 class views_plugin_localization extends views_plugin {
20 // Store for exported strings
21 var $export_strings = array();
22 var $translate = TRUE;
25 * Initialize the plugin.
30 function init(&$view) {
35 * Translate a string / text with format
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'.
44 * Full data for the string to be translated.
47 * Translated string / text
49 function translate($source) {
50 // Allow other modules to make changes to the string before and after translation
51 $source['pre_process'] = $this->invoke_translation_process($source, 'pre');
52 $source['translation'] = $this->translate_string($source['value'], $source['keys'], $source['format']);
53 $source['post_process'] = $this->invoke_translation_process($source, 'post');
54 return $source['translation'];
61 * The string to be translated.
63 * An array of keys to identify the string. Generally constructed from
64 * view name, display_id, and a property, e.g., 'header'.
66 * The input format of the string. This is optional.
68 function translate_string($string, $keys = array(), $format = '') {}
71 * Save string source for translation.
74 * Full data for the string to be translated.
76 function save($source) {
77 // Allow other modules to make changes to the string before saving
78 $source['pre_process'] = $this->invoke_translation_process($source, 'pre');
79 $this->save_string($source['value'], $source['keys'], isset($source['format']) ? $source['format'] : '');
83 * Save a string for translation
86 * The string to be translated.
88 * An array of keys to identify the string. Generally constructed from
89 * view name, display_id, and a property, e.g., 'header'.
91 * The input format of the string. This is optional.
93 function save_string($string, $keys = array(), $format = '') {}
99 * Full data for the string to be translated.
101 function delete($source) { }
104 * Collect strings to be exported to code.
107 * Full data for the string to be translated.
109 function export($source) { }
112 * Render any collected exported strings to code.
115 * An optional indentation for prettifying nested code.
117 function export_render($indent = ' ') { }
120 * Invoke hook_translation_pre_process() or hook_translation_post_process().
122 * Like node_invoke_nodeapi(), this function is needed to enable both passing
123 * by reference and fetching return values.
125 function invoke_translation_process(&$value, $op) {
127 $hook = 'translation_' . $op . '_process';
128 foreach (module_implements($hook) as $module) {
129 $function = $module . '_' . $hook;
130 $result = $function($value);
131 if (isset($result)) {
132 $return[$module] = $result;
138 function process_locale_strings($op) {
139 $this->view->init_display();
141 foreach ($this->view->display as $display_id => $display) {
142 $translatable = array();
143 // Special handling for display title.
144 if (isset($display->display_title)) {
145 $translatable[] = array('value' => $display->display_title, 'keys' => array('display_title'));
148 if (is_object($this->view->display[$display_id]->handler)) {
149 $this->view->display[$display_id]->handler->unpack_translatables($translatable);
151 foreach ($translatable as $data) {
152 $data['keys'] = array_merge(array($this->view->name, $display_id), $data['keys']);
158 $this->delete($data);
161 $this->export($data);