Drupal - How to export translated variables settings in code?
Update: With recent version of the Variable translation module (ie. 7.x-1.7), the list of translated variables is stored by the language
variable realm controller in its list
variable. The following code demonstrate how to use the realm controller to set translated variables from a module hook_enable()
implementation.
function MODULE_enable() {
// Get the language variable controller.
$controller = variable_realm_controller('language');
// The list of translatable variables.
$translatable_variables = $controller->getEnabledVariables();
// The list of variables that could be translated.
$available_variables = $controller->getAvailableVariables();
// Add our variables to $translatable_variables.
foreach ($available_variables as $name) {
// Translate user mail messages.
if (preg_match('/^user_mail_[a-zA-Z_\[\]]*\[mail_part\]$/', $name)) {
$translatable_variables[] = $name;
}
}
$translatable_variables = array_merge($translatable_variables, array(
'user_registration_help',
'user_picture_guidelines',
'maintenance_mode_message',
'menu_main_links_source',
'menu_secondary_links_source',
));
// Save the updated list of translatable variables.
$controller->setRealmVariable('list', array_unique($translatable_variables));
}
Update: This is not the way to do it, unless you are using an old version of Variable translation module (< 7.x-1.7)
The names of the translated variables are stored in the i18n_variable_conf
variable. This variable itself cannot be exported using Strongarm (or similar solution). I ended using an old trick: checking the variable in a hook_init
implementation and setting it if needed. The array in the foreach controller contains the names of the variables to be translated.
/**
* Implements hook_init();
*/
function MODULE_init() {
$i18n_variable_conf = variable_get('i18n_variable_conf', array());
$i18n_variable_conf_updated = FALSE;
foreach(array(...) as $name) {
if (!in_array($name, $i18n_variable_conf)) {
$i18n_variable_conf[] = $name;
$i18n_variable_conf_updated = TRUE;
}
}
if ($i18n_variable_conf_updated) {
variable_set('i18n_variable_conf', $i18n_variable_conf);
variable_set('i18n_variable_list', variable_children($i18n_variable_conf));
cache_clear_all('*', I18N_VARIABLE_CACHE, TRUE);
}
}
Compared to a feature based solution, this enforce the settings for the configured variables and will cancel any manual edit. This may wanted or not wanted. In my case it was.
My only issue with this solution, is that it had some overhead to each request.