3

When using the Variable translation module (part of the Internationalization suite), how can I set the value of a translated variable from the site's settings.php file ?

Using the $conf in the settings.php works, but off course it can only be used when the value of the variable is the same in all languages.

I'm looking for a solution that could be used to achieve the same goal of the $conf in the settings.php file: defining the values of variable at execution without the need store the values anywhere else. One use case is to assign different values to variables depending on the execution environment (as set in an environment variable).

  • Any reason you specifically want to use settings.php for this? – rooby Mar 15 '14 at 7:33
  • I want the value of the variables to be set (and kept) from code. But it does not have to be done from settings.php. Ideally, changing the code would directly change the value of the variables, without any additional step. – Pierre Buyle Mar 15 '14 at 16:54
2

In settings.php

$conf['MODULE_i18n_variables'] = array('my_variable');
$conf['my_variable'] = 'default value';
$conf['my_variable__fr'] = 'french value';

And in a custom module

/**
 * Implements hook_init().
 */
function MODULE_init() {
  global $conf, $language;
  if (is_array($conf['MODULE_i18n_variables'])) {
    foreach ($conf['MODULE_i18n_variables'] as $variable) {
      if (isset($conf["{$variable}__{$language->language}"])) {
        $conf[$variable] = $conf["{$variable}__{$language->language}"];
      }
    }
  }
}

This work as long as other languages values are not used. For instance, a multi-language aware edit form for the variable will not reflect the enforced per language values.

2

Why don't you declare your variables inside a module with the variable module. Then you could use and translated them.

I suggest you read this: http://www.lullabot.com/blog/article/module-monday-variable

I don't think there is a way of having things translated in drupal without using the database, so i give you a simple option of using variable module as the only way.

you can add in your module.info file, so it will work as you enable your module.

/**
 * Implements hook_install().
 */
function mymo_install() {

  // save the translation of your variable
  variable_store_set('language', 'es', 'site_name', 'Este es mi Sitio', TRUE);

}

you could delete things with hook_uninstall if you want.

hope i don't get extra -1, it could be useful to someone else...

  • I'm using variable translation to allow the variable to be translated, which requires declaring them. I can translate the variable using the UI. What I want and need is a way to translate the variable from code, just like you can set the variable from code in settings.php (ie. without actually storing the value somewhere else than in code, like you would with variable_realm_set()). – Pierre Buyle Mar 20 '14 at 20:16
  • jus updated with an example. – angoru Mar 21 '14 at 7:15
  • With the added usage of variable_store_set() in a hook_install(), the answer provide a usable solution. My main concern is that, on my projects, it add a third places where variables are set. I'm already using Strongarm (via Features) for variables that always have the same values (for instance: node_options_page) and variables whose values depend of the runtime environment (for instance: 'memcache_key_prefix'). I would prefer to be able to keep environment depended values in a single place (ie. settings.php). – Pierre Buyle Mar 21 '14 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.