How to update translations with drupal? The core only imports translations when you enable the language. It does not support updating languages.
We all know how to import language translations in the first place. We go to http://drupal.org/project/Translations [7] and pick our translation and we copy the files onto our drupal core.
Then we go to admin/settings/language/add and add a language. When we add a language drupal detects and imports all these packages. So no need to go add import them one by one.
Now we want to update our translations? We import them one by one? I dont think so. Here is a snippet that you could use to update your languages.
It makes use of the same function the locale module uses to import your translations. Existing translations will not be overwritten.
Here is the code:
Define a menu hook, build a form displaying your enabled languages and build a batch which will import all locale files. That's it.
<?php
/**
* Implementation hook_menu()
*/
function language_update_menu() {
$items = array();
$items['admin/settings/language_update'] = array(
'title' => 'Update language',
'page callback' => 'drupal_get_form',
'page arguments' => array('language_update_languages_update_form'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Form with languages to select for updating
*/
function language_update_languages_update_form() {
include_once 'includes/locale.inc';
$predefined = language_update_prepare_predefined_list();
$form = array();
$form['language list']['langcode'] = array('#type' => 'select',
'#title' => t('Language name'),
'#default_value' => key($predefined),
'#options' => $predefined,
'#description' => t('Select the desired language and click the <em>Update language</em> button.'),
);
$form['language list']['submit'] = array('#type' => 'submit', '#value' => t('Update language'));
return $form;
}
/**
* Submit function
*/
function language_update_languages_update_form_submit($form, $form_values) {
if ($batch = locale_batch_by_language($form_values['values']['langcode'], '_locale_batch_language_finished')) {
batch_set($batch);
}
}
/**
* Prepares the language code list for a select form item with only the unsupported ones
*/
function language_update_prepare_predefined_list() {
$languages = language_list('enabled');
$predefined = _locale_get_predefined_list();
foreach ($predefined as $key => $value) {
if (!isset($languages[1][$key]) || $key == 'en') {
unset($predefined[$key]);
continue;
}
// Include native name in output, if possible
if (count($value) > 1) {
$tname = t($value[0]);
$predefined[$key] = ($tname == $value[1]) ? $tname : "$tname ($value[1])";
}
else {
$predefined[$key] = t($value[0]);
}
}
asort($predefined);
return $predefined;
}
?>
You can download the snippet as a module. The module is too small to contribute. It should become a feature in the core locale module.
Links
[1] https://dominiquedecooman.com/blog-topics/locale
[2] https://dominiquedecooman.com/blog-topics/translations
[3] https://dominiquedecooman.com/blog-topics/international
[4] https://dominiquedecooman.com/blog-topics/i18n
[5] https://dominiquedecooman.com/blog-topics/language
[6] https://dominiquedecooman.com/sites/default/files/language_update.rar
[7] http://drupal.org/project/Translations