Make your Drupal module’s variables multilingual

If you’re writing (or using) a module that stores information in Drupal’s variable table there might be the need to change the values of your variables depending on the language your site is currently viewed in.

To do so you’ll need i18n and its submodule Variable Translation enabled. To make a variable multilingual you typically head over to admin/config/regional/i18n/variable, check the desired variable and save your settings. But, what if your module’s variable doesn’t show up here? That means your module didn’t implement hook_variable_info, provided by the Variable Module. Luckily it really is a piece of cake
implementing it. Just create a file called inside the module’s root directory. You could also put it in the main module file, but having a separate file is the cleaner approach. The correctly named include file will be loaded automatically.

Here’s my example, where I needed the variable gtranslate_main_lang of GTranslate module to become a multilingual one:

 * Implements hook_variable_info().
function gtranslate_variable_info($options) {
  $variable['gtranslate_main_lang'] = array(
    'title' => t('GTranslate Main Language'),
    'description' => t('Main language for GTranslate functionality'),
    'localize' => TRUE
  return $variable;

To make the variable i18n-aware it’s important to add 'localize' => TRUE.

Flush the caches and check that newly appeared checkbox on the multilingual variable’s configuration page.

I hope I don’t need to mention that it’s generally a bad idea to start hacking on contrib modules, so for your own sake consider providing a patch to the module’s issue queue and ask if it can be committed.

Leave a Reply