-
Notifications
You must be signed in to change notification settings - Fork 2
/
l10n_update.admin.inc
570 lines (521 loc) · 21.3 KB
/
l10n_update.admin.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
<?php
/**
* @file
* Admin settings and update page.
*/
/**
* Page callback: Checks for translation updates and displays the status.
*
* Manually checks the translation status without the use of cron.
*/
function l10n_update_manual_status() {
module_load_include('compare.inc', 'l10n_update');
// Check the translation status of all translatable projects in all languages.
// First we clear the cached list of projects. Although not strictly
// necessary, this is helpful in case the project list is out of sync.
l10n_update_flush_projects();
l10n_update_check_projects();
// Execute a batch if required. A batch is only used when remote files
// are checked.
if (batch_get()) {
batch_process('admin/config/regional/translate/update');
}
backdrop_goto('admin/config/regional/translate/update');
}
/**
* Page callback: Translation status page.
*/
function l10n_update_status_form() {
module_load_include('compare.inc', 'l10n_update');
$updates = $options = array();
$languages_update = $languages_not_found = array();
$projects_update = array();
// @todo Calling l10n_update_build_projects() is an expensive way to
// get a module name. In follow-up issue https://www.drupal.org/node/1842362
// the project name will be stored to display use, like here.
$project_data = l10n_update_build_projects();
$languages = l10n_update_translatable_language_list();
$status = l10n_update_get_status();
// Prepare information about projects which have available translation
// updates.
if ($languages && $status) {
foreach ($status as $project) {
foreach ($project as $langcode => $project_info) {
if (isset($project_data[$project_info->name])) {
// No translation file found for this project-language combination.
if (empty($project_info->type)) {
$updates[$langcode]['not_found'][] = array(
'name' => $project_info->name == 'backdrop' ? t('Backdrop CMS core') : $project_data[$project_info->name]->info['name'],
'version' => $project_info->version,
'info' => _l10n_update_status_debug_info($project_info),
);
$languages_not_found[$langcode] = $langcode;
}
// Translation update found for this project-language combination.
elseif ($project_info->type == L10N_UPDATE_LOCAL || $project_info->type == L10N_UPDATE_REMOTE) {
$local = isset($project_info->files[L10N_UPDATE_LOCAL]) ? $project_info->files[L10N_UPDATE_LOCAL] : NULL;
$remote = isset($project_info->files[L10N_UPDATE_REMOTE]) ? $project_info->files[L10N_UPDATE_REMOTE] : NULL;
$recent = _l10n_update_source_compare($local, $remote) == L10N_UPDATE_SOURCE_COMPARE_LT ? $remote : $local;
$name = isset($project_data[$project_info->name]->info['name']) ? $project_data[$project_info->name]->info['name'] : '';
$updates[$langcode]['updates'][] = array(
'name' => $project_info->name == 'backdrop' ? t('Backdrop CMS core') : $name,
'version' => $project_info->version,
'timestamp' => $recent->timestamp,
);
$languages_update[$langcode] = $langcode;
$projects_update[$project_info->name] = $project_info->name;
}
}
}
}
$languages_not_found = array_diff($languages_not_found, $languages_update);
// Build data options for the select table.
foreach ($updates as $langcode => $update) {
$title = check_plain($languages[$langcode]);
$l10n_update_update_info = array('#theme' => 'l10n_update_update_info');
foreach (array('updates', 'not_found') as $update_status) {
if (isset($update[$update_status])) {
$l10n_update_update_info['#' . $update_status] = $update[$update_status];
}
}
$options[$langcode] = array(
'title' => array(
'class' => array('label'),
'data' => array(
'#title' => $title,
'#markup' => $title,
),
),
'status' => array(
'class' => array(
'description',
'expand',
'priority-low',
),
'data' => backdrop_render($l10n_update_update_info),
),
);
}
// Sort the table data on language name.
uasort($options, function ($a, $b) {
return strcasecmp($a['title']['data']['#title'], $b['title']['data']['#title']);
});
}
$last_checked = state_get('l10n_update_last_check');
$output = t('Status of interface translations for each of the enabled languages.') . ' ';
$output .= t('If there are available updates you can click on "Update translation" for them to be downloaded and imported now or you can edit the configuration for them to be updated automatically on the <a href="@update-settings">Update settings page</a>', array('@update-settings' => url('admin/config/regional/language/update')));
$form['help'] = array(
'#type' => 'help',
'#markup' => $output,
);
$form['last_checked'] = array(
'#theme' => 'l10n_update_last_check',
'#last' => $last_checked,
);
$header = array(
'title' => array(
'data' => t('Language'),
'class' => array('title'),
),
'status' => array(
'data' => t('Status'),
'class' => array('status', 'priority-low'),
),
);
if (!$languages) {
$empty = t('No translatable languages available. <a href="@add_language">Add a language</a> first.', array('@add_language' => url('admin/config/regional/language')));
}
elseif (empty($options)) {
$empty = t('All translations up to date.');
}
else {
$empty = t('No translation status available. <a href="@check">Check manually</a>.', array('@check' => url('admin/config/regional/translate/check')));
}
// The projects which require an update. Used by the _submit callback.
$form['projects_update'] = array(
'#type' => 'value',
'#value' => $projects_update,
);
$form['langcodes'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#default_value' => $languages_update,
'#empty' => $empty,
'#js_select' => TRUE,
'#multiple' => TRUE,
'#required' => TRUE,
'#not_found' => $languages_not_found,
'#after_build' => array('l10n_update_language_table'),
'#attributes' => array(),
);
$form['#attached'] = array(
'js' => array(
backdrop_get_path('module', 'l10n_update') . '/js/l10n_update.admin.js',
),
'css' => array(
backdrop_get_path('module', 'l10n_update') . '/css/l10n_update.admin.css',
),
);
if ($languages_update) {
$form['actions'] = array(
'#type' => 'actions',
'submit' => array(
'#type' => 'submit',
'#value' => t('Update translations'),
),
'#attributes' => array(),
);
}
return $form;
}
/**
* Form validation handler for locale_translation_status_form().
*/
function l10n_update_status_form_validate($form, &$form_state) {
// Check if a language has been selected. 'tableselect' doesn't.
if (!array_filter($form_state['values']['langcodes'])) {
form_set_error('', t('Select a language to update.'));
}
}
/**
* Form submission handler for locale_translation_status_form().
*/
function l10n_update_status_form_submit($form, $form_state) {
module_load_include('fetch.inc', 'l10n_update');
$langcodes = array_filter($form_state['values']['langcodes']);
$projects = array_filter($form_state['values']['projects_update']);
// Set the translation import options. This determines if existing
// translations will be overwritten by imported strings.
$options = _l10n_update_default_update_options();
// If the status was updated recently we can immediately start fetching the
// translation updates. If the status is expired we clear it an run a batch to
// update the status and then fetch the translation updates.
$last_checked = state_get('l10n_update_last_check');
if ($last_checked < REQUEST_TIME - L10N_UPDATE_STATUS_TTL) {
l10n_update_clear_status();
$batch = l10n_update_batch_update_build(array(), $langcodes, $options);
batch_set($batch);
}
else {
$batch = l10n_update_batch_fetch_build($projects, $langcodes, $options);
batch_set($batch);
}
}
/**
* Page callback: Settings form.
*/
function l10n_update_admin_settings_form($form, &$form_state) {
require_once BACKDROP_ROOT . '/core/includes/locale.inc';
$output = t('These are the settings for the translation update system. To update your translations now, check out the <a href="@update-admin">Translation update administration page</a>.', array('@update-admin' => url('admin/config/regional/translate/update')));
$config = config('l10n_update.settings');
$form['#config'] = 'l10n_update.settings';
$form['help'] = array(
'#type' => 'help',
'#markup' => $output,
);
$form['l10n_update_check_frequency'] = array(
'#type' => 'radios',
'#title' => t('Check for updates'),
'#default_value' => $config->get('l10n_update_check_frequency'),
'#options' => array(
'0' => t('Never (manually)'),
'7' => t('Weekly'),
'30' => t('Monthly'),
),
'#description' => t('Select how frequently you want to check for new interface translations for your currently installed modules and themes. <a href="@url">Check updates now</a>.', array('@url' => url('admin/config/regional/translate/check'))),
);
$form['l10n_update_check_disabled'] = array(
'#type' => 'checkbox',
'#title' => t('Check for updates of disabled modules and themes'),
'#default_value' => $config->get('l10n_update_check_disabled'),
);
$form['l10n_update_check_mode'] = array(
'#type' => 'radios',
'#title' => t('Translation source'),
'#default_value' => $config->get('l10n_update_check_mode'),
'#options' => array(
L10N_UPDATE_USE_SOURCE_REMOTE_AND_LOCAL => t('Backdrop CMS translation server and local files'),
L10N_UPDATE_USE_SOURCE_LOCAL => t('Local files only'),
),
'#description' => t('The source of translation files for automatic interface translation.'),
);
$form['l10n_update_download_store'] = array(
'#title' => t('Translations directory'),
'#type' => 'textfield',
'#default_value' => $config->get('l10n_update_download_store'),
'#required' => TRUE,
'#description' => t('A path relative to the Backdrop CMS installation directory where translation files will be stored, e.g. translations. Saved translation files can be reused by other installations.'),
);
$form['l10n_update_import_mode'] = array(
'#type' => 'radios',
'#title' => t('Import behaviour'),
'#default_value' => $config->get('l10n_update_import_mode'),
'#options' => array(
LOCALE_IMPORT_KEEP => t("Don't overwrite existing translations."),
L10N_UPDATE_OVERWRITE_NON_CUSTOMIZED => t('Only overwrite imported translations, customized translations are kept.'),
LOCALE_IMPORT_OVERWRITE => t('Overwrite existing translations.'),
),
'#description' => t('How to treat existing translations when automatically updating the interface translations.'),
);
$form['disable_update'] = array(
'#type' => 'fieldset',
'#title' => t('Disable update'),
'#collapible' => FALSE,
'#collapsed' => FALSE,
);
$form['disable_update']['disabled_projects'] = array(
'#type' => 'textarea',
'#title' => t('Projects'),
'#default_value' => implode(PHP_EOL, $config->get('l10n_update_disabled_projects')),
'#description' => t("These modules, themes or profiles will not receive interface translation updates. Specify them by there machine name, enter one name per line. The '*' character is a wildcard. Use for example feature_* for every feature."),
);
$languages = language_list(TRUE, TRUE);
unset($languages['en']);
$form['disable_update']['l10n_update_disabled_languages'] = array(
'#type' => 'checkboxes',
'#title' => t('Languages'),
'#options' => $languages,
'#default_value' => $config->get('l10n_update_disabled_languages'),
'#description' => t('The selected languages will not receive interface translation updates.'),
);
$form = system_settings_form($form);
$form['#submit'][] = 'l10n_update_admin_settings_form_submit';
return $form;
}
/**
* Validation handler for translation update settings.
*/
function l10n_update_admin_settings_form_validate($form, &$form_state) {
// Check for existing translations directory and create one if required.
// When using local sources, only check if the directory exists.
$directory = $form_state['values']['l10n_update_download_store'];
$directory = rtrim($directory, '/\\');
if ($form_state['values']['l10n_update_check_mode'] == L10N_UPDATE_USE_SOURCE_LOCAL
&& is_dir($directory)) {
return;
}
if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
form_set_error('l10n_update_download_store', t('The directory %directory does not exist or is not writable.', array('%directory' => $directory)));
watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $directory), WATCHDOG_ERROR);
}
}
/**
* Submit handler for translation update settings.
*/
function l10n_update_admin_settings_form_submit($form, &$form_state) {
// Invalidate the cached translation status when the configuration setting of
// 'l10n_update_check_mode' or 'check_disabled' change.
if ($form['l10n_update_check_mode']['#default_value'] != $form_state['values']['l10n_update_check_mode'] ||
$form['l10n_update_check_disabled']['#default_value'] != $form_state['values']['l10n_update_check_disabled']) {
l10n_update_clear_status();
}
// Convert the disabled projects input into an array value. The input value is
// removed from the form_state values to prevent it being turned into a
// variable.
$input = $form_state['values']['disabled_projects'];
unset($form_state['values']['disabled_projects']);
$input = strtr($input, array("\r" => '', ' ' => ''));
$values = array_filter(explode("\n", $input));
config_set('l10n_update.settings', 'l10n_update_disabled_projects', $values);
// Add .htaccess file to the translations directory.
l10n_update_ensure_htaccess();
}
/**
* Get array of import options.
*
* The import options of the Locale module are used but the UI text is altered
* to suit the Localization update cases.
*
* @return array
* Keyed array of import options.
*/
function _l10n_update_admin_import_options() {
require_once BACKDROP_ROOT . '/core/includes/locale.inc';
return array(
LOCALE_IMPORT_OVERWRITE => t('Translation updates replace existing ones, new ones are added'),
L10N_UPDATE_OVERWRITE_NON_CUSTOMIZED => t('Edited translations are kept, only previously imported ones are overwritten and new translations are added'),
LOCALE_IMPORT_KEEP => t('All existing translations are kept, only new translations are added.'),
);
}
/**
* Provides debug info for projects in case translation files are not found.
*
* Translations files are being fetched either from Backdrop CMS translation
* server and local files or only from the local filesystem depending on the
* "Translation source" setting at admin/config/regional/language/update.
* This method will produce debug information including the respective path(s)
* based on this setting.
*
* Translations for development versions are never fetched, so the debug info
* for that is a fixed message.
*
* @param object $source
* An object which is the project information of the source.
*
* @return string
* The string which contains debug information.
*/
function _l10n_update_status_debug_info($source) {
$remote_path = isset($source->files['remote']->uri) ? $source->files['remote']->uri : '';
$local_path = isset($source->files['local']->uri) ? $source->files['local']->uri : '';
if (strpos($source->version, 'dev') !== FALSE) {
return t('No translation files are provided for development releases.');
}
if (l10n_update_use_remote_source() && $remote_path && $local_path) {
return t('File not found at %remote_path nor at %local_path', array(
'%remote_path' => $remote_path,
'%local_path' => $local_path,
));
}
elseif ($local_path) {
return t('File not found at %local_path', array('%local_path' => $local_path));
}
return t('Translation file location could not be determined.');
}
/**
* Form element callback: After build changes to the language update table.
*
* Adds labels to the languages and removes checkboxes from languages from which
* translation files could not be found.
*/
function l10n_update_language_table($form_element) {
// Remove checkboxes of languages without updates.
if ($form_element['#not_found']) {
foreach ($form_element['#not_found'] as $langcode) {
$form_element[$langcode] = array();
}
}
return $form_element;
}
/**
* Returns HTML for translation edit form.
*
* @param array $variables
* An associative array containing:
* - form: The form that contains the language information.
*
* @return string
* HTML output.
*
* @see l10n_update_edit_form()
*
* @ingroup themeable
*/
function theme_l10n_update_edit_form_strings(array $variables) {
$output = '';
$form = $variables['form'];
$header = array(
t('Source string'),
t('Translation for @language', array('@language' => $form['#language'])),
);
$rows = array();
foreach (element_children($form) as $lid) {
$string = $form[$lid];
if ($string['plural']['#value']) {
$source = backdrop_render($string['original_singular']) . '<br />' . backdrop_render($string['original_plural']);
}
else {
$source = backdrop_render($string['original']);
}
$source .= empty($string['context']) ? '' : '<br /><small>' . t('In Context') . ': ' . $string['context']['#value'] . '</small>';
$rows[] = array(
array('data' => $source),
array('data' => $string['translations']),
);
}
$table = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No strings available.'),
'#attributes' => array('class' => array('locale-translate-edit-table')),
);
$output .= backdrop_render($table);
$pager = array('#theme' => 'pager');
$output .= backdrop_render($pager);
return $output;
}
/**
* Prepares variables for translation status information templates.
*
* Translation status information is displayed per language.
*
* Default template: l10n_update-translation-update-info.tpl.php.
*
* @param array $variables
* An associative array containing:
* - updates: The projects which have updates.
* - not_found: The projects which updates are not found.
*
* @see l10n_update_status_form()
*/
function template_preprocess_l10n_update_update_info(array &$variables) {
$details = array();
$modules = array();
// Default values.
$variables['modules'] = array();
$variables['module_list'] = '';
$details['available_updates_list'] = array();
// Build output for available updates.
if (isset($variables['updates'])) {
$releases = array();
if ($variables['updates']) {
foreach ($variables['updates'] as $update) {
$modules[] = $update['name'];
$releases[] = t('@module (@date)', array(
'@module' => $update['name'],
'@date' => format_date($update['timestamp'], 'html_date'),
));
}
$variables['modules'] = $modules;
$variables['module_list'] = t('Updates for: @modules', array('@modules' => implode(', ', $modules)));
}
$details['available_updates_list'] = array(
'#theme' => 'item_list',
'#items' => $releases,
);
}
// Build output for updates not found.
if (isset($variables['not_found'])) {
$releases = array();
$variables['missing_updates_status'] = format_plural(count($variables['not_found']), 'Missing translations for one project', 'Missing translations for @count projects');
if ($variables['not_found']) {
foreach ($variables['not_found'] as $update) {
$version = $update['version'] ? $update['version'] : t('no version');
$releases[] = t('@module (@version).', array('@module' => $update['name'], '@version' => $version)) . ' ' . $update['info'];
}
}
$details['missing_updates_list'] = array(
'#theme' => 'item_list',
'#items' => $releases,
);
// Prefix the missing updates list if there is an available updates lists
// before it.
if (!empty($details['missing_updates_list']['#items'])) {
$details['missing_updates_list']['#prefix'] = t('Missing translations for:');
}
}
$variables['details'] = $details;
}
/**
* Prepares variables for most recent translation update templates.
*
* Displays the last time we checked for locale update data. In addition to
* properly formatting the given timestamp, this function also provides a "Check
* manually" link that refreshes the available update and redirects back to the
* same page.
*
* Default template: l10n_update-translation-last-check.tpl.php.
*
* @param array $variables
* An associative array containing:
* - last: The timestamp when the site last checked for available updates.
*
* @see l10n_update_status_form()
*/
function template_preprocess_l10n_update_last_check(array &$variables) {
$last = $variables['last'];
$variables['last_checked'] = $last ? t('Last checked: !time ago', array('!time' => format_interval(REQUEST_TIME - $last))) : t('Last checked: never');
$variables['link'] = l(t('Check manually'), 'admin/config/regional/translate/check');
}