Drupal how to update language translations?

Posted by: 
Dominique De Cooman

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 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.

Comments

Drupal how to update language translations?

Or you could use the localization client. It enables you to reimport all your translations:

http://drupal.org/project/l10n_client

From the project page:

A translation package reimport tool, which helps people upgrading from Drupal 5 and those upgrading their translation packages anytime onwards, to import new and changed translations. (Only Drupal 6.x, look for similar functionality in Drupal 5 to Autolocale)

Drupal how to update language translations?

Looks like overkill to install the localization client just to be able to update translations. You would expect this feature to be in core.

Drupal how to update language translations?

Other alternative how to update.

$ curl -s http://ftp.drupal.org/files/projects/es-6.x-1.0.tar.gz | tar xzOf - --wildcards modules/* profiles/* themes/* > es.po

Upload the new es.po file to your Drupal 6 install at admin/build/translate/import and let it overwrite the current translation.

Add new comment