commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / views / plugins / views_plugin_localization.inc
1 <?php
2
3 /**
4 * @file
5 * Contains the base class for views localization plugins.
6 */
7
8 /**
9 * @defgroup views_localization_plugins Views localization plugins
10 * @{
11 * @todo.
12 *
13 * @see hook_views_plugins()
14 */
15
16 /**
17 * The base plugin to handle localization of Views strings.
18 */
19 class views_plugin_localization extends views_plugin {
20 // Store for exported strings
21 var $export_strings = array();
22 var $translate = TRUE;
23
24 /**
25 * Initialize the plugin.
26 *
27 * @param $view
28 * The view object.
29 */
30 function init(&$view) {
31 $this->view = &$view;
32 }
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 $source
44 * Full data for the string to be translated.
45 *
46 * @return string
47 * Translated string / text
48 */
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'];
55 }
56
57 /**
58 * Translate a string.
59 *
60 * @param $string
61 * The string to be translated.
62 * @param $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 $format
66 * The input format of the string. This is optional.
67 */
68 function translate_string($string, $keys = array(), $format = '') {}
69
70 /**
71 * Save string source for translation.
72 *
73 * @param $source
74 * Full data for the string to be translated.
75 */
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'] : '');
80 }
81
82 /**
83 * Save a string for translation
84 *
85 * @param $string
86 * The string to be translated.
87 * @param $keys
88 * An array of keys to identify the string. Generally constructed from
89 * view name, display_id, and a property, e.g., 'header'.
90 * @param $format
91 * The input format of the string. This is optional.
92 */
93 function save_string($string, $keys = array(), $format = '') {}
94
95 /**
96 * Delete a string.
97 *
98 * @param $source
99 * Full data for the string to be translated.
100 */
101 function delete($source) { }
102
103 /**
104 * Collect strings to be exported to code.
105 *
106 * @param $source
107 * Full data for the string to be translated.
108 */
109 function export($source) { }
110
111 /**
112 * Render any collected exported strings to code.
113 *
114 * @param $indent
115 * An optional indentation for prettifying nested code.
116 */
117 function export_render($indent = ' ') { }
118
119 /**
120 * Invoke hook_translation_pre_process() or hook_translation_post_process().
121 *
122 * Like node_invoke_nodeapi(), this function is needed to enable both passing
123 * by reference and fetching return values.
124 */
125 function invoke_translation_process(&$value, $op) {
126 $return = array();
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;
133 }
134 }
135 return $return;
136 }
137
138 function process_locale_strings($op) {
139 $this->view->init_display();
140
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'));
146 }
147 // Unpack handlers.
148 if (is_object($this->view->display[$display_id]->handler)) {
149 $this->view->display[$display_id]->handler->unpack_translatables($translatable);
150 }
151 foreach ($translatable as $data) {
152 $data['keys'] = array_merge(array($this->view->name, $display_id), $data['keys']);
153 switch ($op) {
154 case 'save':
155 $this->save($data);
156 break;
157 case 'delete':
158 $this->delete($data);
159 break;
160 case 'export':
161 $this->export($data);
162 break;
163 }
164 }
165 }
166 }
167 }
168
169 /**
170 * @}
171 */