How can you make a variable translatable by default when you declare it with hook_variable_info()? I.e make the check box next to the variable name be checked when viewing admin/config/regional/i18n/variable.

Thanks!

Comments

bforchhammer’s picture

Variable variable_realm_list_language stores the list of variables which can be translated... you could try set that variable at install-time (hook_install of custom module, or somewhere in your install profile?).

jmlavarenne’s picture

Thanks for your reply. I will look for it. Is this a variable? I'm not finding it in the {variable} table.

mrfelton’s picture

Wouldn't it be better if this was based on a property of the variable? Sich as the 'localize' key which seems to be unused at present. That would mean that would be easy for other modules to alter the list of variables that is translatable by implementing hook_variable_info_alter().

jmlavarenne’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

lklimek’s picture

For anyone who needs a solution with hook_variable_info_alter() - it's not good to just check 'localize' flag because in effect you will get all vars translated (realm list contains > 30 vars in my case). So I've introduced another parameter - 'localize_by_default'.

Code sample below.

function MYMODULE_variable_info_alter(&$vars) {
  $realm = variable_get('variable_realm_list_language', array());

  foreach ($vars as $name => $value) {
    if (isset($value['localize_by_default']) && $value['localize_by_default']) {
      if (!in_array($name, $realm))
        $realm []= $name;
    }
  }

  variable_set('variable_realm_list_language', $realm);
}
doublejosh’s picture

Issue summary: View changes

@lklimek Thank you! Very clever.

Rather that setting the variable via code in each pass through PHP, I might recommend setting them in a hook_install() or hook_update() so they can be cached.

function my_module_install() {
  $vars = variable_list_module('my_module');
  $realm = variable_get('variable_realm_list_language', array());
  foreach ($vars as $name => $value) {
    if (isset($value['localize']) && $value['localize']) {
      if (!in_array($name, $realm)) {
        $realm []= $name;
      }
    }
  }
  variable_set('variable_realm_list_language', $realm);
}
doublejosh’s picture

Version: 7.x-1.0 » 7.x-2.x-dev
Category: Support request » Feature request
Status: Closed (fixed) » Needs work

This really needs to be a variable property not done in an alter. Seems like a reasonable feature request.