webmaster@1
|
1 <?php |
webmaster@1
|
2 // $Id: locale.inc,v 1.174 2008/01/09 21:36:13 goba Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Administration functions for locale.module. |
webmaster@1
|
7 */ |
webmaster@1
|
8 |
webmaster@1
|
9 define('LOCALE_JS_STRING', '(?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+'); |
webmaster@1
|
10 |
webmaster@1
|
11 /** |
webmaster@1
|
12 * Translation import mode overwriting all existing translations |
webmaster@1
|
13 * if new translated version available. |
webmaster@1
|
14 */ |
webmaster@1
|
15 define('LOCALE_IMPORT_OVERWRITE', 0); |
webmaster@1
|
16 |
webmaster@1
|
17 /** |
webmaster@1
|
18 * Translation import mode keeping existing translations and only |
webmaster@1
|
19 * inserting new strings. |
webmaster@1
|
20 */ |
webmaster@1
|
21 define('LOCALE_IMPORT_KEEP', 1); |
webmaster@1
|
22 |
webmaster@1
|
23 /** |
webmaster@1
|
24 * @defgroup locale-language-overview Language overview functionality |
webmaster@1
|
25 * @{ |
webmaster@1
|
26 */ |
webmaster@1
|
27 |
webmaster@1
|
28 /** |
webmaster@1
|
29 * User interface for the language overview screen. |
webmaster@1
|
30 */ |
webmaster@1
|
31 function locale_languages_overview_form() { |
webmaster@1
|
32 $languages = language_list('language', TRUE); |
webmaster@1
|
33 |
webmaster@1
|
34 $options = array(); |
webmaster@1
|
35 $form['weight'] = array('#tree' => TRUE); |
webmaster@1
|
36 foreach ($languages as $langcode => $language) { |
webmaster@1
|
37 |
webmaster@1
|
38 $options[$langcode] = ''; |
webmaster@1
|
39 if ($language->enabled) { |
webmaster@1
|
40 $enabled[] = $langcode; |
webmaster@1
|
41 } |
webmaster@1
|
42 $form['weight'][$langcode] = array( |
webmaster@1
|
43 '#type' => 'weight', |
webmaster@1
|
44 '#default_value' => $language->weight |
webmaster@1
|
45 ); |
webmaster@1
|
46 $form['name'][$langcode] = array('#value' => check_plain($language->name)); |
webmaster@1
|
47 $form['native'][$langcode] = array('#value' => check_plain($language->native)); |
webmaster@1
|
48 $form['direction'][$langcode] = array('#value' => ($language->direction == LANGUAGE_RTL ? t('Right to left') : t('Left to right'))); |
webmaster@1
|
49 } |
webmaster@1
|
50 $form['enabled'] = array('#type' => 'checkboxes', |
webmaster@1
|
51 '#options' => $options, |
webmaster@1
|
52 '#default_value' => $enabled, |
webmaster@1
|
53 ); |
webmaster@1
|
54 $form['site_default'] = array('#type' => 'radios', |
webmaster@1
|
55 '#options' => $options, |
webmaster@1
|
56 '#default_value' => language_default('language'), |
webmaster@1
|
57 ); |
webmaster@1
|
58 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration')); |
webmaster@1
|
59 $form['#theme'] = 'locale_languages_overview_form'; |
webmaster@1
|
60 |
webmaster@1
|
61 return $form; |
webmaster@1
|
62 } |
webmaster@1
|
63 |
webmaster@1
|
64 /** |
webmaster@1
|
65 * Theme the language overview form. |
webmaster@1
|
66 * |
webmaster@1
|
67 * @ingroup themeable |
webmaster@1
|
68 */ |
webmaster@1
|
69 function theme_locale_languages_overview_form($form) { |
webmaster@1
|
70 $default = language_default(); |
webmaster@1
|
71 foreach ($form['name'] as $key => $element) { |
webmaster@1
|
72 // Do not take form control structures. |
webmaster@1
|
73 if (is_array($element) && element_child($key)) { |
webmaster@1
|
74 // Disable checkbox for the default language, because it cannot be disabled. |
webmaster@1
|
75 if ($key == $default->language) { |
webmaster@1
|
76 $form['enabled'][$key]['#attributes']['disabled'] = 'disabled'; |
webmaster@1
|
77 } |
webmaster@1
|
78 $rows[] = array( |
webmaster@1
|
79 array('data' => drupal_render($form['enabled'][$key]), 'align' => 'center'), |
webmaster@1
|
80 check_plain($key), |
webmaster@1
|
81 '<strong>'. drupal_render($form['name'][$key]) .'</strong>', |
webmaster@1
|
82 drupal_render($form['native'][$key]), |
webmaster@1
|
83 drupal_render($form['direction'][$key]), |
webmaster@1
|
84 drupal_render($form['site_default'][$key]), |
webmaster@1
|
85 drupal_render($form['weight'][$key]), |
webmaster@1
|
86 l(t('edit'), 'admin/settings/language/edit/'. $key) . (($key != 'en' && $key != $default->language) ? ' '. l(t('delete'), 'admin/settings/language/delete/'. $key) : '') |
webmaster@1
|
87 ); |
webmaster@1
|
88 } |
webmaster@1
|
89 } |
webmaster@1
|
90 $header = array(array('data' => t('Enabled')), array('data' => t('Code')), array('data' => t('English name')), array('data' => t('Native name')), array('data' => t('Direction')), array('data' => t('Default')), array('data' => t('Weight')), array('data' => t('Operations'))); |
webmaster@1
|
91 $output = theme('table', $header, $rows); |
webmaster@1
|
92 $output .= drupal_render($form); |
webmaster@1
|
93 |
webmaster@1
|
94 return $output; |
webmaster@1
|
95 } |
webmaster@1
|
96 |
webmaster@1
|
97 /** |
webmaster@1
|
98 * Process language overview form submissions, updating existing languages. |
webmaster@1
|
99 */ |
webmaster@1
|
100 function locale_languages_overview_form_submit($form, &$form_state) { |
webmaster@1
|
101 $languages = language_list(); |
webmaster@1
|
102 $default = language_default(); |
webmaster@1
|
103 $enabled_count = 0; |
webmaster@1
|
104 foreach ($languages as $langcode => $language) { |
webmaster@1
|
105 if ($form_state['values']['site_default'] == $langcode || $default->language == $langcode) { |
webmaster@1
|
106 // Automatically enable the default language and the language |
webmaster@1
|
107 // which was default previously (because we will not get the |
webmaster@1
|
108 // value from that disabled checkox). |
webmaster@1
|
109 $form_state['values']['enabled'][$langcode] = 1; |
webmaster@1
|
110 } |
webmaster@1
|
111 if ($form_state['values']['enabled'][$langcode]) { |
webmaster@1
|
112 $enabled_count++; |
webmaster@1
|
113 $language->enabled = 1; |
webmaster@1
|
114 } |
webmaster@1
|
115 else { |
webmaster@1
|
116 $language->enabled = 0; |
webmaster@1
|
117 } |
webmaster@1
|
118 $language->weight = $form_state['values']['weight'][$langcode]; |
webmaster@1
|
119 db_query("UPDATE {languages} SET enabled = %d, weight = %d WHERE language = '%s'", $language->enabled, $language->weight, $langcode); |
webmaster@1
|
120 $languages[$langcode] = $language; |
webmaster@1
|
121 } |
webmaster@1
|
122 drupal_set_message(t('Configuration saved.')); |
webmaster@1
|
123 variable_set('language_default', $languages[$form_state['values']['site_default']]); |
webmaster@1
|
124 variable_set('language_count', $enabled_count); |
webmaster@1
|
125 |
webmaster@1
|
126 // Changing the language settings impacts the interface. |
webmaster@1
|
127 cache_clear_all('*', 'cache_page', TRUE); |
webmaster@1
|
128 |
webmaster@1
|
129 $form_state['redirect'] = 'admin/settings/language'; |
webmaster@1
|
130 return; |
webmaster@1
|
131 } |
webmaster@1
|
132 /** |
webmaster@1
|
133 * @} End of "locale-language-overview" |
webmaster@1
|
134 */ |
webmaster@1
|
135 |
webmaster@1
|
136 /** |
webmaster@1
|
137 * @defgroup locale-language-add-edit Language addition and editing functionality |
webmaster@1
|
138 * @{ |
webmaster@1
|
139 */ |
webmaster@1
|
140 |
webmaster@1
|
141 /** |
webmaster@1
|
142 * User interface for the language addition screen. |
webmaster@1
|
143 */ |
webmaster@1
|
144 function locale_languages_add_screen() { |
webmaster@1
|
145 $output = drupal_get_form('locale_languages_predefined_form'); |
webmaster@1
|
146 $output .= drupal_get_form('locale_languages_custom_form'); |
webmaster@1
|
147 return $output; |
webmaster@1
|
148 } |
webmaster@1
|
149 |
webmaster@1
|
150 /** |
webmaster@1
|
151 * Predefined language setup form. |
webmaster@1
|
152 */ |
webmaster@1
|
153 function locale_languages_predefined_form() { |
webmaster@1
|
154 $predefined = _locale_prepare_predefined_list(); |
webmaster@1
|
155 $form = array(); |
webmaster@1
|
156 $form['language list'] = array('#type' => 'fieldset', |
webmaster@1
|
157 '#title' => t('Predefined language'), |
webmaster@1
|
158 '#collapsible' => TRUE, |
webmaster@1
|
159 ); |
webmaster@1
|
160 $form['language list']['langcode'] = array('#type' => 'select', |
webmaster@1
|
161 '#title' => t('Language name'), |
webmaster@1
|
162 '#default_value' => key($predefined), |
webmaster@1
|
163 '#options' => $predefined, |
webmaster@1
|
164 '#description' => t('Select the desired language and click the <em>Add language</em> button. (Use the <em>Custom language</em> options if your desired language does not appear in this list.)'), |
webmaster@1
|
165 ); |
webmaster@1
|
166 $form['language list']['submit'] = array('#type' => 'submit', '#value' => t('Add language')); |
webmaster@1
|
167 return $form; |
webmaster@1
|
168 } |
webmaster@1
|
169 |
webmaster@1
|
170 /** |
webmaster@1
|
171 * Custom language addition form. |
webmaster@1
|
172 */ |
webmaster@1
|
173 function locale_languages_custom_form() { |
webmaster@1
|
174 $form = array(); |
webmaster@1
|
175 $form['custom language'] = array('#type' => 'fieldset', |
webmaster@1
|
176 '#title' => t('Custom language'), |
webmaster@1
|
177 '#collapsible' => TRUE, |
webmaster@1
|
178 '#collapsed' => TRUE, |
webmaster@1
|
179 ); |
webmaster@1
|
180 _locale_languages_common_controls($form['custom language']); |
webmaster@1
|
181 $form['custom language']['submit'] = array( |
webmaster@1
|
182 '#type' => 'submit', |
webmaster@1
|
183 '#value' => t('Add custom language') |
webmaster@1
|
184 ); |
webmaster@1
|
185 // Reuse the validation and submit functions of the predefined language setup form. |
webmaster@1
|
186 $form['#submit'][] = 'locale_languages_predefined_form_submit'; |
webmaster@1
|
187 $form['#validate'][] = 'locale_languages_predefined_form_validate'; |
webmaster@1
|
188 return $form; |
webmaster@1
|
189 } |
webmaster@1
|
190 |
webmaster@1
|
191 /** |
webmaster@1
|
192 * Editing screen for a particular language. |
webmaster@1
|
193 * |
webmaster@1
|
194 * @param $langcode |
webmaster@1
|
195 * Language code of the language to edit. |
webmaster@1
|
196 */ |
webmaster@1
|
197 function locale_languages_edit_form(&$form_state, $langcode) { |
webmaster@1
|
198 if ($language = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $langcode))) { |
webmaster@1
|
199 $form = array(); |
webmaster@1
|
200 _locale_languages_common_controls($form, $language); |
webmaster@1
|
201 $form['submit'] = array( |
webmaster@1
|
202 '#type' => 'submit', |
webmaster@1
|
203 '#value' => t('Save language') |
webmaster@1
|
204 ); |
webmaster@1
|
205 $form['#submit'][] = 'locale_languages_edit_form_submit'; |
webmaster@1
|
206 $form['#validate'][] = 'locale_languages_edit_form_validate'; |
webmaster@1
|
207 return $form; |
webmaster@1
|
208 } |
webmaster@1
|
209 else { |
webmaster@1
|
210 drupal_not_found(); |
webmaster@1
|
211 } |
webmaster@1
|
212 } |
webmaster@1
|
213 |
webmaster@1
|
214 /** |
webmaster@1
|
215 * Common elements of the language addition and editing form. |
webmaster@1
|
216 * |
webmaster@1
|
217 * @param $form |
webmaster@1
|
218 * A parent form item (or empty array) to add items below. |
webmaster@1
|
219 * @param $language |
webmaster@1
|
220 * Language object to edit. |
webmaster@1
|
221 */ |
webmaster@1
|
222 function _locale_languages_common_controls(&$form, $language = NULL) { |
webmaster@1
|
223 if (!is_object($language)) { |
webmaster@1
|
224 $language = new stdClass(); |
webmaster@1
|
225 } |
webmaster@1
|
226 if (isset($language->language)) { |
webmaster@1
|
227 $form['langcode_view'] = array( |
webmaster@1
|
228 '#type' => 'item', |
webmaster@1
|
229 '#title' => t('Language code'), |
webmaster@1
|
230 '#value' => $language->language |
webmaster@1
|
231 ); |
webmaster@1
|
232 $form['langcode'] = array( |
webmaster@1
|
233 '#type' => 'value', |
webmaster@1
|
234 '#value' => $language->language |
webmaster@1
|
235 ); |
webmaster@1
|
236 } |
webmaster@1
|
237 else { |
webmaster@1
|
238 $form['langcode'] = array('#type' => 'textfield', |
webmaster@1
|
239 '#title' => t('Language code'), |
webmaster@1
|
240 '#size' => 12, |
webmaster@1
|
241 '#maxlength' => 60, |
webmaster@1
|
242 '#required' => TRUE, |
webmaster@1
|
243 '#default_value' => @$language->language, |
webmaster@1
|
244 '#disabled' => (isset($language->language)), |
webmaster@1
|
245 '#description' => t('<a href="@rfc4646">RFC 4646</a> compliant language identifier. Language codes typically use a country code, and optionally, a script or regional variant name. <em>Examples: "en", "en-US" and "zh-Hant".</em>', array('@rfc4646' => 'http://www.ietf.org/rfc/rfc4646.txt')), |
webmaster@1
|
246 ); |
webmaster@1
|
247 } |
webmaster@1
|
248 $form['name'] = array('#type' => 'textfield', |
webmaster@1
|
249 '#title' => t('Language name in English'), |
webmaster@1
|
250 '#maxlength' => 64, |
webmaster@1
|
251 '#default_value' => @$language->name, |
webmaster@1
|
252 '#required' => TRUE, |
webmaster@1
|
253 '#description' => t('Name of the language in English. Will be available for translation in all languages.'), |
webmaster@1
|
254 ); |
webmaster@1
|
255 $form['native'] = array('#type' => 'textfield', |
webmaster@1
|
256 '#title' => t('Native language name'), |
webmaster@1
|
257 '#maxlength' => 64, |
webmaster@1
|
258 '#default_value' => @$language->native, |
webmaster@1
|
259 '#required' => TRUE, |
webmaster@1
|
260 '#description' => t('Name of the language in the language being added.'), |
webmaster@1
|
261 ); |
webmaster@1
|
262 $form['prefix'] = array('#type' => 'textfield', |
webmaster@1
|
263 '#title' => t('Path prefix'), |
webmaster@1
|
264 '#maxlength' => 64, |
webmaster@1
|
265 '#default_value' => @$language->prefix, |
webmaster@1
|
266 '#description' => t('Language code or other custom string for pattern matching within the path. With language negotiation set to <em>Path prefix only</em> or <em>Path prefix with language fallback</em>, this site is presented in this language when the Path prefix value matches an element in the path. For the default language, this value may be left blank. <strong>Modifying this value will break existing URLs and should be used with caution in a production environment.</strong> <em>Example: Specifying "deutsch" as the path prefix for German results in URLs in the form "www.example.com/deutsch/node".</em>') |
webmaster@1
|
267 ); |
webmaster@1
|
268 $form['domain'] = array('#type' => 'textfield', |
webmaster@1
|
269 '#title' => t('Language domain'), |
webmaster@1
|
270 '#maxlength' => 64, |
webmaster@1
|
271 '#default_value' => @$language->domain, |
webmaster@1
|
272 '#description' => t('Language-specific URL, with protocol. With language negotiation set to <em>Domain name only</em>, the site is presented in this language when the URL accessing the site references this domain. For the default language, this value may be left blank. <strong>This value must include a protocol as part of the string.</strong> <em>Example: Specifying "http://example.de" or "http://de.example.com" as language domains for German results in URLs in the forms "http://example.de/node" and "http://de.example.com/node", respectively.</em>'), |
webmaster@1
|
273 ); |
webmaster@1
|
274 $form['direction'] = array('#type' => 'radios', |
webmaster@1
|
275 '#title' => t('Direction'), |
webmaster@1
|
276 '#required' => TRUE, |
webmaster@1
|
277 '#description' => t('Direction that text in this language is presented.'), |
webmaster@1
|
278 '#default_value' => @$language->direction, |
webmaster@1
|
279 '#options' => array(LANGUAGE_LTR => t('Left to right'), LANGUAGE_RTL => t('Right to left')) |
webmaster@1
|
280 ); |
webmaster@1
|
281 return $form; |
webmaster@1
|
282 } |
webmaster@1
|
283 |
webmaster@1
|
284 /** |
webmaster@1
|
285 * Validate the language addition form. |
webmaster@1
|
286 */ |
webmaster@1
|
287 function locale_languages_predefined_form_validate($form, &$form_state) { |
webmaster@1
|
288 $langcode = $form_state['values']['langcode']; |
webmaster@1
|
289 |
webmaster@1
|
290 if ($duplicate = db_result(db_query("SELECT COUNT(*) FROM {languages} WHERE language = '%s'", $langcode)) != 0) { |
webmaster@1
|
291 form_set_error('langcode', t('The language %language (%code) already exists.', array('%language' => $form_state['values']['name'], '%code' => $langcode))); |
webmaster@1
|
292 } |
webmaster@1
|
293 |
webmaster@1
|
294 if (!isset($form_state['values']['name'])) { |
webmaster@1
|
295 // Predefined language selection. |
webmaster@1
|
296 $predefined = _locale_get_predefined_list(); |
webmaster@1
|
297 if (!isset($predefined[$langcode])) { |
webmaster@1
|
298 form_set_error('langcode', t('Invalid language code.')); |
webmaster@1
|
299 } |
webmaster@1
|
300 } |
webmaster@1
|
301 else { |
webmaster@1
|
302 // Reuse the editing form validation routine if we add a custom language. |
webmaster@1
|
303 locale_languages_edit_form_validate($form, $form_state); |
webmaster@1
|
304 } |
webmaster@1
|
305 } |
webmaster@1
|
306 |
webmaster@1
|
307 /** |
webmaster@1
|
308 * Process the language addition form submission. |
webmaster@1
|
309 */ |
webmaster@1
|
310 function locale_languages_predefined_form_submit($form, &$form_state) { |
webmaster@1
|
311 $langcode = $form_state['values']['langcode']; |
webmaster@1
|
312 if (isset($form_state['values']['name'])) { |
webmaster@1
|
313 // Custom language form. |
webmaster@1
|
314 locale_add_language($langcode, $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['direction'], $form_state['values']['domain'], $form_state['values']['prefix']); |
webmaster@1
|
315 drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($form_state['values']['name']), '@locale-help' => url('admin/help/locale')))); |
webmaster@1
|
316 } |
webmaster@1
|
317 else { |
webmaster@1
|
318 // Predefined language selection. |
webmaster@1
|
319 $predefined = _locale_get_predefined_list(); |
webmaster@1
|
320 locale_add_language($langcode); |
webmaster@1
|
321 drupal_set_message(t('The language %language has been created and can now be used. More information is available on the <a href="@locale-help">help screen</a>.', array('%language' => t($predefined[$langcode][0]), '@locale-help' => url('admin/help/locale')))); |
webmaster@1
|
322 } |
webmaster@1
|
323 |
webmaster@1
|
324 // See if we have language files to import for the newly added |
webmaster@1
|
325 // language, collect and import them. |
webmaster@1
|
326 if ($batch = locale_batch_by_language($langcode, '_locale_batch_language_finished')) { |
webmaster@1
|
327 batch_set($batch); |
webmaster@1
|
328 } |
webmaster@1
|
329 |
webmaster@1
|
330 $form_state['redirect'] = 'admin/settings/language'; |
webmaster@1
|
331 return; |
webmaster@1
|
332 } |
webmaster@1
|
333 |
webmaster@1
|
334 /** |
webmaster@1
|
335 * Validate the language editing form. Reused for custom language addition too. |
webmaster@1
|
336 */ |
webmaster@1
|
337 function locale_languages_edit_form_validate($form, &$form_state) { |
webmaster@1
|
338 if (!empty($form_state['values']['domain']) && !empty($form_state['values']['prefix'])) { |
webmaster@1
|
339 form_set_error('prefix', t('Domain and path prefix values should not be set at the same time.')); |
webmaster@1
|
340 } |
webmaster@1
|
341 if (!empty($form_state['values']['domain']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE domain = '%s' AND language != '%s'", $form_state['values']['domain'], $form_state['values']['langcode']))) { |
webmaster@1
|
342 form_set_error('domain', t('The domain (%domain) is already tied to a language (%language).', array('%domain' => $form_state['values']['domain'], '%language' => $duplicate->language))); |
webmaster@1
|
343 } |
webmaster@1
|
344 if (empty($form_state['values']['prefix']) && language_default('language') != $form_state['values']['langcode'] && empty($form_state['values']['domain'])) { |
webmaster@1
|
345 form_set_error('prefix', t('Only the default language can have both the domain and prefix empty.')); |
webmaster@1
|
346 } |
webmaster@1
|
347 if (!empty($form_state['values']['prefix']) && $duplicate = db_fetch_object(db_query("SELECT language FROM {languages} WHERE prefix = '%s' AND language != '%s'", $form_state['values']['prefix'], $form_state['values']['langcode']))) { |
webmaster@1
|
348 form_set_error('prefix', t('The prefix (%prefix) is already tied to a language (%language).', array('%prefix' => $form_state['values']['prefix'], '%language' => $duplicate->language))); |
webmaster@1
|
349 } |
webmaster@1
|
350 } |
webmaster@1
|
351 |
webmaster@1
|
352 /** |
webmaster@1
|
353 * Process the language editing form submission. |
webmaster@1
|
354 */ |
webmaster@1
|
355 function locale_languages_edit_form_submit($form, &$form_state) { |
webmaster@1
|
356 db_query("UPDATE {languages} SET name = '%s', native = '%s', domain = '%s', prefix = '%s', direction = %d WHERE language = '%s'", $form_state['values']['name'], $form_state['values']['native'], $form_state['values']['domain'], $form_state['values']['prefix'], $form_state['values']['direction'], $form_state['values']['langcode']); |
webmaster@1
|
357 $default = language_default(); |
webmaster@1
|
358 if ($default->language == $form_state['values']['langcode']) { |
webmaster@1
|
359 $properties = array('name', 'native', 'direction', 'enabled', 'plurals', 'formula', 'domain', 'prefix', 'weight'); |
webmaster@1
|
360 foreach ($properties as $keyname) { |
webmaster@1
|
361 if (isset($form_state['values'][$keyname])) { |
webmaster@1
|
362 $default->$keyname = $form_state['values'][$keyname]; |
webmaster@1
|
363 } |
webmaster@1
|
364 } |
webmaster@1
|
365 variable_set('language_default', $default); |
webmaster@1
|
366 } |
webmaster@1
|
367 $form_state['redirect'] = 'admin/settings/language'; |
webmaster@1
|
368 return; |
webmaster@1
|
369 } |
webmaster@1
|
370 /** |
webmaster@1
|
371 * @} End of "locale-language-add-edit" |
webmaster@1
|
372 */ |
webmaster@1
|
373 |
webmaster@1
|
374 /** |
webmaster@1
|
375 * @defgroup locale-language-delete Language deletion functionality |
webmaster@1
|
376 * @{ |
webmaster@1
|
377 */ |
webmaster@1
|
378 |
webmaster@1
|
379 /** |
webmaster@1
|
380 * User interface for the language deletion confirmation screen. |
webmaster@1
|
381 */ |
webmaster@1
|
382 function locale_languages_delete_form(&$form_state, $langcode) { |
webmaster@1
|
383 |
webmaster@1
|
384 // Do not allow deletion of English locale. |
webmaster@1
|
385 if ($langcode == 'en') { |
webmaster@1
|
386 drupal_set_message(t('The English language cannot be deleted.')); |
webmaster@1
|
387 drupal_goto('admin/settings/language'); |
webmaster@1
|
388 } |
webmaster@1
|
389 |
webmaster@1
|
390 if (language_default('language') == $langcode) { |
webmaster@1
|
391 drupal_set_message(t('The default language cannot be deleted.')); |
webmaster@1
|
392 drupal_goto('admin/settings/language'); |
webmaster@1
|
393 } |
webmaster@1
|
394 |
webmaster@1
|
395 // For other languages, warn user that data loss is ahead. |
webmaster@1
|
396 $languages = language_list(); |
webmaster@1
|
397 |
webmaster@1
|
398 if (!isset($languages[$langcode])) { |
webmaster@1
|
399 drupal_not_found(); |
webmaster@1
|
400 } |
webmaster@1
|
401 else { |
webmaster@1
|
402 $form['langcode'] = array('#type' => 'value', '#value' => $langcode); |
webmaster@1
|
403 return confirm_form($form, t('Are you sure you want to delete the language %name?', array('%name' => t($languages[$langcode]->name))), 'admin/settings/language', t('Deleting a language will remove all interface translations associated with it, and posts in this language will be set to be language neutral. This action cannot be undone.'), t('Delete'), t('Cancel')); |
webmaster@1
|
404 } |
webmaster@1
|
405 } |
webmaster@1
|
406 |
webmaster@1
|
407 /** |
webmaster@1
|
408 * Process language deletion submissions. |
webmaster@1
|
409 */ |
webmaster@1
|
410 function locale_languages_delete_form_submit($form, &$form_state) { |
webmaster@1
|
411 $languages = language_list(); |
webmaster@1
|
412 if (isset($languages[$form_state['values']['langcode']])) { |
webmaster@1
|
413 // Remove translations first. |
webmaster@1
|
414 db_query("DELETE FROM {locales_target} WHERE language = '%s'", $form_state['values']['langcode']); |
webmaster@1
|
415 cache_clear_all('locale:'. $form_state['values']['langcode'], 'cache'); |
webmaster@1
|
416 // With no translations, this removes existing JavaScript translations file. |
webmaster@1
|
417 _locale_rebuild_js($form_state['values']['langcode']); |
webmaster@1
|
418 // Remove the language. |
webmaster@1
|
419 db_query("DELETE FROM {languages} WHERE language = '%s'", $form_state['values']['langcode']); |
webmaster@1
|
420 db_query("UPDATE {node} SET language = '' WHERE language = '%s'", $form_state['values']['langcode']); |
webmaster@1
|
421 $variables = array('%locale' => $languages[$form_state['values']['langcode']]->name); |
webmaster@1
|
422 drupal_set_message(t('The language %locale has been removed.', $variables)); |
webmaster@1
|
423 watchdog('locale', 'The language %locale has been removed.', $variables); |
webmaster@1
|
424 } |
webmaster@1
|
425 |
webmaster@1
|
426 // Changing the language settings impacts the interface: |
webmaster@1
|
427 cache_clear_all('*', 'cache_page', TRUE); |
webmaster@1
|
428 |
webmaster@1
|
429 $form_state['redirect'] = 'admin/settings/language'; |
webmaster@1
|
430 return; |
webmaster@1
|
431 } |
webmaster@1
|
432 /** |
webmaster@1
|
433 * @} End of "locale-language-add-edit" |
webmaster@1
|
434 */ |
webmaster@1
|
435 |
webmaster@1
|
436 /** |
webmaster@1
|
437 * @defgroup locale-languages-negotiation Language negotiation options screen |
webmaster@1
|
438 * @{ |
webmaster@1
|
439 */ |
webmaster@1
|
440 |
webmaster@1
|
441 /** |
webmaster@1
|
442 * Setting for language negotiation options |
webmaster@1
|
443 */ |
webmaster@1
|
444 function locale_languages_configure_form() { |
webmaster@1
|
445 $form['language_negotiation'] = array( |
webmaster@1
|
446 '#title' => t('Language negotiation'), |
webmaster@1
|
447 '#type' => 'radios', |
webmaster@1
|
448 '#options' => array( |
webmaster@1
|
449 LANGUAGE_NEGOTIATION_NONE => t('None.'), |
webmaster@1
|
450 LANGUAGE_NEGOTIATION_PATH_DEFAULT => t('Path prefix only.'), |
webmaster@1
|
451 LANGUAGE_NEGOTIATION_PATH => t('Path prefix with language fallback.'), |
webmaster@1
|
452 LANGUAGE_NEGOTIATION_DOMAIN => t('Domain name only.')), |
webmaster@1
|
453 '#default_value' => variable_get('language_negotiation', LANGUAGE_NEGOTIATION_NONE), |
webmaster@1
|
454 '#description' => t("Select the mechanism used to determine your site's presentation language. <strong>Modifying this setting may break all incoming URLs and should be used with caution in a production environment.</strong>") |
webmaster@1
|
455 ); |
webmaster@1
|
456 $form['submit'] = array( |
webmaster@1
|
457 '#type' => 'submit', |
webmaster@1
|
458 '#value' => t('Save settings') |
webmaster@1
|
459 ); |
webmaster@1
|
460 return $form; |
webmaster@1
|
461 } |
webmaster@1
|
462 |
webmaster@1
|
463 /** |
webmaster@1
|
464 * Submit function for language negotiation settings. |
webmaster@1
|
465 */ |
webmaster@1
|
466 function locale_languages_configure_form_submit($form, &$form_state) { |
webmaster@1
|
467 variable_set('language_negotiation', $form_state['values']['language_negotiation']); |
webmaster@1
|
468 drupal_set_message(t('Language negotiation configuration saved.')); |
webmaster@1
|
469 $form_state['redirect'] = 'admin/settings/language'; |
webmaster@1
|
470 return; |
webmaster@1
|
471 } |
webmaster@1
|
472 /** |
webmaster@1
|
473 * @} End of "locale-languages-negotiation" |
webmaster@1
|
474 */ |
webmaster@1
|
475 |
webmaster@1
|
476 /** |
webmaster@1
|
477 * @defgroup locale-translate-overview Translation overview screen. |
webmaster@1
|
478 * @{ |
webmaster@1
|
479 */ |
webmaster@1
|
480 |
webmaster@1
|
481 /** |
webmaster@1
|
482 * Overview screen for translations. |
webmaster@1
|
483 */ |
webmaster@1
|
484 function locale_translate_overview_screen() { |
webmaster@1
|
485 $languages = language_list('language', TRUE); |
webmaster@1
|
486 $groups = module_invoke_all('locale', 'groups'); |
webmaster@1
|
487 |
webmaster@1
|
488 // Build headers with all groups in order. |
webmaster@1
|
489 $headers = array_merge(array(t('Language')), array_values($groups)); |
webmaster@1
|
490 |
webmaster@1
|
491 // Collect summaries of all source strings in all groups. |
webmaster@1
|
492 $sums = db_query("SELECT COUNT(*) AS strings, textgroup FROM {locales_source} GROUP BY textgroup"); |
webmaster@1
|
493 $groupsums = array(); |
webmaster@1
|
494 while ($group = db_fetch_object($sums)) { |
webmaster@1
|
495 $groupsums[$group->textgroup] = $group->strings; |
webmaster@1
|
496 } |
webmaster@1
|
497 |
webmaster@1
|
498 // Set up overview table with default values, ensuring common order for values. |
webmaster@1
|
499 $rows = array(); |
webmaster@1
|
500 foreach ($languages as $langcode => $language) { |
webmaster@1
|
501 $rows[$langcode] = array('name' => ($langcode == 'en' ? t('English (built-in)') : t($language->name))); |
webmaster@1
|
502 foreach ($groups as $group => $name) { |
webmaster@1
|
503 $rows[$langcode][$group] = ($langcode == 'en' ? t('n/a') : '0/'. (isset($groupsums[$group]) ? $groupsums[$group] : 0) .' (0%)'); |
webmaster@1
|
504 } |
webmaster@1
|
505 } |
webmaster@1
|
506 |
webmaster@1
|
507 // Languages with at least one record in the locale table. |
webmaster@1
|
508 $translations = db_query("SELECT COUNT(*) AS translation, t.language, s.textgroup FROM {locales_source} s INNER JOIN {locales_target} t ON s.lid = t.lid GROUP BY textgroup, language"); |
webmaster@1
|
509 while ($data = db_fetch_object($translations)) { |
webmaster@1
|
510 $ratio = (!empty($groupsums[$data->textgroup]) && $data->translation > 0) ? round(($data->translation/$groupsums[$data->textgroup])*100., 2) : 0; |
webmaster@1
|
511 $rows[$data->language][$data->textgroup] = $data->translation .'/'. $groupsums[$data->textgroup] ." ($ratio%)"; |
webmaster@1
|
512 } |
webmaster@1
|
513 |
webmaster@1
|
514 return theme('table', $headers, $rows); |
webmaster@1
|
515 } |
webmaster@1
|
516 /** |
webmaster@1
|
517 * @} End of "locale-translate-overview" |
webmaster@1
|
518 */ |
webmaster@1
|
519 |
webmaster@1
|
520 /** |
webmaster@1
|
521 * @defgroup locale-translate-seek Translation search screen. |
webmaster@1
|
522 * @{ |
webmaster@1
|
523 */ |
webmaster@1
|
524 |
webmaster@1
|
525 /** |
webmaster@1
|
526 * String search screen. |
webmaster@1
|
527 */ |
webmaster@1
|
528 function locale_translate_seek_screen() { |
webmaster@1
|
529 $output = _locale_translate_seek(); |
webmaster@1
|
530 $output .= drupal_get_form('locale_translate_seek_form'); |
webmaster@1
|
531 return $output; |
webmaster@1
|
532 } |
webmaster@1
|
533 |
webmaster@1
|
534 /** |
webmaster@1
|
535 * User interface for the string search screen. |
webmaster@1
|
536 */ |
webmaster@1
|
537 function locale_translate_seek_form() { |
webmaster@1
|
538 // Get all languages, except English |
webmaster@1
|
539 $languages = locale_language_list('name', TRUE); |
webmaster@1
|
540 unset($languages['en']); |
webmaster@1
|
541 |
webmaster@1
|
542 // Present edit form preserving previous user settings |
webmaster@1
|
543 $query = _locale_translate_seek_query(); |
webmaster@1
|
544 $form = array(); |
webmaster@1
|
545 $form['search'] = array('#type' => 'fieldset', |
webmaster@1
|
546 '#title' => t('Search'), |
webmaster@1
|
547 ); |
webmaster@1
|
548 $form['search']['string'] = array('#type' => 'textfield', |
webmaster@1
|
549 '#title' => t('String contains'), |
webmaster@1
|
550 '#default_value' => @$query['string'], |
webmaster@1
|
551 '#description' => t('Leave blank to show all strings. The search is case sensitive.'), |
webmaster@1
|
552 ); |
webmaster@1
|
553 $form['search']['language'] = array( |
webmaster@1
|
554 // Change type of form widget if more the 5 options will |
webmaster@1
|
555 // be present (2 of the options are added below). |
webmaster@1
|
556 '#type' => (count($languages) <= 3 ? 'radios' : 'select'), |
webmaster@1
|
557 '#title' => t('Language'), |
webmaster@1
|
558 '#default_value' => (!empty($query['language']) ? $query['language'] : 'all'), |
webmaster@1
|
559 '#options' => array_merge(array('all' => t('All languages'), 'en' => t('English (provided by Drupal)')), $languages), |
webmaster@1
|
560 ); |
webmaster@1
|
561 $form['search']['translation'] = array('#type' => 'radios', |
webmaster@1
|
562 '#title' => t('Search in'), |
webmaster@1
|
563 '#default_value' => (!empty($query['translation']) ? $query['translation'] : 'all'), |
webmaster@1
|
564 '#options' => array('all' => t('Both translated and untranslated strings'), 'translated' => t('Only translated strings'), 'untranslated' => t('Only untranslated strings')), |
webmaster@1
|
565 ); |
webmaster@1
|
566 $groups = module_invoke_all('locale', 'groups'); |
webmaster@1
|
567 $form['search']['group'] = array('#type' => 'radios', |
webmaster@1
|
568 '#title' => t('Limit search to'), |
webmaster@1
|
569 '#default_value' => (!empty($query['group']) ? $query['group'] : 'all'), |
webmaster@1
|
570 '#options' => array_merge(array('all' => t('All text groups')), $groups), |
webmaster@1
|
571 ); |
webmaster@1
|
572 |
webmaster@1
|
573 $form['search']['submit'] = array('#type' => 'submit', '#value' => t('Search')); |
webmaster@1
|
574 $form['#redirect'] = FALSE; |
webmaster@1
|
575 |
webmaster@1
|
576 return $form; |
webmaster@1
|
577 } |
webmaster@1
|
578 /** |
webmaster@1
|
579 * @} End of "locale-translate-seek" |
webmaster@1
|
580 */ |
webmaster@1
|
581 |
webmaster@1
|
582 /** |
webmaster@1
|
583 * @defgroup locale-translate-import Translation import screen. |
webmaster@1
|
584 * @{ |
webmaster@1
|
585 */ |
webmaster@1
|
586 |
webmaster@1
|
587 /** |
webmaster@1
|
588 * User interface for the translation import screen. |
webmaster@1
|
589 */ |
webmaster@1
|
590 function locale_translate_import_form() { |
webmaster@1
|
591 // Get all languages, except English |
webmaster@1
|
592 $names = locale_language_list('name', TRUE); |
webmaster@1
|
593 unset($names['en']); |
webmaster@1
|
594 |
webmaster@1
|
595 if (!count($names)) { |
webmaster@1
|
596 $languages = _locale_prepare_predefined_list(); |
webmaster@1
|
597 $default = array_shift(array_keys($languages)); |
webmaster@1
|
598 } |
webmaster@1
|
599 else { |
webmaster@1
|
600 $languages = array( |
webmaster@1
|
601 t('Already added languages') => $names, |
webmaster@1
|
602 t('Languages not yet added') => _locale_prepare_predefined_list() |
webmaster@1
|
603 ); |
webmaster@1
|
604 $default = array_shift(array_keys($names)); |
webmaster@1
|
605 } |
webmaster@1
|
606 |
webmaster@1
|
607 $form = array(); |
webmaster@1
|
608 $form['import'] = array('#type' => 'fieldset', |
webmaster@1
|
609 '#title' => t('Import translation'), |
webmaster@1
|
610 ); |
webmaster@1
|
611 $form['import']['file'] = array('#type' => 'file', |
webmaster@1
|
612 '#title' => t('Language file'), |
webmaster@1
|
613 '#size' => 50, |
webmaster@1
|
614 '#description' => t('A Gettext Portable Object (<em>.po</em>) file.'), |
webmaster@1
|
615 ); |
webmaster@1
|
616 $form['import']['langcode'] = array('#type' => 'select', |
webmaster@1
|
617 '#title' => t('Import into'), |
webmaster@1
|
618 '#options' => $languages, |
webmaster@1
|
619 '#default_value' => $default, |
webmaster@1
|
620 '#description' => t('Choose the language you want to add strings into. If you choose a language which is not yet set up, it will be added.'), |
webmaster@1
|
621 ); |
webmaster@1
|
622 $form['import']['group'] = array('#type' => 'radios', |
webmaster@1
|
623 '#title' => t('Text group'), |
webmaster@1
|
624 '#default_value' => 'default', |
webmaster@1
|
625 '#options' => module_invoke_all('locale', 'groups'), |
webmaster@1
|
626 '#description' => t('Imported translations will be added to this text group.'), |
webmaster@1
|
627 ); |
webmaster@1
|
628 $form['import']['mode'] = array('#type' => 'radios', |
webmaster@1
|
629 '#title' => t('Mode'), |
webmaster@1
|
630 '#default_value' => LOCALE_IMPORT_KEEP, |
webmaster@1
|
631 '#options' => array( |
webmaster@1
|
632 LOCALE_IMPORT_OVERWRITE => t('Strings in the uploaded file replace existing ones, new ones are added'), |
webmaster@1
|
633 LOCALE_IMPORT_KEEP => t('Existing strings are kept, only new strings are added') |
webmaster@1
|
634 ), |
webmaster@1
|
635 ); |
webmaster@1
|
636 $form['import']['submit'] = array('#type' => 'submit', '#value' => t('Import')); |
webmaster@1
|
637 $form['#attributes']['enctype'] = 'multipart/form-data'; |
webmaster@1
|
638 |
webmaster@1
|
639 return $form; |
webmaster@1
|
640 } |
webmaster@1
|
641 |
webmaster@1
|
642 /** |
webmaster@1
|
643 * Process the locale import form submission. |
webmaster@1
|
644 */ |
webmaster@1
|
645 function locale_translate_import_form_submit($form, &$form_state) { |
webmaster@1
|
646 // Ensure we have the file uploaded |
webmaster@1
|
647 if ($file = file_save_upload('file')) { |
webmaster@1
|
648 |
webmaster@1
|
649 // Add language, if not yet supported |
webmaster@1
|
650 $languages = language_list('language', TRUE); |
webmaster@1
|
651 $langcode = $form_state['values']['langcode']; |
webmaster@1
|
652 if (!isset($languages[$langcode])) { |
webmaster@1
|
653 $predefined = _locale_get_predefined_list(); |
webmaster@1
|
654 locale_add_language($langcode); |
webmaster@1
|
655 drupal_set_message(t('The language %language has been created.', array('%language' => t($predefined[$langcode][0])))); |
webmaster@1
|
656 } |
webmaster@1
|
657 |
webmaster@1
|
658 // Now import strings into the language |
webmaster@1
|
659 if ($ret = _locale_import_po($file, $langcode, $form_state['values']['mode'], $form_state['values']['group']) == FALSE) { |
webmaster@1
|
660 $variables = array('%filename' => $file->filename); |
webmaster@1
|
661 drupal_set_message(t('The translation import of %filename failed.', $variables), 'error'); |
webmaster@1
|
662 watchdog('locale', 'The translation import of %filename failed.', $variables, WATCHDOG_ERROR); |
webmaster@1
|
663 } |
webmaster@1
|
664 } |
webmaster@1
|
665 else { |
webmaster@1
|
666 drupal_set_message(t('File to import not found.'), 'error'); |
webmaster@1
|
667 return 'admin/build/translate/import'; |
webmaster@1
|
668 } |
webmaster@1
|
669 |
webmaster@1
|
670 $form_state['redirect'] = 'admin/build/translate'; |
webmaster@1
|
671 return; |
webmaster@1
|
672 } |
webmaster@1
|
673 /** |
webmaster@1
|
674 * @} End of "locale-translate-import" |
webmaster@1
|
675 */ |
webmaster@1
|
676 |
webmaster@1
|
677 /** |
webmaster@1
|
678 * @defgroup locale-translate-export Translation export screen. |
webmaster@1
|
679 * @{ |
webmaster@1
|
680 */ |
webmaster@1
|
681 |
webmaster@1
|
682 /** |
webmaster@1
|
683 * User interface for the translation export screen. |
webmaster@1
|
684 */ |
webmaster@1
|
685 function locale_translate_export_screen() { |
webmaster@1
|
686 // Get all languages, except English |
webmaster@1
|
687 $names = locale_language_list('name', TRUE); |
webmaster@1
|
688 unset($names['en']); |
webmaster@1
|
689 $output = ''; |
webmaster@1
|
690 // Offer translation export if any language is set up. |
webmaster@1
|
691 if (count($names)) { |
webmaster@1
|
692 $output = drupal_get_form('locale_translate_export_po_form', $names); |
webmaster@1
|
693 } |
webmaster@1
|
694 $output .= drupal_get_form('locale_translate_export_pot_form'); |
webmaster@1
|
695 return $output; |
webmaster@1
|
696 } |
webmaster@1
|
697 |
webmaster@1
|
698 /** |
webmaster@1
|
699 * Form to export PO files for the languages provided. |
webmaster@1
|
700 * |
webmaster@1
|
701 * @param $names |
webmaster@1
|
702 * An associate array with localized language names |
webmaster@1
|
703 */ |
webmaster@1
|
704 function locale_translate_export_po_form(&$form_state, $names) { |
webmaster@1
|
705 $form['export'] = array('#type' => 'fieldset', |
webmaster@1
|
706 '#title' => t('Export translation'), |
webmaster@1
|
707 '#collapsible' => TRUE, |
webmaster@1
|
708 ); |
webmaster@1
|
709 $form['export']['langcode'] = array('#type' => 'select', |
webmaster@1
|
710 '#title' => t('Language name'), |
webmaster@1
|
711 '#options' => $names, |
webmaster@1
|
712 '#description' => t('Select the language to export in Gettext Portable Object (<em>.po</em>) format.'), |
webmaster@1
|
713 ); |
webmaster@1
|
714 $form['export']['group'] = array('#type' => 'radios', |
webmaster@1
|
715 '#title' => t('Text group'), |
webmaster@1
|
716 '#default_value' => 'default', |
webmaster@1
|
717 '#options' => module_invoke_all('locale', 'groups'), |
webmaster@1
|
718 ); |
webmaster@1
|
719 $form['export']['submit'] = array('#type' => 'submit', '#value' => t('Export')); |
webmaster@1
|
720 return $form; |
webmaster@1
|
721 } |
webmaster@1
|
722 |
webmaster@1
|
723 /** |
webmaster@1
|
724 * Translation template export form. |
webmaster@1
|
725 */ |
webmaster@1
|
726 function locale_translate_export_pot_form() { |
webmaster@1
|
727 // Complete template export of the strings |
webmaster@1
|
728 $form['export'] = array('#type' => 'fieldset', |
webmaster@1
|
729 '#title' => t('Export template'), |
webmaster@1
|
730 '#collapsible' => TRUE, |
webmaster@1
|
731 '#description' => t('Generate a Gettext Portable Object Template (<em>.pot</em>) file with all strings from the Drupal locale database.'), |
webmaster@1
|
732 ); |
webmaster@1
|
733 $form['export']['group'] = array('#type' => 'radios', |
webmaster@1
|
734 '#title' => t('Text group'), |
webmaster@1
|
735 '#default_value' => 'default', |
webmaster@1
|
736 '#options' => module_invoke_all('locale', 'groups'), |
webmaster@1
|
737 ); |
webmaster@1
|
738 $form['export']['submit'] = array('#type' => 'submit', '#value' => t('Export')); |
webmaster@1
|
739 // Reuse PO export submission callback. |
webmaster@1
|
740 $form['#submit'][] = 'locale_translate_export_po_form_submit'; |
webmaster@1
|
741 $form['#validate'][] = 'locale_translate_export_po_form_validate'; |
webmaster@1
|
742 return $form; |
webmaster@1
|
743 } |
webmaster@1
|
744 |
webmaster@1
|
745 /** |
webmaster@1
|
746 * Process a translation (or template) export form submission. |
webmaster@1
|
747 */ |
webmaster@1
|
748 function locale_translate_export_po_form_submit($form, &$form_state) { |
webmaster@1
|
749 // If template is required, language code is not given. |
webmaster@1
|
750 $language = NULL; |
webmaster@1
|
751 if (isset($form_state['values']['langcode'])) { |
webmaster@1
|
752 $languages = language_list(); |
webmaster@1
|
753 $language = $languages[$form_state['values']['langcode']]; |
webmaster@1
|
754 } |
webmaster@1
|
755 _locale_export_po($language, _locale_export_po_generate($language, _locale_export_get_strings($language, $form_state['values']['group']))); |
webmaster@1
|
756 } |
webmaster@1
|
757 /** |
webmaster@1
|
758 * @} End of "locale-translate-export" |
webmaster@1
|
759 */ |
webmaster@1
|
760 |
webmaster@1
|
761 /** |
webmaster@1
|
762 * @defgroup locale-translate-edit Translation text editing |
webmaster@1
|
763 * @{ |
webmaster@1
|
764 */ |
webmaster@1
|
765 |
webmaster@1
|
766 /** |
webmaster@1
|
767 * User interface for string editing. |
webmaster@1
|
768 */ |
webmaster@1
|
769 function locale_translate_edit_form(&$form_state, $lid) { |
webmaster@1
|
770 // Fetch source string, if possible. |
webmaster@1
|
771 $source = db_fetch_object(db_query('SELECT source, textgroup, location FROM {locales_source} WHERE lid = %d', $lid)); |
webmaster@1
|
772 if (!$source) { |
webmaster@1
|
773 drupal_set_message(t('String not found.'), 'error'); |
webmaster@1
|
774 drupal_goto('admin/build/translate/search'); |
webmaster@1
|
775 } |
webmaster@1
|
776 |
webmaster@1
|
777 // Add original text to the top and some values for form altering. |
webmaster@1
|
778 $form = array( |
webmaster@1
|
779 'original' => array( |
webmaster@1
|
780 '#type' => 'item', |
webmaster@1
|
781 '#title' => t('Original text'), |
webmaster@1
|
782 '#value' => check_plain(wordwrap($source->source, 0)), |
webmaster@1
|
783 ), |
webmaster@1
|
784 'lid' => array( |
webmaster@1
|
785 '#type' => 'value', |
webmaster@1
|
786 '#value' => $lid |
webmaster@1
|
787 ), |
webmaster@1
|
788 'textgroup' => array( |
webmaster@1
|
789 '#type' => 'value', |
webmaster@1
|
790 '#value' => $source->textgroup, |
webmaster@1
|
791 ), |
webmaster@1
|
792 'location' => array( |
webmaster@1
|
793 '#type' => 'value', |
webmaster@1
|
794 '#value' => $source->location |
webmaster@1
|
795 ), |
webmaster@1
|
796 ); |
webmaster@1
|
797 |
webmaster@1
|
798 // Include default form controls with empty values for all languages. |
webmaster@1
|
799 // This ensures that the languages are always in the same order in forms. |
webmaster@1
|
800 $languages = language_list(); |
webmaster@1
|
801 $default = language_default(); |
webmaster@1
|
802 // We don't need the default language value, that value is in $source. |
webmaster@1
|
803 $omit = $source->textgroup == 'default' ? 'en' : $default->language; |
webmaster@1
|
804 unset($languages[($omit)]); |
webmaster@1
|
805 $form['translations'] = array('#tree' => TRUE); |
webmaster@1
|
806 // Approximate the number of rows to use in the default textarea. |
webmaster@1
|
807 $rows = min(ceil(str_word_count($source->source) / 12), 10); |
webmaster@1
|
808 foreach ($languages as $langcode => $language) { |
webmaster@1
|
809 $form['translations'][$langcode] = array( |
webmaster@1
|
810 '#type' => 'textarea', |
webmaster@1
|
811 '#title' => t($language->name), |
webmaster@1
|
812 '#rows' => $rows, |
webmaster@1
|
813 '#default_value' => '', |
webmaster@1
|
814 ); |
webmaster@1
|
815 } |
webmaster@1
|
816 |
webmaster@1
|
817 // Fetch translations and fill in default values in the form. |
webmaster@1
|
818 $result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = %d AND language != '%s'", $lid, $omit); |
webmaster@1
|
819 while ($translation = db_fetch_object($result)) { |
webmaster@1
|
820 $form['translations'][$translation->language]['#default_value'] = $translation->translation; |
webmaster@1
|
821 } |
webmaster@1
|
822 |
webmaster@1
|
823 $form['submit'] = array('#type' => 'submit', '#value' => t('Save translations')); |
webmaster@1
|
824 return $form; |
webmaster@1
|
825 } |
webmaster@1
|
826 |
webmaster@1
|
827 /** |
webmaster@1
|
828 * Process string editing form submissions. |
webmaster@1
|
829 * Saves all translations of one string submitted from a form. |
webmaster@1
|
830 */ |
webmaster@1
|
831 function locale_translate_edit_form_submit($form, &$form_state) { |
webmaster@1
|
832 $lid = $form_state['values']['lid']; |
webmaster@1
|
833 foreach ($form_state['values']['translations'] as $key => $value) { |
webmaster@1
|
834 $translation = db_result(db_query("SELECT translation FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key)); |
webmaster@1
|
835 if (!empty($value)) { |
webmaster@1
|
836 // Only update or insert if we have a value to use. |
webmaster@1
|
837 if (!empty($translation)) { |
webmaster@1
|
838 db_query("UPDATE {locales_target} SET translation = '%s' WHERE lid = %d AND language = '%s'", $value, $lid, $key); |
webmaster@1
|
839 } |
webmaster@1
|
840 else { |
webmaster@1
|
841 db_query("INSERT INTO {locales_target} (lid, translation, language) VALUES (%d, '%s', '%s')", $lid, $value, $key); |
webmaster@1
|
842 } |
webmaster@1
|
843 } |
webmaster@1
|
844 elseif (!empty($translation)) { |
webmaster@1
|
845 // Empty translation entered: remove existing entry from database. |
webmaster@1
|
846 db_query("DELETE FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $key); |
webmaster@1
|
847 } |
webmaster@1
|
848 |
webmaster@1
|
849 // Force JavaScript translation file recreation for this language. |
webmaster@1
|
850 _locale_invalidate_js($key); |
webmaster@1
|
851 } |
webmaster@1
|
852 |
webmaster@1
|
853 drupal_set_message(t('The string has been saved.')); |
webmaster@1
|
854 |
webmaster@1
|
855 // Clear locale cache. |
webmaster@1
|
856 cache_clear_all('locale:', 'cache', TRUE); |
webmaster@1
|
857 |
webmaster@1
|
858 $form_state['redirect'] = 'admin/build/translate/search'; |
webmaster@1
|
859 return; |
webmaster@1
|
860 } |
webmaster@1
|
861 /** |
webmaster@1
|
862 * @} End of "locale-translate-edit" |
webmaster@1
|
863 */ |
webmaster@1
|
864 |
webmaster@1
|
865 /** |
webmaster@1
|
866 * @defgroup locale-translate-delete Translation delete interface. |
webmaster@1
|
867 * @{ |
webmaster@1
|
868 */ |
webmaster@1
|
869 |
webmaster@1
|
870 /** |
webmaster@1
|
871 * Delete a language string. |
webmaster@1
|
872 */ |
webmaster@1
|
873 function locale_translate_delete($lid) { |
webmaster@1
|
874 db_query('DELETE FROM {locales_source} WHERE lid = %d', $lid); |
webmaster@1
|
875 db_query('DELETE FROM {locales_target} WHERE lid = %d', $lid); |
webmaster@1
|
876 // Force JavaScript translation file recreation for all languages. |
webmaster@1
|
877 _locale_invalidate_js(); |
webmaster@1
|
878 cache_clear_all('locale:', 'cache', TRUE); |
webmaster@1
|
879 drupal_set_message(t('The string has been removed.')); |
webmaster@1
|
880 drupal_goto('admin/build/translate/search'); |
webmaster@1
|
881 } |
webmaster@1
|
882 /** |
webmaster@1
|
883 * @} End of "locale-translate-delete" |
webmaster@1
|
884 */ |
webmaster@1
|
885 |
webmaster@1
|
886 /** |
webmaster@1
|
887 * @defgroup locale-api-add Language addition API. |
webmaster@1
|
888 * @{ |
webmaster@1
|
889 */ |
webmaster@1
|
890 |
webmaster@1
|
891 /** |
webmaster@1
|
892 * API function to add a language. |
webmaster@1
|
893 * |
webmaster@1
|
894 * @param $langcode |
webmaster@1
|
895 * Language code. |
webmaster@1
|
896 * @param $name |
webmaster@1
|
897 * English name of the language |
webmaster@1
|
898 * @param $native |
webmaster@1
|
899 * Native name of the language |
webmaster@1
|
900 * @param $direction |
webmaster@1
|
901 * LANGUAGE_LTR or LANGUAGE_RTL |
webmaster@1
|
902 * @param $domain |
webmaster@1
|
903 * Optional custom domain name with protocol, without |
webmaster@1
|
904 * trailing slash (eg. http://de.example.com). |
webmaster@1
|
905 * @param $prefix |
webmaster@1
|
906 * Optional path prefix for the language. Defaults to the |
webmaster@1
|
907 * language code if omitted. |
webmaster@1
|
908 * @param $enabled |
webmaster@1
|
909 * Optionally TRUE to enable the language when created or FALSE to disable. |
webmaster@1
|
910 * @param $default |
webmaster@1
|
911 * Optionally set this language to be the default. |
webmaster@1
|
912 */ |
webmaster@1
|
913 function locale_add_language($langcode, $name = NULL, $native = NULL, $direction = LANGUAGE_LTR, $domain = '', $prefix = '', $enabled = TRUE, $default = FALSE) { |
webmaster@1
|
914 // Default prefix on language code. |
webmaster@1
|
915 if (empty($prefix)) { |
webmaster@1
|
916 $prefix = $langcode; |
webmaster@1
|
917 } |
webmaster@1
|
918 |
webmaster@1
|
919 // If name was not set, we add a predefined language. |
webmaster@1
|
920 if (!isset($name)) { |
webmaster@1
|
921 $predefined = _locale_get_predefined_list(); |
webmaster@1
|
922 $name = $predefined[$langcode][0]; |
webmaster@1
|
923 $native = isset($predefined[$langcode][1]) ? $predefined[$langcode][1] : $predefined[$langcode][0]; |
webmaster@1
|
924 $direction = isset($predefined[$langcode][2]) ? $predefined[$langcode][2] : LANGUAGE_LTR; |
webmaster@1
|
925 } |
webmaster@1
|
926 |
webmaster@1
|
927 db_query("INSERT INTO {languages} (language, name, native, direction, domain, prefix, enabled) VALUES ('%s', '%s', '%s', %d, '%s', '%s', %d)", $langcode, $name, $native, $direction, $domain, $prefix, $enabled); |
webmaster@1
|
928 |
webmaster@1
|
929 // Only set it as default if enabled. |
webmaster@1
|
930 if ($enabled && $default) { |
webmaster@1
|
931 variable_set('language_default', (object) array('language' => $langcode, 'name' => $name, 'native' => $native, 'direction' => $direction, 'enabled' => (int) $enabled, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => $prefix, 'weight' => 0, 'javascript' => '')); |
webmaster@1
|
932 } |
webmaster@1
|
933 |
webmaster@1
|
934 if ($enabled) { |
webmaster@1
|
935 // Increment enabled language count if we are adding an enabled language. |
webmaster@1
|
936 variable_set('language_count', variable_get('language_count', 1) + 1); |
webmaster@1
|
937 } |
webmaster@1
|
938 |
webmaster@1
|
939 // Force JavaScript translation file creation for the newly added language. |
webmaster@1
|
940 _locale_invalidate_js($langcode); |
webmaster@1
|
941 |
webmaster@1
|
942 watchdog('locale', 'The %language language (%code) has been created.', array('%language' => $name, '%code' => $langcode)); |
webmaster@1
|
943 } |
webmaster@1
|
944 /** |
webmaster@1
|
945 * @} End of "locale-api-add" |
webmaster@1
|
946 */ |
webmaster@1
|
947 |
webmaster@1
|
948 /** |
webmaster@1
|
949 * @defgroup locale-api-import Translation import API. |
webmaster@1
|
950 * @{ |
webmaster@1
|
951 */ |
webmaster@1
|
952 |
webmaster@1
|
953 /** |
webmaster@1
|
954 * Parses Gettext Portable Object file information and inserts into database |
webmaster@1
|
955 * |
webmaster@1
|
956 * @param $file |
webmaster@1
|
957 * Drupal file object corresponding to the PO file to import |
webmaster@1
|
958 * @param $langcode |
webmaster@1
|
959 * Language code |
webmaster@1
|
960 * @param $mode |
webmaster@1
|
961 * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE |
webmaster@1
|
962 * @param $group |
webmaster@1
|
963 * Text group to import PO file into (eg. 'default' for interface translations) |
webmaster@1
|
964 */ |
webmaster@1
|
965 function _locale_import_po($file, $langcode, $mode, $group = NULL) { |
webmaster@1
|
966 // If not in 'safe mode', increase the maximum execution time. |
webmaster@1
|
967 if (!ini_get('safe_mode')) { |
webmaster@1
|
968 set_time_limit(240); |
webmaster@1
|
969 } |
webmaster@1
|
970 |
webmaster@1
|
971 // Check if we have the language already in the database. |
webmaster@1
|
972 if (!db_fetch_object(db_query("SELECT language FROM {languages} WHERE language = '%s'", $langcode))) { |
webmaster@1
|
973 drupal_set_message(t('The language selected for import is not supported.'), 'error'); |
webmaster@1
|
974 return FALSE; |
webmaster@1
|
975 } |
webmaster@1
|
976 |
webmaster@1
|
977 // Get strings from file (returns on failure after a partial import, or on success) |
webmaster@1
|
978 $status = _locale_import_read_po('db-store', $file, $mode, $langcode, $group); |
webmaster@1
|
979 if ($status === FALSE) { |
webmaster@1
|
980 // Error messages are set in _locale_import_read_po(). |
webmaster@1
|
981 return FALSE; |
webmaster@1
|
982 } |
webmaster@1
|
983 |
webmaster@1
|
984 // Get status information on import process. |
webmaster@1
|
985 list($headerdone, $additions, $updates, $deletes) = _locale_import_one_string('db-report'); |
webmaster@1
|
986 |
webmaster@1
|
987 if (!$headerdone) { |
webmaster@1
|
988 drupal_set_message(t('The translation file %filename appears to have a missing or malformed header.', array('%filename' => $file->filename)), 'error'); |
webmaster@1
|
989 } |
webmaster@1
|
990 |
webmaster@1
|
991 // Clear cache and force refresh of JavaScript translations. |
webmaster@1
|
992 _locale_invalidate_js($langcode); |
webmaster@1
|
993 cache_clear_all('locale:', 'cache', TRUE); |
webmaster@1
|
994 |
webmaster@1
|
995 // Rebuild the menu, strings may have changed. |
webmaster@1
|
996 menu_rebuild(); |
webmaster@1
|
997 |
webmaster@1
|
998 drupal_set_message(t('The translation was successfully imported. There are %number newly created translated strings, %update strings were updated and %delete strings were removed.', array('%number' => $additions, '%update' => $updates, '%delete' => $deletes))); |
webmaster@1
|
999 watchdog('locale', 'Imported %file into %locale: %number new strings added, %update updated and %delete removed.', array('%file' => $file->filename, '%locale' => $langcode, '%number' => $additions, '%update' => $updates, '%delete' => $deletes)); |
webmaster@1
|
1000 return TRUE; |
webmaster@1
|
1001 } |
webmaster@1
|
1002 |
webmaster@1
|
1003 /** |
webmaster@1
|
1004 * Parses Gettext Portable Object file into an array |
webmaster@1
|
1005 * |
webmaster@1
|
1006 * @param $op |
webmaster@1
|
1007 * Storage operation type: db-store or mem-store |
webmaster@1
|
1008 * @param $file |
webmaster@1
|
1009 * Drupal file object corresponding to the PO file to import |
webmaster@1
|
1010 * @param $mode |
webmaster@1
|
1011 * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE |
webmaster@1
|
1012 * @param $lang |
webmaster@1
|
1013 * Language code |
webmaster@1
|
1014 * @param $group |
webmaster@1
|
1015 * Text group to import PO file into (eg. 'default' for interface translations) |
webmaster@1
|
1016 */ |
webmaster@1
|
1017 function _locale_import_read_po($op, $file, $mode = NULL, $lang = NULL, $group = 'default') { |
webmaster@1
|
1018 |
webmaster@1
|
1019 $fd = fopen($file->filepath, "rb"); // File will get closed by PHP on return |
webmaster@1
|
1020 if (!$fd) { |
webmaster@1
|
1021 _locale_import_message('The translation import failed, because the file %filename could not be read.', $file); |
webmaster@1
|
1022 return FALSE; |
webmaster@1
|
1023 } |
webmaster@1
|
1024 |
webmaster@1
|
1025 $context = "COMMENT"; // Parser context: COMMENT, MSGID, MSGID_PLURAL, MSGSTR and MSGSTR_ARR |
webmaster@1
|
1026 $current = array(); // Current entry being read |
webmaster@1
|
1027 $plural = 0; // Current plural form |
webmaster@1
|
1028 $lineno = 0; // Current line |
webmaster@1
|
1029 |
webmaster@1
|
1030 while (!feof($fd)) { |
webmaster@1
|
1031 $line = fgets($fd, 10*1024); // A line should not be this long |
webmaster@1
|
1032 if ($lineno == 0) { |
webmaster@1
|
1033 // The first line might come with a UTF-8 BOM, which should be removed. |
webmaster@1
|
1034 $line = str_replace("\xEF\xBB\xBF", '', $line); |
webmaster@1
|
1035 } |
webmaster@1
|
1036 $lineno++; |
webmaster@1
|
1037 $line = trim(strtr($line, array("\\\n" => ""))); |
webmaster@1
|
1038 |
webmaster@1
|
1039 if (!strncmp("#", $line, 1)) { // A comment |
webmaster@1
|
1040 if ($context == "COMMENT") { // Already in comment context: add |
webmaster@1
|
1041 $current["#"][] = substr($line, 1); |
webmaster@1
|
1042 } |
webmaster@1
|
1043 elseif (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { // End current entry, start a new one |
webmaster@1
|
1044 _locale_import_one_string($op, $current, $mode, $lang, $file, $group); |
webmaster@1
|
1045 $current = array(); |
webmaster@1
|
1046 $current["#"][] = substr($line, 1); |
webmaster@1
|
1047 $context = "COMMENT"; |
webmaster@1
|
1048 } |
webmaster@1
|
1049 else { // Parse error |
webmaster@1
|
1050 _locale_import_message('The translation file %filename contains an error: "msgstr" was expected but not found on line %line.', $file, $lineno); |
webmaster@1
|
1051 return FALSE; |
webmaster@1
|
1052 } |
webmaster@1
|
1053 } |
webmaster@1
|
1054 elseif (!strncmp("msgid_plural", $line, 12)) { |
webmaster@1
|
1055 if ($context != "MSGID") { // Must be plural form for current entry |
webmaster@1
|
1056 _locale_import_message('The translation file %filename contains an error: "msgid_plural" was expected but not found on line %line.', $file, $lineno); |
webmaster@1
|
1057 return FALSE; |
webmaster@1
|
1058 } |
webmaster@1
|
1059 $line = trim(substr($line, 12)); |
webmaster@1
|
1060 $quoted = _locale_import_parse_quoted($line); |
webmaster@1
|
1061 if ($quoted === FALSE) { |
webmaster@1
|
1062 _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); |
webmaster@1
|
1063 return FALSE; |
webmaster@1
|
1064 } |
webmaster@1
|
1065 $current["msgid"] = $current["msgid"] ."\0". $quoted; |
webmaster@1
|
1066 $context = "MSGID_PLURAL"; |
webmaster@1
|
1067 } |
webmaster@1
|
1068 elseif (!strncmp("msgid", $line, 5)) { |
webmaster@1
|
1069 if ($context == "MSGSTR") { // End current entry, start a new one |
webmaster@1
|
1070 _locale_import_one_string($op, $current, $mode, $lang, $file, $group); |
webmaster@1
|
1071 $current = array(); |
webmaster@1
|
1072 } |
webmaster@1
|
1073 elseif ($context == "MSGID") { // Already in this context? Parse error |
webmaster@1
|
1074 _locale_import_message('The translation file %filename contains an error: "msgid" is unexpected on line %line.', $file, $lineno); |
webmaster@1
|
1075 return FALSE; |
webmaster@1
|
1076 } |
webmaster@1
|
1077 $line = trim(substr($line, 5)); |
webmaster@1
|
1078 $quoted = _locale_import_parse_quoted($line); |
webmaster@1
|
1079 if ($quoted === FALSE) { |
webmaster@1
|
1080 _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); |
webmaster@1
|
1081 return FALSE; |
webmaster@1
|
1082 } |
webmaster@1
|
1083 $current["msgid"] = $quoted; |
webmaster@1
|
1084 $context = "MSGID"; |
webmaster@1
|
1085 } |
webmaster@1
|
1086 elseif (!strncmp("msgstr[", $line, 7)) { |
webmaster@1
|
1087 if (($context != "MSGID") && ($context != "MSGID_PLURAL") && ($context != "MSGSTR_ARR")) { // Must come after msgid, msgid_plural, or msgstr[] |
webmaster@1
|
1088 _locale_import_message('The translation file %filename contains an error: "msgstr[]" is unexpected on line %line.', $file, $lineno); |
webmaster@1
|
1089 return FALSE; |
webmaster@1
|
1090 } |
webmaster@1
|
1091 if (strpos($line, "]") === FALSE) { |
webmaster@1
|
1092 _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); |
webmaster@1
|
1093 return FALSE; |
webmaster@1
|
1094 } |
webmaster@1
|
1095 $frombracket = strstr($line, "["); |
webmaster@1
|
1096 $plural = substr($frombracket, 1, strpos($frombracket, "]") - 1); |
webmaster@1
|
1097 $line = trim(strstr($line, " ")); |
webmaster@1
|
1098 $quoted = _locale_import_parse_quoted($line); |
webmaster@1
|
1099 if ($quoted === FALSE) { |
webmaster@1
|
1100 _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); |
webmaster@1
|
1101 return FALSE; |
webmaster@1
|
1102 } |
webmaster@1
|
1103 $current["msgstr"][$plural] = $quoted; |
webmaster@1
|
1104 $context = "MSGSTR_ARR"; |
webmaster@1
|
1105 } |
webmaster@1
|
1106 elseif (!strncmp("msgstr", $line, 6)) { |
webmaster@1
|
1107 if ($context != "MSGID") { // Should come just after a msgid block |
webmaster@1
|
1108 _locale_import_message('The translation file %filename contains an error: "msgstr" is unexpected on line %line.', $file, $lineno); |
webmaster@1
|
1109 return FALSE; |
webmaster@1
|
1110 } |
webmaster@1
|
1111 $line = trim(substr($line, 6)); |
webmaster@1
|
1112 $quoted = _locale_import_parse_quoted($line); |
webmaster@1
|
1113 if ($quoted === FALSE) { |
webmaster@1
|
1114 _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); |
webmaster@1
|
1115 return FALSE; |
webmaster@1
|
1116 } |
webmaster@1
|
1117 $current["msgstr"] = $quoted; |
webmaster@1
|
1118 $context = "MSGSTR"; |
webmaster@1
|
1119 } |
webmaster@1
|
1120 elseif ($line != "") { |
webmaster@1
|
1121 $quoted = _locale_import_parse_quoted($line); |
webmaster@1
|
1122 if ($quoted === FALSE) { |
webmaster@1
|
1123 _locale_import_message('The translation file %filename contains a syntax error on line %line.', $file, $lineno); |
webmaster@1
|
1124 return FALSE; |
webmaster@1
|
1125 } |
webmaster@1
|
1126 if (($context == "MSGID") || ($context == "MSGID_PLURAL")) { |
webmaster@1
|
1127 $current["msgid"] .= $quoted; |
webmaster@1
|
1128 } |
webmaster@1
|
1129 elseif ($context == "MSGSTR") { |
webmaster@1
|
1130 $current["msgstr"] .= $quoted; |
webmaster@1
|
1131 } |
webmaster@1
|
1132 elseif ($context == "MSGSTR_ARR") { |
webmaster@1
|
1133 $current["msgstr"][$plural] .= $quoted; |
webmaster@1
|
1134 } |
webmaster@1
|
1135 else { |
webmaster@1
|
1136 _locale_import_message('The translation file %filename contains an error: there is an unexpected string on line %line.', $file, $lineno); |
webmaster@1
|
1137 return FALSE; |
webmaster@1
|
1138 } |
webmaster@1
|
1139 } |
webmaster@1
|
1140 } |
webmaster@1
|
1141 |
webmaster@1
|
1142 // End of PO file, flush last entry |
webmaster@1
|
1143 if (($context == "MSGSTR") || ($context == "MSGSTR_ARR")) { |
webmaster@1
|
1144 _locale_import_one_string($op, $current, $mode, $lang, $file, $group); |
webmaster@1
|
1145 } |
webmaster@1
|
1146 elseif ($context != "COMMENT") { |
webmaster@1
|
1147 _locale_import_message('The translation file %filename ended unexpectedly at line %line.', $file, $lineno); |
webmaster@1
|
1148 return FALSE; |
webmaster@1
|
1149 } |
webmaster@1
|
1150 |
webmaster@1
|
1151 } |
webmaster@1
|
1152 |
webmaster@1
|
1153 /** |
webmaster@1
|
1154 * Sets an error message occurred during locale file parsing. |
webmaster@1
|
1155 * |
webmaster@1
|
1156 * @param $message |
webmaster@1
|
1157 * The message to be translated |
webmaster@1
|
1158 * @param $file |
webmaster@1
|
1159 * Drupal file object corresponding to the PO file to import |
webmaster@1
|
1160 * @param $lineno |
webmaster@1
|
1161 * An optional line number argument |
webmaster@1
|
1162 */ |
webmaster@1
|
1163 function _locale_import_message($message, $file, $lineno = NULL) { |
webmaster@1
|
1164 $vars = array('%filename' => $file->filename); |
webmaster@1
|
1165 if (isset($lineno)) { |
webmaster@1
|
1166 $vars['%line'] = $lineno; |
webmaster@1
|
1167 } |
webmaster@1
|
1168 $t = get_t(); |
webmaster@1
|
1169 drupal_set_message($t($message, $vars), 'error'); |
webmaster@1
|
1170 } |
webmaster@1
|
1171 |
webmaster@1
|
1172 /** |
webmaster@1
|
1173 * Imports a string into the database |
webmaster@1
|
1174 * |
webmaster@1
|
1175 * @param $op |
webmaster@1
|
1176 * Operation to perform: 'db-store', 'db-report', 'mem-store' or 'mem-report' |
webmaster@1
|
1177 * @param $value |
webmaster@1
|
1178 * Details of the string stored |
webmaster@1
|
1179 * @param $mode |
webmaster@1
|
1180 * Should existing translations be replaced LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE |
webmaster@1
|
1181 * @param $lang |
webmaster@1
|
1182 * Language to store the string in |
webmaster@1
|
1183 * @param $file |
webmaster@1
|
1184 * Object representation of file being imported, only required when op is 'db-store' |
webmaster@1
|
1185 * @param $group |
webmaster@1
|
1186 * Text group to import PO file into (eg. 'default' for interface translations) |
webmaster@1
|
1187 */ |
webmaster@1
|
1188 function _locale_import_one_string($op, $value = NULL, $mode = NULL, $lang = NULL, $file = NULL, $group = 'default') { |
webmaster@1
|
1189 static $report = array(0, 0, 0); |
webmaster@1
|
1190 static $headerdone = FALSE; |
webmaster@1
|
1191 static $strings = array(); |
webmaster@1
|
1192 |
webmaster@1
|
1193 switch ($op) { |
webmaster@1
|
1194 // Return stored strings |
webmaster@1
|
1195 case 'mem-report': |
webmaster@1
|
1196 return $strings; |
webmaster@1
|
1197 |
webmaster@1
|
1198 // Store string in memory (only supports single strings) |
webmaster@1
|
1199 case 'mem-store': |
webmaster@1
|
1200 $strings[$value['msgid']] = $value['msgstr']; |
webmaster@1
|
1201 return; |
webmaster@1
|
1202 |
webmaster@1
|
1203 // Called at end of import to inform the user |
webmaster@1
|
1204 case 'db-report': |
webmaster@1
|
1205 return array($headerdone, $report[0], $report[1], $report[2]); |
webmaster@1
|
1206 |
webmaster@1
|
1207 // Store the string we got in the database. |
webmaster@1
|
1208 case 'db-store': |
webmaster@1
|
1209 // We got header information. |
webmaster@1
|
1210 if ($value['msgid'] == '') { |
webmaster@1
|
1211 $header = _locale_import_parse_header($value['msgstr']); |
webmaster@1
|
1212 |
webmaster@1
|
1213 // Get the plural formula and update in database. |
webmaster@1
|
1214 if (isset($header["Plural-Forms"]) && $p = _locale_import_parse_plural_forms($header["Plural-Forms"], $file->filename)) { |
webmaster@1
|
1215 list($nplurals, $plural) = $p; |
webmaster@1
|
1216 db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", $nplurals, $plural, $lang); |
webmaster@1
|
1217 } |
webmaster@1
|
1218 else { |
webmaster@1
|
1219 db_query("UPDATE {languages} SET plurals = %d, formula = '%s' WHERE language = '%s'", 0, '', $lang); |
webmaster@1
|
1220 } |
webmaster@1
|
1221 $headerdone = TRUE; |
webmaster@1
|
1222 } |
webmaster@1
|
1223 |
webmaster@1
|
1224 else { |
webmaster@1
|
1225 // Some real string to import. |
webmaster@1
|
1226 $comments = _locale_import_shorten_comments(empty($value['#']) ? array() : $value['#']); |
webmaster@1
|
1227 |
webmaster@1
|
1228 if (strpos($value['msgid'], "\0")) { |
webmaster@1
|
1229 // This string has plural versions. |
webmaster@1
|
1230 $english = explode("\0", $value['msgid'], 2); |
webmaster@1
|
1231 $entries = array_keys($value['msgstr']); |
webmaster@1
|
1232 for ($i = 3; $i <= count($entries); $i++) { |
webmaster@1
|
1233 $english[] = $english[1]; |
webmaster@1
|
1234 } |
webmaster@1
|
1235 $translation = array_map('_locale_import_append_plural', $value['msgstr'], $entries); |
webmaster@1
|
1236 $english = array_map('_locale_import_append_plural', $english, $entries); |
webmaster@1
|
1237 foreach ($translation as $key => $trans) { |
webmaster@1
|
1238 if ($key == 0) { |
webmaster@1
|
1239 $plid = 0; |
webmaster@1
|
1240 } |
webmaster@1
|
1241 $plid = _locale_import_one_string_db($report, $lang, $english[$key], $trans, $group, $comments, $mode, $plid, $key); |
webmaster@1
|
1242 } |
webmaster@1
|
1243 } |
webmaster@1
|
1244 |
webmaster@1
|
1245 else { |
webmaster@1
|
1246 // A simple string to import. |
webmaster@1
|
1247 $english = $value['msgid']; |
webmaster@1
|
1248 $translation = $value['msgstr']; |
webmaster@1
|
1249 _locale_import_one_string_db($report, $lang, $english, $translation, $group, $comments, $mode); |
webmaster@1
|
1250 } |
webmaster@1
|
1251 } |
webmaster@1
|
1252 } // end of db-store operation |
webmaster@1
|
1253 } |
webmaster@1
|
1254 |
webmaster@1
|
1255 /** |
webmaster@1
|
1256 * Import one string into the database. |
webmaster@1
|
1257 * |
webmaster@1
|
1258 * @param $report |
webmaster@1
|
1259 * Report array summarizing the number of changes done in the form: |
webmaster@1
|
1260 * array(inserts, updates, deletes). |
webmaster@1
|
1261 * @param $langcode |
webmaster@1
|
1262 * Language code to import string into. |
webmaster@1
|
1263 * @param $source |
webmaster@1
|
1264 * Source string. |
webmaster@1
|
1265 * @param $translation |
webmaster@1
|
1266 * Translation to language specified in $langcode. |
webmaster@1
|
1267 * @param $textgroup |
webmaster@1
|
1268 * Name of textgroup to store translation in. |
webmaster@1
|
1269 * @param $location |
webmaster@1
|
1270 * Location value to save with source string. |
webmaster@1
|
1271 * @param $mode |
webmaster@1
|
1272 * Import mode to use, LOCALE_IMPORT_KEEP or LOCALE_IMPORT_OVERWRITE. |
webmaster@1
|
1273 * @param $plid |
webmaster@1
|
1274 * Optional plural ID to use. |
webmaster@1
|
1275 * @param $plural |
webmaster@1
|
1276 * Optional plural value to use. |
webmaster@1
|
1277 * @return |
webmaster@1
|
1278 * The string ID of the existing string modified or the new string added. |
webmaster@1
|
1279 */ |
webmaster@1
|
1280 function _locale_import_one_string_db(&$report, $langcode, $source, $translation, $textgroup, $location, $mode, $plid = NULL, $plural = NULL) { |
webmaster@1
|
1281 $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup)); |
webmaster@1
|
1282 |
webmaster@1
|
1283 if (!empty($translation)) { |
webmaster@1
|
1284 if ($lid) { |
webmaster@1
|
1285 // We have this source string saved already. |
webmaster@1
|
1286 db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $location, $lid); |
webmaster@1
|
1287 $exists = (bool) db_result(db_query("SELECT lid FROM {locales_target} WHERE lid = %d AND language = '%s'", $lid, $langcode)); |
webmaster@1
|
1288 if (!$exists) { |
webmaster@1
|
1289 // No translation in this language. |
webmaster@1
|
1290 db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural); |
webmaster@1
|
1291 $report[0]++; |
webmaster@1
|
1292 } |
webmaster@1
|
1293 else if ($mode == LOCALE_IMPORT_OVERWRITE) { |
webmaster@1
|
1294 // Translation exists, only overwrite if instructed. |
webmaster@1
|
1295 db_query("UPDATE {locales_target} SET translation = '%s', plid = %d, plural = %d WHERE language = '%s' AND lid = %d", $translation, $plid, $plural, $langcode, $lid); |
webmaster@1
|
1296 $report[1]++; |
webmaster@1
|
1297 } |
webmaster@1
|
1298 } |
webmaster@1
|
1299 else { |
webmaster@1
|
1300 // No such source string in the database yet. |
webmaster@1
|
1301 db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', '%s')", $location, $source, $textgroup); |
webmaster@1
|
1302 $lid = db_result(db_query("SELECT lid FROM {locales_source} WHERE source = '%s' AND textgroup = '%s'", $source, $textgroup)); |
webmaster@1
|
1303 db_query("INSERT INTO {locales_target} (lid, language, translation, plid, plural) VALUES (%d, '%s', '%s', %d, %d)", $lid, $langcode, $translation, $plid, $plural); |
webmaster@1
|
1304 $report[0]++; |
webmaster@1
|
1305 } |
webmaster@1
|
1306 } |
webmaster@1
|
1307 elseif ($mode == LOCALE_IMPORT_OVERWRITE) { |
webmaster@1
|
1308 // Empty translation, remove existing if instructed. |
webmaster@1
|
1309 db_query("DELETE FROM {locales_target} WHERE language = '%s' AND lid = %d AND plid = %d AND plural = %d", $translation, $langcode, $lid, $plid, $plural); |
webmaster@1
|
1310 $report[2]++; |
webmaster@1
|
1311 } |
webmaster@1
|
1312 |
webmaster@1
|
1313 return $lid; |
webmaster@1
|
1314 } |
webmaster@1
|
1315 |
webmaster@1
|
1316 /** |
webmaster@1
|
1317 * Parses a Gettext Portable Object file header |
webmaster@1
|
1318 * |
webmaster@1
|
1319 * @param $header |
webmaster@1
|
1320 * A string containing the complete header |
webmaster@1
|
1321 * @return |
webmaster@1
|
1322 * An associative array of key-value pairs |
webmaster@1
|
1323 */ |
webmaster@1
|
1324 function _locale_import_parse_header($header) { |
webmaster@1
|
1325 $header_parsed = array(); |
webmaster@1
|
1326 $lines = array_map('trim', explode("\n", $header)); |
webmaster@1
|
1327 foreach ($lines as $line) { |
webmaster@1
|
1328 if ($line) { |
webmaster@1
|
1329 list($tag, $contents) = explode(":", $line, 2); |
webmaster@1
|
1330 $header_parsed[trim($tag)] = trim($contents); |
webmaster@1
|
1331 } |
webmaster@1
|
1332 } |
webmaster@1
|
1333 return $header_parsed; |
webmaster@1
|
1334 } |
webmaster@1
|
1335 |
webmaster@1
|
1336 /** |
webmaster@1
|
1337 * Parses a Plural-Forms entry from a Gettext Portable Object file header |
webmaster@1
|
1338 * |
webmaster@1
|
1339 * @param $pluralforms |
webmaster@1
|
1340 * A string containing the Plural-Forms entry |
webmaster@1
|
1341 * @param $filename |
webmaster@1
|
1342 * A string containing the filename |
webmaster@1
|
1343 * @return |
webmaster@1
|
1344 * An array containing the number of plurals and a |
webmaster@1
|
1345 * formula in PHP for computing the plural form |
webmaster@1
|
1346 */ |
webmaster@1
|
1347 function _locale_import_parse_plural_forms($pluralforms, $filename) { |
webmaster@1
|
1348 // First, delete all whitespace |
webmaster@1
|
1349 $pluralforms = strtr($pluralforms, array(" " => "", "\t" => "")); |
webmaster@1
|
1350 |
webmaster@1
|
1351 // Select the parts that define nplurals and plural |
webmaster@1
|
1352 $nplurals = strstr($pluralforms, "nplurals="); |
webmaster@1
|
1353 if (strpos($nplurals, ";")) { |
webmaster@1
|
1354 $nplurals = substr($nplurals, 9, strpos($nplurals, ";") - 9); |
webmaster@1
|
1355 } |
webmaster@1
|
1356 else { |
webmaster@1
|
1357 return FALSE; |
webmaster@1
|
1358 } |
webmaster@1
|
1359 $plural = strstr($pluralforms, "plural="); |
webmaster@1
|
1360 if (strpos($plural, ";")) { |
webmaster@1
|
1361 $plural = substr($plural, 7, strpos($plural, ";") - 7); |
webmaster@1
|
1362 } |
webmaster@1
|
1363 else { |
webmaster@1
|
1364 return FALSE; |
webmaster@1
|
1365 } |
webmaster@1
|
1366 |
webmaster@1
|
1367 // Get PHP version of the plural formula |
webmaster@1
|
1368 $plural = _locale_import_parse_arithmetic($plural); |
webmaster@1
|
1369 |
webmaster@1
|
1370 if ($plural !== FALSE) { |
webmaster@1
|
1371 return array($nplurals, $plural); |
webmaster@1
|
1372 } |
webmaster@1
|
1373 else { |
webmaster@1
|
1374 drupal_set_message(t('The translation file %filename contains an error: the plural formula could not be parsed.', array('%filename' => $filename)), 'error'); |
webmaster@1
|
1375 return FALSE; |
webmaster@1
|
1376 } |
webmaster@1
|
1377 } |
webmaster@1
|
1378 |
webmaster@1
|
1379 /** |
webmaster@1
|
1380 * Parses and sanitizes an arithmetic formula into a PHP expression |
webmaster@1
|
1381 * |
webmaster@1
|
1382 * While parsing, we ensure, that the operators have the right |
webmaster@1
|
1383 * precedence and associativity. |
webmaster@1
|
1384 * |
webmaster@1
|
1385 * @param $string |
webmaster@1
|
1386 * A string containing the arithmetic formula |
webmaster@1
|
1387 * @return |
webmaster@1
|
1388 * The PHP version of the formula |
webmaster@1
|
1389 */ |
webmaster@1
|
1390 function _locale_import_parse_arithmetic($string) { |
webmaster@1
|
1391 // Operator precedence table |
webmaster@1
|
1392 $prec = array("(" => -1, ")" => -1, "?" => 1, ":" => 1, "||" => 3, "&&" => 4, "==" => 5, "!=" => 5, "<" => 6, ">" => 6, "<=" => 6, ">=" => 6, "+" => 7, "-" => 7, "*" => 8, "/" => 8, "%" => 8); |
webmaster@1
|
1393 // Right associativity |
webmaster@1
|
1394 $rasc = array("?" => 1, ":" => 1); |
webmaster@1
|
1395 |
webmaster@1
|
1396 $tokens = _locale_import_tokenize_formula($string); |
webmaster@1
|
1397 |
webmaster@1
|
1398 // Parse by converting into infix notation then back into postfix |
webmaster@1
|
1399 $opstk = array(); |
webmaster@1
|
1400 $elstk = array(); |
webmaster@1
|
1401 |
webmaster@1
|
1402 foreach ($tokens as $token) { |
webmaster@1
|
1403 $ctok = $token; |
webmaster@1
|
1404 |
webmaster@1
|
1405 // Numbers and the $n variable are simply pushed into $elarr |
webmaster@1
|
1406 if (is_numeric($token)) { |
webmaster@1
|
1407 $elstk[] = $ctok; |
webmaster@1
|
1408 } |
webmaster@1
|
1409 elseif ($ctok == "n") { |
webmaster@1
|
1410 $elstk[] = '$n'; |
webmaster@1
|
1411 } |
webmaster@1
|
1412 elseif ($ctok == "(") { |
webmaster@1
|
1413 $opstk[] = $ctok; |
webmaster@1
|
1414 } |
webmaster@1
|
1415 elseif ($ctok == ")") { |
webmaster@1
|
1416 $topop = array_pop($opstk); |
webmaster@1
|
1417 while (isset($topop) && ($topop != "(")) { |
webmaster@1
|
1418 $elstk[] = $topop; |
webmaster@1
|
1419 $topop = array_pop($opstk); |
webmaster@1
|
1420 } |
webmaster@1
|
1421 } |
webmaster@1
|
1422 elseif (!empty($prec[$ctok])) { |
webmaster@1
|
1423 // If it's an operator, then pop from $oparr into $elarr until the |
webmaster@1
|
1424 // precedence in $oparr is less than current, then push into $oparr |
webmaster@1
|
1425 $topop = array_pop($opstk); |
webmaster@1
|
1426 while (isset($topop) && ($prec[$topop] >= $prec[$ctok]) && !(($prec[$topop] == $prec[$ctok]) && !empty($rasc[$topop]) && !empty($rasc[$ctok]))) { |
webmaster@1
|
1427 $elstk[] = $topop; |
webmaster@1
|
1428 $topop = array_pop($opstk); |
webmaster@1
|
1429 } |
webmaster@1
|
1430 if ($topop) { |
webmaster@1
|
1431 $opstk[] = $topop; // Return element to top |
webmaster@1
|
1432 } |
webmaster@1
|
1433 $opstk[] = $ctok; // Parentheses are not needed |
webmaster@1
|
1434 } |
webmaster@1
|
1435 else { |
webmaster@1
|
1436 return FALSE; |
webmaster@1
|
1437 } |
webmaster@1
|
1438 } |
webmaster@1
|
1439 |
webmaster@1
|
1440 // Flush operator stack |
webmaster@1
|
1441 $topop = array_pop($opstk); |
webmaster@1
|
1442 while ($topop != NULL) { |
webmaster@1
|
1443 $elstk[] = $topop; |
webmaster@1
|
1444 $topop = array_pop($opstk); |
webmaster@1
|
1445 } |
webmaster@1
|
1446 |
webmaster@1
|
1447 // Now extract formula from stack |
webmaster@1
|
1448 $prevsize = count($elstk) + 1; |
webmaster@1
|
1449 while (count($elstk) < $prevsize) { |
webmaster@1
|
1450 $prevsize = count($elstk); |
webmaster@1
|
1451 for ($i = 2; $i < count($elstk); $i++) { |
webmaster@1
|
1452 $op = $elstk[$i]; |
webmaster@1
|
1453 if (!empty($prec[$op])) { |
webmaster@1
|
1454 $f = ""; |
webmaster@1
|
1455 if ($op == ":") { |
webmaster@1
|
1456 $f = $elstk[$i - 2] ."):". $elstk[$i - 1] .")"; |
webmaster@1
|
1457 } |
webmaster@1
|
1458 elseif ($op == "?") { |
webmaster@1
|
1459 $f = "(". $elstk[$i - 2] ."?(". $elstk[$i - 1]; |
webmaster@1
|
1460 } |
webmaster@1
|
1461 else { |
webmaster@1
|
1462 $f = "(". $elstk[$i - 2] . $op . $elstk[$i - 1] .")"; |
webmaster@1
|
1463 } |
webmaster@1
|
1464 array_splice($elstk, $i - 2, 3, $f); |
webmaster@1
|
1465 break; |
webmaster@1
|
1466 } |
webmaster@1
|
1467 } |
webmaster@1
|
1468 } |
webmaster@1
|
1469 |
webmaster@1
|
1470 // If only one element is left, the number of operators is appropriate |
webmaster@1
|
1471 if (count($elstk) == 1) { |
webmaster@1
|
1472 return $elstk[0]; |
webmaster@1
|
1473 } |
webmaster@1
|
1474 else { |
webmaster@1
|
1475 return FALSE; |
webmaster@1
|
1476 } |
webmaster@1
|
1477 } |
webmaster@1
|
1478 |
webmaster@1
|
1479 /** |
webmaster@1
|
1480 * Backward compatible implementation of token_get_all() for formula parsing |
webmaster@1
|
1481 * |
webmaster@1
|
1482 * @param $string |
webmaster@1
|
1483 * A string containing the arithmetic formula |
webmaster@1
|
1484 * @return |
webmaster@1
|
1485 * The PHP version of the formula |
webmaster@1
|
1486 */ |
webmaster@1
|
1487 function _locale_import_tokenize_formula($formula) { |
webmaster@1
|
1488 $formula = str_replace(" ", "", $formula); |
webmaster@1
|
1489 $tokens = array(); |
webmaster@1
|
1490 for ($i = 0; $i < strlen($formula); $i++) { |
webmaster@1
|
1491 if (is_numeric($formula[$i])) { |
webmaster@1
|
1492 $num = $formula[$i]; |
webmaster@1
|
1493 $j = $i + 1; |
webmaster@1
|
1494 while ($j < strlen($formula) && is_numeric($formula[$j])) { |
webmaster@1
|
1495 $num .= $formula[$j]; |
webmaster@1
|
1496 $j++; |
webmaster@1
|
1497 } |
webmaster@1
|
1498 $i = $j - 1; |
webmaster@1
|
1499 $tokens[] = $num; |
webmaster@1
|
1500 } |
webmaster@1
|
1501 elseif ($pos = strpos(" =<>!&|", $formula[$i])) { // We won't have a space |
webmaster@1
|
1502 $next = $formula[$i + 1]; |
webmaster@1
|
1503 switch ($pos) { |
webmaster@1
|
1504 case 1: |
webmaster@1
|
1505 case 2: |
webmaster@1
|
1506 case 3: |
webmaster@1
|
1507 case 4: |
webmaster@1
|
1508 if ($next == '=') { |
webmaster@1
|
1509 $tokens[] = $formula[$i] .'='; |
webmaster@1
|
1510 $i++; |
webmaster@1
|
1511 } |
webmaster@1
|
1512 else { |
webmaster@1
|
1513 $tokens[] = $formula[$i]; |
webmaster@1
|
1514 } |
webmaster@1
|
1515 break; |
webmaster@1
|
1516 case 5: |
webmaster@1
|
1517 if ($next == '&') { |
webmaster@1
|
1518 $tokens[] = '&&'; |
webmaster@1
|
1519 $i++; |
webmaster@1
|
1520 } |
webmaster@1
|
1521 else { |
webmaster@1
|
1522 $tokens[] = $formula[$i]; |
webmaster@1
|
1523 } |
webmaster@1
|
1524 break; |
webmaster@1
|
1525 case 6: |
webmaster@1
|
1526 if ($next == '|') { |
webmaster@1
|
1527 $tokens[] = '||'; |
webmaster@1
|
1528 $i++; |
webmaster@1
|
1529 } |
webmaster@1
|
1530 else { |
webmaster@1
|
1531 $tokens[] = $formula[$i]; |
webmaster@1
|
1532 } |
webmaster@1
|
1533 break; |
webmaster@1
|
1534 } |
webmaster@1
|
1535 } |
webmaster@1
|
1536 else { |
webmaster@1
|
1537 $tokens[] = $formula[$i]; |
webmaster@1
|
1538 } |
webmaster@1
|
1539 } |
webmaster@1
|
1540 return $tokens; |
webmaster@1
|
1541 } |
webmaster@1
|
1542 |
webmaster@1
|
1543 /** |
webmaster@1
|
1544 * Modify a string to contain proper count indices |
webmaster@1
|
1545 * |
webmaster@1
|
1546 * This is a callback function used via array_map() |
webmaster@1
|
1547 * |
webmaster@1
|
1548 * @param $entry |
webmaster@1
|
1549 * An array element |
webmaster@1
|
1550 * @param $key |
webmaster@1
|
1551 * Index of the array element |
webmaster@1
|
1552 */ |
webmaster@1
|
1553 function _locale_import_append_plural($entry, $key) { |
webmaster@1
|
1554 // No modifications for 0, 1 |
webmaster@1
|
1555 if ($key == 0 || $key == 1) { |
webmaster@1
|
1556 return $entry; |
webmaster@1
|
1557 } |
webmaster@1
|
1558 |
webmaster@1
|
1559 // First remove any possibly false indices, then add new ones |
webmaster@1
|
1560 $entry = preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); |
webmaster@1
|
1561 return preg_replace('/(@count)/', "\\1[$key]", $entry); |
webmaster@1
|
1562 } |
webmaster@1
|
1563 |
webmaster@1
|
1564 /** |
webmaster@1
|
1565 * Generate a short, one string version of the passed comment array |
webmaster@1
|
1566 * |
webmaster@1
|
1567 * @param $comment |
webmaster@1
|
1568 * An array of strings containing a comment |
webmaster@1
|
1569 * @return |
webmaster@1
|
1570 * Short one string version of the comment |
webmaster@1
|
1571 */ |
webmaster@1
|
1572 function _locale_import_shorten_comments($comment) { |
webmaster@1
|
1573 $comm = ''; |
webmaster@1
|
1574 while (count($comment)) { |
webmaster@1
|
1575 $test = $comm . substr(array_shift($comment), 1) .', '; |
webmaster@1
|
1576 if (strlen($comm) < 130) { |
webmaster@1
|
1577 $comm = $test; |
webmaster@1
|
1578 } |
webmaster@1
|
1579 else { |
webmaster@1
|
1580 break; |
webmaster@1
|
1581 } |
webmaster@1
|
1582 } |
webmaster@1
|
1583 return substr($comm, 0, -2); |
webmaster@1
|
1584 } |
webmaster@1
|
1585 |
webmaster@1
|
1586 /** |
webmaster@1
|
1587 * Parses a string in quotes |
webmaster@1
|
1588 * |
webmaster@1
|
1589 * @param $string |
webmaster@1
|
1590 * A string specified with enclosing quotes |
webmaster@1
|
1591 * @return |
webmaster@1
|
1592 * The string parsed from inside the quotes |
webmaster@1
|
1593 */ |
webmaster@1
|
1594 function _locale_import_parse_quoted($string) { |
webmaster@1
|
1595 if (substr($string, 0, 1) != substr($string, -1, 1)) { |
webmaster@1
|
1596 return FALSE; // Start and end quotes must be the same |
webmaster@1
|
1597 } |
webmaster@1
|
1598 $quote = substr($string, 0, 1); |
webmaster@1
|
1599 $string = substr($string, 1, -1); |
webmaster@1
|
1600 if ($quote == '"') { // Double quotes: strip slashes |
webmaster@1
|
1601 return stripcslashes($string); |
webmaster@1
|
1602 } |
webmaster@1
|
1603 elseif ($quote == "'") { // Simple quote: return as-is |
webmaster@1
|
1604 return $string; |
webmaster@1
|
1605 } |
webmaster@1
|
1606 else { |
webmaster@1
|
1607 return FALSE; // Unrecognized quote |
webmaster@1
|
1608 } |
webmaster@1
|
1609 } |
webmaster@1
|
1610 /** |
webmaster@1
|
1611 * @} End of "locale-api-import" |
webmaster@1
|
1612 */ |
webmaster@1
|
1613 |
webmaster@1
|
1614 /** |
webmaster@1
|
1615 * Parses a JavaScript file, extracts strings wrapped in Drupal.t() and |
webmaster@1
|
1616 * Drupal.formatPlural() and inserts them into the database. |
webmaster@1
|
1617 */ |
webmaster@1
|
1618 function _locale_parse_js_file($filepath) { |
webmaster@1
|
1619 global $language; |
webmaster@1
|
1620 |
webmaster@1
|
1621 // Load the JavaScript file. |
webmaster@1
|
1622 $file = file_get_contents($filepath); |
webmaster@1
|
1623 |
webmaster@1
|
1624 // Match all calls to Drupal.t() in an array. |
webmaster@1
|
1625 // Note: \s also matches newlines with the 's' modifier. |
webmaster@1
|
1626 preg_match_all('~[^\w]Drupal\s*\.\s*t\s*\(\s*('. LOCALE_JS_STRING .')\s*[,\)]~s', $file, $t_matches); |
webmaster@1
|
1627 |
webmaster@1
|
1628 // Match all Drupal.formatPlural() calls in another array. |
webmaster@1
|
1629 preg_match_all('~[^\w]Drupal\s*\.\s*formatPlural\s*\(\s*.+?\s*,\s*('. LOCALE_JS_STRING .')\s*,\s*((?:(?:\'(?:\\\\\'|[^\'])*@count(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*@count(?:\\\\"|[^"])*")(?:\s*\+\s*)?)+)\s*[,\)]~s', $file, $plural_matches); |
webmaster@1
|
1630 |
webmaster@1
|
1631 // Loop through all matches and process them. |
webmaster@1
|
1632 $all_matches = array_merge($plural_matches[1], $t_matches[1]); |
webmaster@1
|
1633 foreach ($all_matches as $key => $string) { |
webmaster@1
|
1634 $strings = array($string); |
webmaster@1
|
1635 |
webmaster@1
|
1636 // If there is also a plural version of this string, add it to the strings array. |
webmaster@1
|
1637 if (isset($plural_matches[2][$key])) { |
webmaster@1
|
1638 $strings[] = $plural_matches[2][$key]; |
webmaster@1
|
1639 } |
webmaster@1
|
1640 |
webmaster@1
|
1641 foreach ($strings as $key => $string) { |
webmaster@1
|
1642 // Remove the quotes and string concatenations from the string. |
webmaster@1
|
1643 $string = implode('', preg_split('~(?<!\\\\)[\'"]\s*\+\s*[\'"]~s', substr($string, 1, -1))); |
webmaster@1
|
1644 |
webmaster@1
|
1645 $result = db_query("SELECT lid, location FROM {locales_source} WHERE source = '%s' AND textgroup = 'default'", $string); |
webmaster@1
|
1646 if ($source = db_fetch_object($result)) { |
webmaster@1
|
1647 // We already have this source string and now have to add the location |
webmaster@1
|
1648 // to the location column, if this file is not yet present in there. |
webmaster@1
|
1649 $locations = preg_split('~\s*;\s*~', $source->location); |
webmaster@1
|
1650 |
webmaster@1
|
1651 if (!in_array($filepath, $locations)) { |
webmaster@1
|
1652 $locations[] = $filepath; |
webmaster@1
|
1653 $locations = implode('; ', $locations); |
webmaster@1
|
1654 |
webmaster@1
|
1655 // Save the new locations string to the database. |
webmaster@1
|
1656 db_query("UPDATE {locales_source} SET location = '%s' WHERE lid = %d", $locations, $source->lid); |
webmaster@1
|
1657 } |
webmaster@1
|
1658 } |
webmaster@1
|
1659 else { |
webmaster@1
|
1660 // We don't have the source string yet, thus we insert it into the database. |
webmaster@1
|
1661 db_query("INSERT INTO {locales_source} (location, source, textgroup) VALUES ('%s', '%s', 'default')", $filepath, $string); |
webmaster@1
|
1662 } |
webmaster@1
|
1663 } |
webmaster@1
|
1664 } |
webmaster@1
|
1665 } |
webmaster@1
|
1666 |
webmaster@1
|
1667 /** |
webmaster@1
|
1668 * @defgroup locale-api-export Translation (template) export API. |
webmaster@1
|
1669 * @{ |
webmaster@1
|
1670 */ |
webmaster@1
|
1671 |
webmaster@1
|
1672 /** |
webmaster@1
|
1673 * Generates a structured array of all strings with translations in |
webmaster@1
|
1674 * $language, if given. This array can be used to generate an export |
webmaster@1
|
1675 * of the string in the database. |
webmaster@1
|
1676 * |
webmaster@1
|
1677 * @param $language |
webmaster@1
|
1678 * Language object to generate the output for, or NULL if generating |
webmaster@1
|
1679 * translation template. |
webmaster@1
|
1680 * @param $group |
webmaster@1
|
1681 * Text group to export PO file from (eg. 'default' for interface translations) |
webmaster@1
|
1682 */ |
webmaster@1
|
1683 function _locale_export_get_strings($language = NULL, $group = 'default') { |
webmaster@1
|
1684 if (isset($language)) { |
webmaster@1
|
1685 $result = db_query("SELECT s.lid, s.source, s.location, t.translation, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $language->language, $group); |
webmaster@1
|
1686 } |
webmaster@1
|
1687 else { |
webmaster@1
|
1688 $result = db_query("SELECT s.lid, s.source, s.location, t.plid, t.plural FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.textgroup = '%s' ORDER BY t.plid, t.plural", $group); |
webmaster@1
|
1689 } |
webmaster@1
|
1690 $strings = array(); |
webmaster@1
|
1691 while ($child = db_fetch_object($result)) { |
webmaster@1
|
1692 $string = array( |
webmaster@1
|
1693 'comment' => $child->location, |
webmaster@1
|
1694 'source' => $child->source, |
webmaster@1
|
1695 'translation' => isset($child->translation) ? $child->translation : '' |
webmaster@1
|
1696 ); |
webmaster@1
|
1697 if ($child->plid) { |
webmaster@1
|
1698 // Has a parent lid. Since we process in the order of plids, |
webmaster@1
|
1699 // we already have the parent in the array, so we can add the |
webmaster@1
|
1700 // lid to the next plural version to it. This builds a linked |
webmaster@1
|
1701 // list of plurals. |
webmaster@1
|
1702 $string['child'] = TRUE; |
webmaster@1
|
1703 $strings[$child->plid]['plural'] = $child->lid; |
webmaster@1
|
1704 } |
webmaster@1
|
1705 $strings[$child->lid] = $string; |
webmaster@1
|
1706 } |
webmaster@1
|
1707 return $strings; |
webmaster@1
|
1708 } |
webmaster@1
|
1709 |
webmaster@1
|
1710 /** |
webmaster@1
|
1711 * Generates the PO(T) file contents for given strings. |
webmaster@1
|
1712 * |
webmaster@1
|
1713 * @param $language |
webmaster@1
|
1714 * Language object to generate the output for, or NULL if generating |
webmaster@1
|
1715 * translation template. |
webmaster@1
|
1716 * @param $strings |
webmaster@1
|
1717 * Array of strings to export. See _locale_export_get_strings() |
webmaster@1
|
1718 * on how it should be formatted. |
webmaster@1
|
1719 * @param $header |
webmaster@1
|
1720 * The header portion to use for the output file. Defaults |
webmaster@1
|
1721 * are provided for PO and POT files. |
webmaster@1
|
1722 */ |
webmaster@1
|
1723 function _locale_export_po_generate($language = NULL, $strings = array(), $header = NULL) { |
webmaster@1
|
1724 global $user; |
webmaster@1
|
1725 |
webmaster@1
|
1726 if (!isset($header)) { |
webmaster@1
|
1727 if (isset($language)) { |
webmaster@1
|
1728 $header = '# '. $language->name .' translation of '. variable_get('site_name', 'Drupal') ."\n"; |
webmaster@1
|
1729 $header .= '# Generated by '. $user->name .' <'. $user->mail .">\n"; |
webmaster@1
|
1730 $header .= "#\n"; |
webmaster@1
|
1731 $header .= "msgid \"\"\n"; |
webmaster@1
|
1732 $header .= "msgstr \"\"\n"; |
webmaster@1
|
1733 $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n"; |
webmaster@1
|
1734 $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; |
webmaster@1
|
1735 $header .= "\"PO-Revision-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; |
webmaster@1
|
1736 $header .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n"; |
webmaster@1
|
1737 $header .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n"; |
webmaster@1
|
1738 $header .= "\"MIME-Version: 1.0\\n\"\n"; |
webmaster@1
|
1739 $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n"; |
webmaster@1
|
1740 $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; |
webmaster@1
|
1741 if ($language->formula && $language->plurals) { |
webmaster@1
|
1742 $header .= "\"Plural-Forms: nplurals=". $language->plurals ."; plural=". strtr($language->formula, array('$' => '')) .";\\n\"\n"; |
webmaster@1
|
1743 } |
webmaster@1
|
1744 } |
webmaster@1
|
1745 else { |
webmaster@1
|
1746 $header = "# LANGUAGE translation of PROJECT\n"; |
webmaster@1
|
1747 $header .= "# Copyright (c) YEAR NAME <EMAIL@ADDRESS>\n"; |
webmaster@1
|
1748 $header .= "#\n"; |
webmaster@1
|
1749 $header .= "msgid \"\"\n"; |
webmaster@1
|
1750 $header .= "msgstr \"\"\n"; |
webmaster@1
|
1751 $header .= "\"Project-Id-Version: PROJECT VERSION\\n\"\n"; |
webmaster@1
|
1752 $header .= "\"POT-Creation-Date: ". date("Y-m-d H:iO") ."\\n\"\n"; |
webmaster@1
|
1753 $header .= "\"PO-Revision-Date: YYYY-mm-DD HH:MM+ZZZZ\\n\"\n"; |
webmaster@1
|
1754 $header .= "\"Last-Translator: NAME <EMAIL@ADDRESS>\\n\"\n"; |
webmaster@1
|
1755 $header .= "\"Language-Team: LANGUAGE <EMAIL@ADDRESS>\\n\"\n"; |
webmaster@1
|
1756 $header .= "\"MIME-Version: 1.0\\n\"\n"; |
webmaster@1
|
1757 $header .= "\"Content-Type: text/plain; charset=utf-8\\n\"\n"; |
webmaster@1
|
1758 $header .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; |
webmaster@1
|
1759 $header .= "\"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n\"\n"; |
webmaster@1
|
1760 } |
webmaster@1
|
1761 } |
webmaster@1
|
1762 |
webmaster@1
|
1763 $output = $header ."\n"; |
webmaster@1
|
1764 |
webmaster@1
|
1765 foreach ($strings as $lid => $string) { |
webmaster@1
|
1766 // Only process non-children, children are output below their parent. |
webmaster@1
|
1767 if (!isset($string['child'])) { |
webmaster@1
|
1768 if ($string['comment']) { |
webmaster@1
|
1769 $output .= '#: '. $string['comment'] ."\n"; |
webmaster@1
|
1770 } |
webmaster@1
|
1771 $output .= 'msgid '. _locale_export_string($string['source']); |
webmaster@1
|
1772 if (!empty($string['plural'])) { |
webmaster@1
|
1773 $plural = $string['plural']; |
webmaster@1
|
1774 $output .= 'msgid_plural '. _locale_export_string($strings[$plural]['source']); |
webmaster@1
|
1775 if (isset($language)) { |
webmaster@1
|
1776 $translation = $string['translation']; |
webmaster@1
|
1777 for ($i = 0; $i < $language->plurals; $i++) { |
webmaster@1
|
1778 $output .= 'msgstr['. $i .'] '. _locale_export_string($translation); |
webmaster@1
|
1779 if ($plural) { |
webmaster@1
|
1780 $translation = _locale_export_remove_plural($strings[$plural]['translation']); |
webmaster@1
|
1781 $plural = isset($strings[$plural]['plural']) ? $strings[$plural]['plural'] : 0; |
webmaster@1
|
1782 } |
webmaster@1
|
1783 else { |
webmaster@1
|
1784 $translation = ''; |
webmaster@1
|
1785 } |
webmaster@1
|
1786 } |
webmaster@1
|
1787 } |
webmaster@1
|
1788 else { |
webmaster@1
|
1789 $output .= 'msgstr[0] ""'."\n"; |
webmaster@1
|
1790 $output .= 'msgstr[1] ""'."\n"; |
webmaster@1
|
1791 } |
webmaster@1
|
1792 } |
webmaster@1
|
1793 else { |
webmaster@1
|
1794 $output .= 'msgstr '. _locale_export_string($string['translation']); |
webmaster@1
|
1795 } |
webmaster@1
|
1796 $output .= "\n"; |
webmaster@1
|
1797 } |
webmaster@1
|
1798 } |
webmaster@1
|
1799 return $output; |
webmaster@1
|
1800 } |
webmaster@1
|
1801 |
webmaster@1
|
1802 /** |
webmaster@1
|
1803 * Write a generated PO or POT file to the output. |
webmaster@1
|
1804 * |
webmaster@1
|
1805 * @param $language |
webmaster@1
|
1806 * Language object to generate the output for, or NULL if generating |
webmaster@1
|
1807 * translation template. |
webmaster@1
|
1808 * @param $output |
webmaster@1
|
1809 * The PO(T) file to output as a string. See _locale_export_generate_po() |
webmaster@1
|
1810 * on how it can be generated. |
webmaster@1
|
1811 */ |
webmaster@1
|
1812 function _locale_export_po($language = NULL, $output = NULL) { |
webmaster@1
|
1813 // Log the export event. |
webmaster@1
|
1814 if (isset($language)) { |
webmaster@1
|
1815 $filename = $language->language .'.po'; |
webmaster@1
|
1816 watchdog('locale', 'Exported %locale translation file: %filename.', array('%locale' => $language->name, '%filename' => $filename)); |
webmaster@1
|
1817 } |
webmaster@1
|
1818 else { |
webmaster@1
|
1819 $filename = 'drupal.pot'; |
webmaster@1
|
1820 watchdog('locale', 'Exported translation file: %filename.', array('%filename' => $filename)); |
webmaster@1
|
1821 } |
webmaster@1
|
1822 // Download the file fo the client. |
webmaster@1
|
1823 header("Content-Disposition: attachment; filename=$filename"); |
webmaster@1
|
1824 header("Content-Type: text/plain; charset=utf-8"); |
webmaster@1
|
1825 print $output; |
webmaster@1
|
1826 die(); |
webmaster@1
|
1827 } |
webmaster@1
|
1828 |
webmaster@1
|
1829 /** |
webmaster@1
|
1830 * Print out a string on multiple lines |
webmaster@1
|
1831 */ |
webmaster@1
|
1832 function _locale_export_string($str) { |
webmaster@1
|
1833 $stri = addcslashes($str, "\0..\37\\\""); |
webmaster@1
|
1834 $parts = array(); |
webmaster@1
|
1835 |
webmaster@1
|
1836 // Cut text into several lines |
webmaster@1
|
1837 while ($stri != "") { |
webmaster@1
|
1838 $i = strpos($stri, "\\n"); |
webmaster@1
|
1839 if ($i === FALSE) { |
webmaster@1
|
1840 $curstr = $stri; |
webmaster@1
|
1841 $stri = ""; |
webmaster@1
|
1842 } |
webmaster@1
|
1843 else { |
webmaster@1
|
1844 $curstr = substr($stri, 0, $i + 2); |
webmaster@1
|
1845 $stri = substr($stri, $i + 2); |
webmaster@1
|
1846 } |
webmaster@1
|
1847 $curparts = explode("\n", _locale_export_wrap($curstr, 70)); |
webmaster@1
|
1848 $parts = array_merge($parts, $curparts); |
webmaster@1
|
1849 } |
webmaster@1
|
1850 |
webmaster@1
|
1851 // Multiline string |
webmaster@1
|
1852 if (count($parts) > 1) { |
webmaster@1
|
1853 return "\"\"\n\"". implode("\"\n\"", $parts) ."\"\n"; |
webmaster@1
|
1854 } |
webmaster@1
|
1855 // Single line string |
webmaster@1
|
1856 elseif (count($parts) == 1) { |
webmaster@1
|
1857 return "\"$parts[0]\"\n"; |
webmaster@1
|
1858 } |
webmaster@1
|
1859 // No translation |
webmaster@1
|
1860 else { |
webmaster@1
|
1861 return "\"\"\n"; |
webmaster@1
|
1862 } |
webmaster@1
|
1863 } |
webmaster@1
|
1864 |
webmaster@1
|
1865 /** |
webmaster@1
|
1866 * Custom word wrapping for Portable Object (Template) files. |
webmaster@1
|
1867 */ |
webmaster@1
|
1868 function _locale_export_wrap($str, $len) { |
webmaster@1
|
1869 $words = explode(' ', $str); |
webmaster@1
|
1870 $ret = array(); |
webmaster@1
|
1871 |
webmaster@1
|
1872 $cur = ""; |
webmaster@1
|
1873 $nstr = 1; |
webmaster@1
|
1874 while (count($words)) { |
webmaster@1
|
1875 $word = array_shift($words); |
webmaster@1
|
1876 if ($nstr) { |
webmaster@1
|
1877 $cur = $word; |
webmaster@1
|
1878 $nstr = 0; |
webmaster@1
|
1879 } |
webmaster@1
|
1880 elseif (strlen("$cur $word") > $len) { |
webmaster@1
|
1881 $ret[] = $cur ." "; |
webmaster@1
|
1882 $cur = $word; |
webmaster@1
|
1883 } |
webmaster@1
|
1884 else { |
webmaster@1
|
1885 $cur = "$cur $word"; |
webmaster@1
|
1886 } |
webmaster@1
|
1887 } |
webmaster@1
|
1888 $ret[] = $cur; |
webmaster@1
|
1889 |
webmaster@1
|
1890 return implode("\n", $ret); |
webmaster@1
|
1891 } |
webmaster@1
|
1892 |
webmaster@1
|
1893 /** |
webmaster@1
|
1894 * Removes plural index information from a string |
webmaster@1
|
1895 */ |
webmaster@1
|
1896 function _locale_export_remove_plural($entry) { |
webmaster@1
|
1897 return preg_replace('/(@count)\[[0-9]\]/', '\\1', $entry); |
webmaster@1
|
1898 } |
webmaster@1
|
1899 /** |
webmaster@1
|
1900 * @} End of "locale-api-export" |
webmaster@1
|
1901 */ |
webmaster@1
|
1902 |
webmaster@1
|
1903 /** |
webmaster@1
|
1904 * @defgroup locale-api-seek String search functions. |
webmaster@1
|
1905 * @{ |
webmaster@1
|
1906 */ |
webmaster@1
|
1907 |
webmaster@1
|
1908 /** |
webmaster@1
|
1909 * Perform a string search and display results in a table |
webmaster@1
|
1910 */ |
webmaster@1
|
1911 function _locale_translate_seek() { |
webmaster@1
|
1912 $output = ''; |
webmaster@1
|
1913 |
webmaster@1
|
1914 // We have at least one criterion to match |
webmaster@1
|
1915 if ($query = _locale_translate_seek_query()) { |
webmaster@1
|
1916 $join = "SELECT s.source, s.location, s.lid, s.textgroup, t.translation, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid "; |
webmaster@1
|
1917 |
webmaster@1
|
1918 $arguments = array(); |
webmaster@1
|
1919 $limit_language = FALSE; |
webmaster@1
|
1920 // Compute LIKE section |
webmaster@1
|
1921 switch ($query['translation']) { |
webmaster@1
|
1922 case 'translated': |
webmaster@1
|
1923 $where = "WHERE (t.translation LIKE '%%%s%%')"; |
webmaster@1
|
1924 $orderby = "ORDER BY t.translation"; |
webmaster@1
|
1925 $arguments[] = $query['string']; |
webmaster@1
|
1926 break; |
webmaster@1
|
1927 case 'untranslated': |
webmaster@1
|
1928 $where = "WHERE (s.source LIKE '%%%s%%' AND t.translation IS NULL)"; |
webmaster@1
|
1929 $orderby = "ORDER BY s.source"; |
webmaster@1
|
1930 $arguments[] = $query['string']; |
webmaster@1
|
1931 break; |
webmaster@1
|
1932 case 'all' : |
webmaster@1
|
1933 default: |
webmaster@1
|
1934 $where = "WHERE (s.source LIKE '%%%s%%' OR t.translation LIKE '%%%s%%')"; |
webmaster@1
|
1935 $orderby = ''; |
webmaster@1
|
1936 $arguments[] = $query['string']; |
webmaster@1
|
1937 $arguments[] = $query['string']; |
webmaster@1
|
1938 break; |
webmaster@1
|
1939 } |
webmaster@1
|
1940 $grouplimit = ''; |
webmaster@1
|
1941 if (!empty($query['group']) && $query['group'] != 'all') { |
webmaster@1
|
1942 $grouplimit = " AND s.textgroup = '%s'"; |
webmaster@1
|
1943 $arguments[] = $query['group']; |
webmaster@1
|
1944 } |
webmaster@1
|
1945 |
webmaster@1
|
1946 switch ($query['language']) { |
webmaster@1
|
1947 // Force search in source strings |
webmaster@1
|
1948 case "en": |
webmaster@1
|
1949 $sql = $join ." WHERE s.source LIKE '%%%s%%' $grouplimit ORDER BY s.source"; |
webmaster@1
|
1950 $arguments = array($query['string']); // $where is not used, discard its arguments |
webmaster@1
|
1951 if (!empty($grouplimit)) { |
webmaster@1
|
1952 $arguments[] = $query['group']; |
webmaster@1
|
1953 } |
webmaster@1
|
1954 break; |
webmaster@1
|
1955 // Search in all languages |
webmaster@1
|
1956 case "all": |
webmaster@1
|
1957 $sql = "$join $where $grouplimit $orderby"; |
webmaster@1
|
1958 break; |
webmaster@1
|
1959 // Some different language |
webmaster@1
|
1960 default: |
webmaster@1
|
1961 $sql = "$join AND t.language = '%s' $where $grouplimit $orderby"; |
webmaster@1
|
1962 array_unshift($arguments, $query['language']); |
webmaster@1
|
1963 // Don't show translation flags for other languages, we can't see them with this search. |
webmaster@1
|
1964 $limit_language = $query['language']; |
webmaster@1
|
1965 } |
webmaster@1
|
1966 |
webmaster@1
|
1967 $result = pager_query($sql, 50, 0, NULL, $arguments); |
webmaster@1
|
1968 |
webmaster@1
|
1969 $groups = module_invoke_all('locale', 'groups'); |
webmaster@1
|
1970 $header = array(t('Text group'), t('String'), ($limit_language) ? t('Language') : t('Languages'), array('data' => t('Operations'), 'colspan' => '2')); |
webmaster@1
|
1971 $arr = array(); |
webmaster@1
|
1972 while ($locale = db_fetch_object($result)) { |
webmaster@1
|
1973 $arr[$locale->lid]['group'] = $groups[$locale->textgroup]; |
webmaster@1
|
1974 $arr[$locale->lid]['languages'][$locale->language] = $locale->translation; |
webmaster@1
|
1975 $arr[$locale->lid]['location'] = $locale->location; |
webmaster@1
|
1976 $arr[$locale->lid]['source'] = $locale->source; |
webmaster@1
|
1977 } |
webmaster@1
|
1978 $rows = array(); |
webmaster@1
|
1979 foreach ($arr as $lid => $value) { |
webmaster@1
|
1980 $rows[] = array( |
webmaster@1
|
1981 $value['group'], |
webmaster@1
|
1982 array('data' => check_plain(truncate_utf8($value['source'], 150, FALSE, TRUE)) .'<br /><small>'. $value['location'] .'</small>'), |
webmaster@1
|
1983 array('data' => _locale_translate_language_list($value['languages'], $limit_language), 'align' => 'center'), |
webmaster@1
|
1984 array('data' => l(t('edit'), "admin/build/translate/edit/$lid"), 'class' => 'nowrap'), |
webmaster@1
|
1985 array('data' => l(t('delete'), "admin/build/translate/delete/$lid"), 'class' => 'nowrap'), |
webmaster@1
|
1986 ); |
webmaster@1
|
1987 } |
webmaster@1
|
1988 |
webmaster@1
|
1989 if (count($rows)) { |
webmaster@1
|
1990 $output .= theme('table', $header, $rows); |
webmaster@1
|
1991 if ($pager = theme('pager', NULL, 50)) { |
webmaster@1
|
1992 $output .= $pager; |
webmaster@1
|
1993 } |
webmaster@1
|
1994 } |
webmaster@1
|
1995 else { |
webmaster@1
|
1996 $output .= t('No strings found for your search.'); |
webmaster@1
|
1997 } |
webmaster@1
|
1998 } |
webmaster@1
|
1999 |
webmaster@1
|
2000 return $output; |
webmaster@1
|
2001 } |
webmaster@1
|
2002 |
webmaster@1
|
2003 /** |
webmaster@1
|
2004 * Build array out of search criteria specified in request variables |
webmaster@1
|
2005 */ |
webmaster@1
|
2006 function _locale_translate_seek_query() { |
webmaster@1
|
2007 static $query = NULL; |
webmaster@1
|
2008 if (!isset($query)) { |
webmaster@1
|
2009 $query = array(); |
webmaster@1
|
2010 $fields = array('string', 'language', 'translation', 'group'); |
webmaster@1
|
2011 foreach ($fields as $field) { |
webmaster@1
|
2012 if (isset($_REQUEST[$field])) { |
webmaster@1
|
2013 $query[$field] = $_REQUEST[$field]; |
webmaster@1
|
2014 } |
webmaster@1
|
2015 } |
webmaster@1
|
2016 } |
webmaster@1
|
2017 return $query; |
webmaster@1
|
2018 } |
webmaster@1
|
2019 |
webmaster@1
|
2020 /** |
webmaster@1
|
2021 * Force the JavaScript translation file(s) to be refreshed. |
webmaster@1
|
2022 * |
webmaster@1
|
2023 * This function sets a refresh flag for a specified language, or all |
webmaster@1
|
2024 * languages except English, if none specified. JavaScript translation |
webmaster@1
|
2025 * files are rebuilt (with locale_update_js_files()) the next time a |
webmaster@1
|
2026 * request is served in that language. |
webmaster@1
|
2027 * |
webmaster@1
|
2028 * @param $langcode |
webmaster@1
|
2029 * The language code for which the file needs to be refreshed. |
webmaster@1
|
2030 * @return |
webmaster@1
|
2031 * New content of the 'javascript_parsed' variable. |
webmaster@1
|
2032 */ |
webmaster@1
|
2033 function _locale_invalidate_js($langcode = NULL) { |
webmaster@1
|
2034 $parsed = variable_get('javascript_parsed', array()); |
webmaster@1
|
2035 |
webmaster@1
|
2036 if (empty($langcode)) { |
webmaster@1
|
2037 // Invalidate all languages. |
webmaster@1
|
2038 $languages = language_list(); |
webmaster@1
|
2039 unset($languages['en']); |
webmaster@1
|
2040 foreach ($languages as $lcode => $data) { |
webmaster@1
|
2041 $parsed['refresh:'. $lcode] = 'waiting'; |
webmaster@1
|
2042 } |
webmaster@1
|
2043 } |
webmaster@1
|
2044 else { |
webmaster@1
|
2045 // Invalidate single language. |
webmaster@1
|
2046 $parsed['refresh:'. $langcode] = 'waiting'; |
webmaster@1
|
2047 } |
webmaster@1
|
2048 |
webmaster@1
|
2049 variable_set('javascript_parsed', $parsed); |
webmaster@1
|
2050 return $parsed; |
webmaster@1
|
2051 } |
webmaster@1
|
2052 |
webmaster@1
|
2053 /** |
webmaster@1
|
2054 * (Re-)Creates the JavaScript translation file for a language. |
webmaster@1
|
2055 * |
webmaster@1
|
2056 * @param $language |
webmaster@1
|
2057 * The language, the translation file should be (re)created for. |
webmaster@1
|
2058 */ |
webmaster@1
|
2059 function _locale_rebuild_js($langcode = NULL) { |
webmaster@1
|
2060 if (!isset($langcode)) { |
webmaster@1
|
2061 global $language; |
webmaster@1
|
2062 } |
webmaster@1
|
2063 else { |
webmaster@1
|
2064 // Get information about the locale. |
webmaster@1
|
2065 $languages = language_list(); |
webmaster@1
|
2066 $language = $languages[$langcode]; |
webmaster@1
|
2067 } |
webmaster@1
|
2068 |
webmaster@1
|
2069 // Construct the array for JavaScript translations. |
webmaster@1
|
2070 // We sort on plural so that we have all plural forms before singular forms. |
webmaster@1
|
2071 $result = db_query("SELECT s.lid, s.source, t.plid, t.plural, t.translation FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid AND t.language = '%s' WHERE s.location LIKE '%%.js%%' AND s.textgroup = 'default' ORDER BY t.plural DESC", $language->language); |
webmaster@1
|
2072 |
webmaster@1
|
2073 $translations = $plurals = array(); |
webmaster@1
|
2074 while ($data = db_fetch_object($result)) { |
webmaster@1
|
2075 // Only add this to the translations array when there is actually a translation. |
webmaster@1
|
2076 if (!empty($data->translation)) { |
webmaster@1
|
2077 if ($data->plural) { |
webmaster@1
|
2078 // When the translation is a plural form, first add it to another array and |
webmaster@1
|
2079 // wait for the singular (parent) translation. |
webmaster@1
|
2080 if (!isset($plurals[$data->plid])) { |
webmaster@1
|
2081 $plurals[$data->plid] = array($data->plural => $data->translation); |
webmaster@1
|
2082 } |
webmaster@1
|
2083 else { |
webmaster@1
|
2084 $plurals[$data->plid] += array($data->plural => $data->translation); |
webmaster@1
|
2085 } |
webmaster@1
|
2086 } |
webmaster@1
|
2087 elseif (isset($plurals[$data->lid])) { |
webmaster@1
|
2088 // There are plural translations for this translation, so get them from |
webmaster@1
|
2089 // the plurals array and add them to the final translations array. |
webmaster@1
|
2090 $translations[$data->source] = array($data->plural => $data->translation) + $plurals[$data->lid]; |
webmaster@1
|
2091 unset($plurals[$data->lid]); |
webmaster@1
|
2092 } |
webmaster@1
|
2093 else { |
webmaster@1
|
2094 // There are no plural forms for this translation, so just add it to |
webmaster@1
|
2095 // the translations array. |
webmaster@1
|
2096 $translations[$data->source] = $data->translation; |
webmaster@1
|
2097 } |
webmaster@1
|
2098 } |
webmaster@1
|
2099 } |
webmaster@1
|
2100 |
webmaster@1
|
2101 // Construct the JavaScript file, if there are translations. |
webmaster@1
|
2102 $data = $status = ''; |
webmaster@1
|
2103 if (!empty($translations)) { |
webmaster@1
|
2104 |
webmaster@1
|
2105 $data = "Drupal.locale = { "; |
webmaster@1
|
2106 |
webmaster@1
|
2107 if (!empty($language->formula)) { |
webmaster@1
|
2108 $data .= "'pluralFormula': function(\$n) { return Number({$language->formula}); }, "; |
webmaster@1
|
2109 } |
webmaster@1
|
2110 |
webmaster@1
|
2111 $data .= "'strings': ". drupal_to_js($translations) ." };"; |
webmaster@1
|
2112 $data_hash = md5($data); |
webmaster@1
|
2113 } |
webmaster@1
|
2114 |
webmaster@1
|
2115 // Construct the filepath where JS translation files are stored. |
webmaster@1
|
2116 // There is (on purpose) no front end to edit that variable. |
webmaster@1
|
2117 $dir = file_create_path(variable_get('locale_js_directory', 'languages')); |
webmaster@1
|
2118 |
webmaster@1
|
2119 // Delete old file, if we have no translations anymore, or a different file to be saved. |
webmaster@1
|
2120 if (!empty($language->javascript) && (!$data || $language->javascript != $data_hash)) { |
webmaster@1
|
2121 file_delete(file_create_path($dir .'/'. $language->language .'_'. $language->javascript .'.js')); |
webmaster@1
|
2122 $language->javascript = ''; |
webmaster@1
|
2123 $status = 'deleted'; |
webmaster@1
|
2124 } |
webmaster@1
|
2125 |
webmaster@1
|
2126 // Only create a new file if the content has changed. |
webmaster@1
|
2127 if ($data && $language->javascript != $data_hash) { |
webmaster@1
|
2128 // Ensure that the directory exists and is writable, if possible. |
webmaster@1
|
2129 file_check_directory($dir, TRUE); |
webmaster@1
|
2130 |
webmaster@1
|
2131 // Save the file. |
webmaster@1
|
2132 $dest = $dir .'/'. $language->language .'_'. $data_hash .'.js'; |
webmaster@1
|
2133 if (file_save_data($data, $dest)) { |
webmaster@1
|
2134 $language->javascript = $data_hash; |
webmaster@1
|
2135 $status = ($status == 'deleted') ? 'updated' : 'created'; |
webmaster@1
|
2136 } |
webmaster@1
|
2137 else { |
webmaster@1
|
2138 $language->javascript = ''; |
webmaster@1
|
2139 $status = 'error'; |
webmaster@1
|
2140 } |
webmaster@1
|
2141 } |
webmaster@1
|
2142 |
webmaster@1
|
2143 // Save the new JavaScript hash (or an empty value if the file |
webmaster@1
|
2144 // just got deleted). Act only if some operation was executed. |
webmaster@1
|
2145 if ($status) { |
webmaster@1
|
2146 db_query("UPDATE {languages} SET javascript = '%s' WHERE language = '%s'", $language->javascript, $language->language); |
webmaster@1
|
2147 |
webmaster@1
|
2148 // Update the default language variable if the default language has been altered. |
webmaster@1
|
2149 // This is necessary to keep the variable consistent with the database |
webmaster@1
|
2150 // version of the language and to prevent checking against an outdated hash. |
webmaster@1
|
2151 $default_langcode = language_default('language'); |
webmaster@1
|
2152 if ($default_langcode == $language->language) { |
webmaster@1
|
2153 $default = db_fetch_object(db_query("SELECT * FROM {languages} WHERE language = '%s'", $default_langcode)); |
webmaster@1
|
2154 variable_set('language_default', $default); |
webmaster@1
|
2155 } |
webmaster@1
|
2156 } |
webmaster@1
|
2157 |
webmaster@1
|
2158 // Log the operation and return success flag. |
webmaster@1
|
2159 switch ($status) { |
webmaster@1
|
2160 case 'updated': |
webmaster@1
|
2161 watchdog('locale', 'Updated JavaScript translation file for the language %language.', array('%language' => t($language->name))); |
webmaster@1
|
2162 return TRUE; |
webmaster@1
|
2163 case 'created': |
webmaster@1
|
2164 watchdog('locale', 'Created JavaScript translation file for the language %language.', array('%language' => t($language->name))); |
webmaster@1
|
2165 return TRUE; |
webmaster@1
|
2166 case 'deleted': |
webmaster@1
|
2167 watchdog('locale', 'Removed JavaScript translation file for the language %language, because no translations currently exist for that language.', array('%language' => t($language->name))); |
webmaster@1
|
2168 return TRUE; |
webmaster@1
|
2169 case 'error': |
webmaster@1
|
2170 watchdog('locale', 'An error occurred during creation of the JavaScript translation file for the language %language.', array('%language' => t($language->name)), WATCHDOG_ERROR); |
webmaster@1
|
2171 return FALSE; |
webmaster@1
|
2172 default: |
webmaster@1
|
2173 // No operation needed. |
webmaster@1
|
2174 return TRUE; |
webmaster@1
|
2175 } |
webmaster@1
|
2176 } |
webmaster@1
|
2177 |
webmaster@1
|
2178 /** |
webmaster@1
|
2179 * List languages in search result table |
webmaster@1
|
2180 */ |
webmaster@1
|
2181 function _locale_translate_language_list($translation, $limit_language) { |
webmaster@1
|
2182 // Add CSS |
webmaster@1
|
2183 drupal_add_css(drupal_get_path('module', 'locale') .'/locale.css', 'module', 'all', FALSE); |
webmaster@1
|
2184 |
webmaster@1
|
2185 $languages = language_list(); |
webmaster@1
|
2186 unset($languages['en']); |
webmaster@1
|
2187 $output = ''; |
webmaster@1
|
2188 foreach ($languages as $langcode => $language) { |
webmaster@1
|
2189 if (!$limit_language || $limit_language == $langcode) { |
webmaster@1
|
2190 $output .= (!empty($translation[$langcode])) ? $langcode .' ' : "<em class=\"locale-untranslated\">$langcode</em> "; |
webmaster@1
|
2191 } |
webmaster@1
|
2192 } |
webmaster@1
|
2193 |
webmaster@1
|
2194 return $output; |
webmaster@1
|
2195 } |
webmaster@1
|
2196 /** |
webmaster@1
|
2197 * @} End of "locale-api-seek" |
webmaster@1
|
2198 */ |
webmaster@1
|
2199 |
webmaster@1
|
2200 /** |
webmaster@1
|
2201 * @defgroup locale-api-predefined List of predefined languages |
webmaster@1
|
2202 * @{ |
webmaster@1
|
2203 */ |
webmaster@1
|
2204 |
webmaster@1
|
2205 /** |
webmaster@1
|
2206 * Prepares the language code list for a select form item with only the unsupported ones |
webmaster@1
|
2207 */ |
webmaster@1
|
2208 function _locale_prepare_predefined_list() { |
webmaster@1
|
2209 $languages = language_list(); |
webmaster@1
|
2210 $predefined = _locale_get_predefined_list(); |
webmaster@1
|
2211 foreach ($predefined as $key => $value) { |
webmaster@1
|
2212 if (isset($languages[$key])) { |
webmaster@1
|
2213 unset($predefined[$key]); |
webmaster@1
|
2214 continue; |
webmaster@1
|
2215 } |
webmaster@1
|
2216 // Include native name in output, if possible |
webmaster@1
|
2217 if (count($value) > 1) { |
webmaster@1
|
2218 $tname = t($value[0]); |
webmaster@1
|
2219 $predefined[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])"; |
webmaster@1
|
2220 } |
webmaster@1
|
2221 else { |
webmaster@1
|
2222 $predefined[$key] = t($value[0]); |
webmaster@1
|
2223 } |
webmaster@1
|
2224 } |
webmaster@1
|
2225 asort($predefined); |
webmaster@1
|
2226 return $predefined; |
webmaster@1
|
2227 } |
webmaster@1
|
2228 |
webmaster@1
|
2229 /** |
webmaster@1
|
2230 * Some of the common languages with their English and native names |
webmaster@1
|
2231 * |
webmaster@1
|
2232 * Based on ISO 639 and http://people.w3.org/rishida/names/languages.html |
webmaster@1
|
2233 */ |
webmaster@1
|
2234 function _locale_get_predefined_list() { |
webmaster@1
|
2235 return array( |
webmaster@1
|
2236 "aa" => array("Afar"), |
webmaster@1
|
2237 "ab" => array("Abkhazian", "аҧсуа бызшәа"), |
webmaster@1
|
2238 "ae" => array("Avestan"), |
webmaster@1
|
2239 "af" => array("Afrikaans"), |
webmaster@1
|
2240 "ak" => array("Akan"), |
webmaster@1
|
2241 "am" => array("Amharic", "አማርኛ"), |
webmaster@1
|
2242 "ar" => array("Arabic", /* Left-to-right marker "" */ "العربية", LANGUAGE_RTL), |
webmaster@1
|
2243 "as" => array("Assamese"), |
webmaster@1
|
2244 "av" => array("Avar"), |
webmaster@1
|
2245 "ay" => array("Aymara"), |
webmaster@1
|
2246 "az" => array("Azerbaijani", "azərbaycan"), |
webmaster@1
|
2247 "ba" => array("Bashkir"), |
webmaster@1
|
2248 "be" => array("Belarusian", "Беларуская"), |
webmaster@1
|
2249 "bg" => array("Bulgarian", "Български"), |
webmaster@1
|
2250 "bh" => array("Bihari"), |
webmaster@1
|
2251 "bi" => array("Bislama"), |
webmaster@1
|
2252 "bm" => array("Bambara", "Bamanankan"), |
webmaster@1
|
2253 "bn" => array("Bengali"), |
webmaster@1
|
2254 "bo" => array("Tibetan"), |
webmaster@1
|
2255 "br" => array("Breton"), |
webmaster@1
|
2256 "bs" => array("Bosnian", "Bosanski"), |
webmaster@1
|
2257 "ca" => array("Catalan", "Català"), |
webmaster@1
|
2258 "ce" => array("Chechen"), |
webmaster@1
|
2259 "ch" => array("Chamorro"), |
webmaster@1
|
2260 "co" => array("Corsican"), |
webmaster@1
|
2261 "cr" => array("Cree"), |
webmaster@1
|
2262 "cs" => array("Czech", "Čeština"), |
webmaster@1
|
2263 "cu" => array("Old Slavonic"), |
webmaster@1
|
2264 "cv" => array("Chuvash"), |
webmaster@1
|
2265 "cy" => array("Welsh", "Cymraeg"), |
webmaster@1
|
2266 "da" => array("Danish", "Dansk"), |
webmaster@1
|
2267 "de" => array("German", "Deutsch"), |
webmaster@1
|
2268 "dv" => array("Maldivian"), |
webmaster@1
|
2269 "dz" => array("Bhutani"), |
webmaster@1
|
2270 "ee" => array("Ewe", "Ɛʋɛ"), |
webmaster@1
|
2271 "el" => array("Greek", "Ελληνικά"), |
webmaster@1
|
2272 "en" => array("English"), |
webmaster@1
|
2273 "eo" => array("Esperanto"), |
webmaster@1
|
2274 "es" => array("Spanish", "Español"), |
webmaster@1
|
2275 "et" => array("Estonian", "Eesti"), |
webmaster@1
|
2276 "eu" => array("Basque", "Euskera"), |
webmaster@1
|
2277 "fa" => array("Persian", /* Left-to-right marker "" */ "فارسی", LANGUAGE_RTL), |
webmaster@1
|
2278 "ff" => array("Fulah", "Fulfulde"), |
webmaster@1
|
2279 "fi" => array("Finnish", "Suomi"), |
webmaster@1
|
2280 "fj" => array("Fiji"), |
webmaster@1
|
2281 "fo" => array("Faeroese"), |
webmaster@1
|
2282 "fr" => array("French", "Français"), |
webmaster@1
|
2283 "fy" => array("Frisian", "Frysk"), |
webmaster@1
|
2284 "ga" => array("Irish", "Gaeilge"), |
webmaster@1
|
2285 "gd" => array("Scots Gaelic"), |
webmaster@1
|
2286 "gl" => array("Galician", "Galego"), |
webmaster@1
|
2287 "gn" => array("Guarani"), |
webmaster@1
|
2288 "gu" => array("Gujarati"), |
webmaster@1
|
2289 "gv" => array("Manx"), |
webmaster@1
|
2290 "ha" => array("Hausa"), |
webmaster@1
|
2291 "he" => array("Hebrew", /* Left-to-right marker "" */ "עברית", LANGUAGE_RTL), |
webmaster@1
|
2292 "hi" => array("Hindi", "हिन्दी"), |
webmaster@1
|
2293 "ho" => array("Hiri Motu"), |
webmaster@1
|
2294 "hr" => array("Croatian", "Hrvatski"), |
webmaster@1
|
2295 "hu" => array("Hungarian", "Magyar"), |
webmaster@1
|
2296 "hy" => array("Armenian", "Հայերեն"), |
webmaster@1
|
2297 "hz" => array("Herero"), |
webmaster@1
|
2298 "ia" => array("Interlingua"), |
webmaster@1
|
2299 "id" => array("Indonesian", "Bahasa Indonesia"), |
webmaster@1
|
2300 "ie" => array("Interlingue"), |
webmaster@1
|
2301 "ig" => array("Igbo"), |
webmaster@1
|
2302 "ik" => array("Inupiak"), |
webmaster@1
|
2303 "is" => array("Icelandic", "Íslenska"), |
webmaster@1
|
2304 "it" => array("Italian", "Italiano"), |
webmaster@1
|
2305 "iu" => array("Inuktitut"), |
webmaster@1
|
2306 "ja" => array("Japanese", "日本語"), |
webmaster@1
|
2307 "jv" => array("Javanese"), |
webmaster@1
|
2308 "ka" => array("Georgian"), |
webmaster@1
|
2309 "kg" => array("Kongo"), |
webmaster@1
|
2310 "ki" => array("Kikuyu"), |
webmaster@1
|
2311 "kj" => array("Kwanyama"), |
webmaster@1
|
2312 "kk" => array("Kazakh", "Қазақ"), |
webmaster@1
|
2313 "kl" => array("Greenlandic"), |
webmaster@1
|
2314 "km" => array("Cambodian"), |
webmaster@1
|
2315 "kn" => array("Kannada", "ಕನ್ನಡ"), |
webmaster@1
|
2316 "ko" => array("Korean", "한국어"), |
webmaster@1
|
2317 "kr" => array("Kanuri"), |
webmaster@1
|
2318 "ks" => array("Kashmiri"), |
webmaster@1
|
2319 "ku" => array("Kurdish", "Kurdî"), |
webmaster@1
|
2320 "kv" => array("Komi"), |
webmaster@1
|
2321 "kw" => array("Cornish"), |
webmaster@1
|
2322 "ky" => array("Kirghiz", "Кыргыз"), |
webmaster@1
|
2323 "la" => array("Latin", "Latina"), |
webmaster@1
|
2324 "lb" => array("Luxembourgish"), |
webmaster@1
|
2325 "lg" => array("Luganda"), |
webmaster@1
|
2326 "ln" => array("Lingala"), |
webmaster@1
|
2327 "lo" => array("Laothian"), |
webmaster@1
|
2328 "lt" => array("Lithuanian", "Lietuvių"), |
webmaster@1
|
2329 "lv" => array("Latvian", "Latviešu"), |
webmaster@1
|
2330 "mg" => array("Malagasy"), |
webmaster@1
|
2331 "mh" => array("Marshallese"), |
webmaster@1
|
2332 "mi" => array("Maori"), |
webmaster@1
|
2333 "mk" => array("Macedonian", "Македонски"), |
webmaster@1
|
2334 "ml" => array("Malayalam", "മലയാളം"), |
webmaster@1
|
2335 "mn" => array("Mongolian"), |
webmaster@1
|
2336 "mo" => array("Moldavian"), |
webmaster@1
|
2337 "mr" => array("Marathi"), |
webmaster@1
|
2338 "ms" => array("Malay", "Bahasa Melayu"), |
webmaster@1
|
2339 "mt" => array("Maltese", "Malti"), |
webmaster@1
|
2340 "my" => array("Burmese"), |
webmaster@1
|
2341 "na" => array("Nauru"), |
webmaster@1
|
2342 "nd" => array("North Ndebele"), |
webmaster@1
|
2343 "ne" => array("Nepali"), |
webmaster@1
|
2344 "ng" => array("Ndonga"), |
webmaster@1
|
2345 "nl" => array("Dutch", "Nederlands"), |
webmaster@1
|
2346 "nb" => array("Norwegian Bokmål", "Bokmål"), |
webmaster@1
|
2347 "nn" => array("Norwegian Nynorsk", "Nynorsk"), |
webmaster@1
|
2348 "nr" => array("South Ndebele"), |
webmaster@1
|
2349 "nv" => array("Navajo"), |
webmaster@1
|
2350 "ny" => array("Chichewa"), |
webmaster@1
|
2351 "oc" => array("Occitan"), |
webmaster@1
|
2352 "om" => array("Oromo"), |
webmaster@1
|
2353 "or" => array("Oriya"), |
webmaster@1
|
2354 "os" => array("Ossetian"), |
webmaster@1
|
2355 "pa" => array("Punjabi"), |
webmaster@1
|
2356 "pi" => array("Pali"), |
webmaster@1
|
2357 "pl" => array("Polish", "Polski"), |
webmaster@1
|
2358 "ps" => array("Pashto", /* Left-to-right marker "" */ "پښتو", LANGUAGE_RTL), |
webmaster@1
|
2359 "pt" => array("Portuguese, Portugal", "Português"), |
webmaster@1
|
2360 "pt-br" => array("Portuguese, Brazil", "Português"), |
webmaster@1
|
2361 "qu" => array("Quechua"), |
webmaster@1
|
2362 "rm" => array("Rhaeto-Romance"), |
webmaster@1
|
2363 "rn" => array("Kirundi"), |
webmaster@1
|
2364 "ro" => array("Romanian", "Română"), |
webmaster@1
|
2365 "ru" => array("Russian", "Русский"), |
webmaster@1
|
2366 "rw" => array("Kinyarwanda"), |
webmaster@1
|
2367 "sa" => array("Sanskrit"), |
webmaster@1
|
2368 "sc" => array("Sardinian"), |
webmaster@1
|
2369 "sd" => array("Sindhi"), |
webmaster@1
|
2370 "se" => array("Northern Sami"), |
webmaster@1
|
2371 "sg" => array("Sango"), |
webmaster@1
|
2372 "sh" => array("Serbo-Croatian"), |
webmaster@1
|
2373 "si" => array("Singhalese"), |
webmaster@1
|
2374 "sk" => array("Slovak", "Slovenčina"), |
webmaster@1
|
2375 "sl" => array("Slovenian", "Slovenščina"), |
webmaster@1
|
2376 "sm" => array("Samoan"), |
webmaster@1
|
2377 "sn" => array("Shona"), |
webmaster@1
|
2378 "so" => array("Somali"), |
webmaster@1
|
2379 "sq" => array("Albanian", "Shqip"), |
webmaster@1
|
2380 "sr" => array("Serbian", "Српски"), |
webmaster@1
|
2381 "ss" => array("Siswati"), |
webmaster@1
|
2382 "st" => array("Sesotho"), |
webmaster@1
|
2383 "su" => array("Sudanese"), |
webmaster@1
|
2384 "sv" => array("Swedish", "Svenska"), |
webmaster@1
|
2385 "sw" => array("Swahili", "Kiswahili"), |
webmaster@1
|
2386 "ta" => array("Tamil", "தமிழ்"), |
webmaster@1
|
2387 "te" => array("Telugu", "తెలుగు"), |
webmaster@1
|
2388 "tg" => array("Tajik"), |
webmaster@1
|
2389 "th" => array("Thai", "ภาษาไทย"), |
webmaster@1
|
2390 "ti" => array("Tigrinya"), |
webmaster@1
|
2391 "tk" => array("Turkmen"), |
webmaster@1
|
2392 "tl" => array("Tagalog"), |
webmaster@1
|
2393 "tn" => array("Setswana"), |
webmaster@1
|
2394 "to" => array("Tonga"), |
webmaster@1
|
2395 "tr" => array("Turkish", "Türkçe"), |
webmaster@1
|
2396 "ts" => array("Tsonga"), |
webmaster@1
|
2397 "tt" => array("Tatar", "Tatarça"), |
webmaster@1
|
2398 "tw" => array("Twi"), |
webmaster@1
|
2399 "ty" => array("Tahitian"), |
webmaster@1
|
2400 "ug" => array("Uighur"), |
webmaster@1
|
2401 "uk" => array("Ukrainian", "Українська"), |
webmaster@1
|
2402 "ur" => array("Urdu", /* Left-to-right marker "" */ "اردو", LANGUAGE_RTL), |
webmaster@1
|
2403 "uz" => array("Uzbek", "o'zbek"), |
webmaster@1
|
2404 "ve" => array("Venda"), |
webmaster@1
|
2405 "vi" => array("Vietnamese", "Tiếng Việt"), |
webmaster@1
|
2406 "wo" => array("Wolof"), |
webmaster@1
|
2407 "xh" => array("Xhosa", "isiXhosa"), |
webmaster@1
|
2408 "yi" => array("Yiddish"), |
webmaster@1
|
2409 "yo" => array("Yoruba", "Yorùbá"), |
webmaster@1
|
2410 "za" => array("Zhuang"), |
webmaster@1
|
2411 "zh-hans" => array("Chinese, Simplified", "简体中文"), |
webmaster@1
|
2412 "zh-hant" => array("Chinese, Traditional", "繁體中文"), |
webmaster@1
|
2413 "zu" => array("Zulu", "isiZulu"), |
webmaster@1
|
2414 ); |
webmaster@1
|
2415 } |
webmaster@1
|
2416 /** |
webmaster@1
|
2417 * @} End of "locale-api-languages-predefined" |
webmaster@1
|
2418 */ |
webmaster@1
|
2419 |
webmaster@1
|
2420 /** |
webmaster@1
|
2421 * @defgroup locale-autoimport Automatic interface translation import |
webmaster@1
|
2422 * @{ |
webmaster@1
|
2423 */ |
webmaster@1
|
2424 |
webmaster@1
|
2425 /** |
webmaster@1
|
2426 * Prepare a batch to import translations for all enabled |
webmaster@1
|
2427 * modules in a given language. |
webmaster@1
|
2428 * |
webmaster@1
|
2429 * @param $langcode |
webmaster@1
|
2430 * Language code to import translations for. |
webmaster@1
|
2431 * @param $finished |
webmaster@1
|
2432 * Optional finished callback for the batch. |
webmaster@1
|
2433 * @param $skip |
webmaster@1
|
2434 * Array of component names to skip. Used in the installer for the |
webmaster@1
|
2435 * second pass import, when most components are already imported. |
webmaster@1
|
2436 * @return |
webmaster@1
|
2437 * A batch structure or FALSE if no files found. |
webmaster@1
|
2438 */ |
webmaster@1
|
2439 function locale_batch_by_language($langcode, $finished = NULL, $skip = array()) { |
webmaster@1
|
2440 // Collect all files to import for all enabled modules and themes. |
webmaster@1
|
2441 $files = array(); |
webmaster@1
|
2442 $components = array(); |
webmaster@1
|
2443 $query = "SELECT name, filename FROM {system} WHERE status = 1"; |
webmaster@1
|
2444 if (count($skip)) { |
webmaster@1
|
2445 $query .= " AND name NOT IN (". db_placeholders($skip, 'varchar') .")"; |
webmaster@1
|
2446 } |
webmaster@1
|
2447 $result = db_query($query, $skip); |
webmaster@1
|
2448 while ($component = db_fetch_object($result)) { |
webmaster@1
|
2449 // Collect all files for all components, names as $langcode.po or |
webmaster@1
|
2450 // with names ending with $langcode.po. This allows for filenames |
webmaster@1
|
2451 // like node-module.de.po to let translators use small files and |
webmaster@1
|
2452 // be able to import in smaller chunks. |
webmaster@1
|
2453 $files = array_merge($files, file_scan_directory(dirname($component->filename) .'/translations', '(^|\.)'. $langcode .'\.po$', array('.', '..', 'CVS'), 0, FALSE)); |
webmaster@1
|
2454 $components[] = $component->name; |
webmaster@1
|
2455 } |
webmaster@1
|
2456 |
webmaster@1
|
2457 return _locale_batch_build($files, $finished, $components); |
webmaster@1
|
2458 } |
webmaster@1
|
2459 |
webmaster@1
|
2460 /** |
webmaster@1
|
2461 * Prepare a batch to run when installing modules or enabling themes. |
webmaster@1
|
2462 * This batch will import translations for the newly added components |
webmaster@1
|
2463 * in all the languages already set up on the site. |
webmaster@1
|
2464 * |
webmaster@1
|
2465 * @param $components |
webmaster@1
|
2466 * An array of component (theme and/or module) names to import |
webmaster@1
|
2467 * translations for. |
webmaster@1
|
2468 * @param $finished |
webmaster@1
|
2469 * Optional finished callback for the batch. |
webmaster@1
|
2470 */ |
webmaster@1
|
2471 function locale_batch_by_component($components, $finished = '_locale_batch_system_finished') { |
webmaster@1
|
2472 $files = array(); |
webmaster@1
|
2473 $languages = language_list('enabled'); |
webmaster@1
|
2474 unset($languages[1]['en']); |
webmaster@1
|
2475 if (count($languages[1])) { |
webmaster@1
|
2476 $language_list = join('|', array_keys($languages[1])); |
webmaster@1
|
2477 // Collect all files to import for all $components. |
webmaster@1
|
2478 $result = db_query("SELECT name, filename FROM {system} WHERE status = 1"); |
webmaster@1
|
2479 while ($component = db_fetch_object($result)) { |
webmaster@1
|
2480 if (in_array($component->name, $components)) { |
webmaster@1
|
2481 // Collect all files for this component in all enabled languages, named |
webmaster@1
|
2482 // as $langcode.po or with names ending with $langcode.po. This allows |
webmaster@1
|
2483 // for filenames like node-module.de.po to let translators use small |
webmaster@1
|
2484 // files and be able to import in smaller chunks. |
webmaster@1
|
2485 $files = array_merge($files, file_scan_directory(dirname($component->filename) .'/translations', '(^|\.)('. $language_list .')\.po$', array('.', '..', 'CVS'), 0, FALSE)); |
webmaster@1
|
2486 } |
webmaster@1
|
2487 } |
webmaster@1
|
2488 return _locale_batch_build($files, $finished); |
webmaster@1
|
2489 } |
webmaster@1
|
2490 return FALSE; |
webmaster@1
|
2491 } |
webmaster@1
|
2492 |
webmaster@1
|
2493 /** |
webmaster@1
|
2494 * Build a locale batch from an array of files. |
webmaster@1
|
2495 * |
webmaster@1
|
2496 * @param $files |
webmaster@1
|
2497 * Array of files to import |
webmaster@1
|
2498 * @param $finished |
webmaster@1
|
2499 * Optional finished callback for the batch. |
webmaster@1
|
2500 * @param $components |
webmaster@1
|
2501 * Optional list of component names the batch covers. Used in the installer. |
webmaster@1
|
2502 * @return |
webmaster@1
|
2503 * A batch structure |
webmaster@1
|
2504 */ |
webmaster@1
|
2505 function _locale_batch_build($files, $finished = NULL, $components = array()) { |
webmaster@1
|
2506 $t = get_t(); |
webmaster@1
|
2507 if (count($files)) { |
webmaster@1
|
2508 $operations = array(); |
webmaster@1
|
2509 foreach ($files as $file) { |
webmaster@1
|
2510 // We call _locale_batch_import for every batch operation. |
webmaster@1
|
2511 $operations[] = array('_locale_batch_import', array($file->filename)); } |
webmaster@1
|
2512 $batch = array( |
webmaster@1
|
2513 'operations' => $operations, |
webmaster@1
|
2514 'title' => $t('Importing interface translations'), |
webmaster@1
|
2515 'init_message' => $t('Starting import'), |
webmaster@1
|
2516 'error_message' => $t('Error importing interface translations'), |
webmaster@1
|
2517 'file' => './includes/locale.inc', |
webmaster@1
|
2518 // This is not a batch API construct, but data passed along to the |
webmaster@1
|
2519 // installer, so we know what did we import already. |
webmaster@1
|
2520 '#components' => $components, |
webmaster@1
|
2521 ); |
webmaster@1
|
2522 if (isset($finished)) { |
webmaster@1
|
2523 $batch['finished'] = $finished; |
webmaster@1
|
2524 } |
webmaster@1
|
2525 return $batch; |
webmaster@1
|
2526 } |
webmaster@1
|
2527 return FALSE; |
webmaster@1
|
2528 } |
webmaster@1
|
2529 |
webmaster@1
|
2530 /** |
webmaster@1
|
2531 * Perform interface translation import as a batch step. |
webmaster@1
|
2532 * |
webmaster@1
|
2533 * @param $filepath |
webmaster@1
|
2534 * Path to a file to import. |
webmaster@1
|
2535 * @param $results |
webmaster@1
|
2536 * Contains a list of files imported. |
webmaster@1
|
2537 */ |
webmaster@1
|
2538 function _locale_batch_import($filepath, &$context) { |
webmaster@1
|
2539 // The filename is either {langcode}.po or {prefix}.{langcode}.po, so |
webmaster@1
|
2540 // we can extract the language code to use for the import from the end. |
webmaster@1
|
2541 if (preg_match('!(/|\.)([^\./]+)\.po$!', $filepath, $langcode)) { |
webmaster@1
|
2542 $file = (object) array('filename' => basename($filepath), 'filepath' => $filepath); |
webmaster@1
|
2543 _locale_import_read_po('db-store', $file, LOCALE_IMPORT_KEEP, $langcode[2]); |
webmaster@1
|
2544 $context['results'][] = $filepath; |
webmaster@1
|
2545 } |
webmaster@1
|
2546 } |
webmaster@1
|
2547 |
webmaster@1
|
2548 /** |
webmaster@1
|
2549 * Finished callback of system page locale import batch. |
webmaster@1
|
2550 * Inform the user of translation files imported. |
webmaster@1
|
2551 */ |
webmaster@1
|
2552 function _locale_batch_system_finished($success, $results) { |
webmaster@1
|
2553 if ($success) { |
webmaster@1
|
2554 drupal_set_message(format_plural(count($results), 'One translation file imported for the newly installed modules.', '@count translation files imported for the newly installed modules.')); |
webmaster@1
|
2555 } |
webmaster@1
|
2556 } |
webmaster@1
|
2557 |
webmaster@1
|
2558 /** |
webmaster@1
|
2559 * Finished callback of language addition locale import batch. |
webmaster@1
|
2560 * Inform the user of translation files imported. |
webmaster@1
|
2561 */ |
webmaster@1
|
2562 function _locale_batch_language_finished($success, $results) { |
webmaster@1
|
2563 if ($success) { |
webmaster@1
|
2564 drupal_set_message(format_plural(count($results), 'One translation file imported for the enabled modules.', '@count translation files imported for the enabled modules.')); |
webmaster@1
|
2565 } |
webmaster@1
|
2566 } |
webmaster@1
|
2567 |
webmaster@1
|
2568 /** |
webmaster@1
|
2569 * @} End of "locale-autoimport" |
webmaster@1
|
2570 */ |