Skip to content

Instantly share code, notes, and snippets.

@plopesc
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plopesc/71b3dd91d99ec48888eb to your computer and use it in GitHub Desktop.
Save plopesc/71b3dd91d99ec48888eb to your computer and use it in GitHub Desktop.
Enables multilingual support for variables defined in a group.
<?php
/**
* Enables multilingual support for variables defined in a group.
*
* @param string $group
* The group of variables to load
*/
function enable_multilingual_variables($group) {
// Load all the variables defined by group.
$variables = variable_list_group($group);
// Enable multilingual support adding those variables to Language realm.
$variable_names = array_keys($variables);
$controller = variable_realm_controller('language');
$controller->setRealmVariable('list', $variable_names);
// Set default value as English translation to avoid inconsistencies.
foreach ($variables as $name => $variable) {
variable_realm_set('language', 'en', $name, $variable['default']);
}
}
/**
* Translates variables given an name-value array.
*
* @param array $variables
* Array containing translations keyed by variable name.
* @param string $language
* Language code of the translated variables.
*/
function translate_variables($variables, $language) {
// Enable multilingual support adding those variables to Language realm.
$variable_names = array_keys($variables);
$controller = variable_realm_controller('language');
$controller->setRealmVariable('list', $variable_names);
// Set default value as English translation to avoid inconsistencies.
foreach ($variables as $name => $variable) {
$info = variable_get_info($name);
variable_realm_set('language', 'en', $name, $info['default']);
}
// Add the translation.
foreach ($variables as $name => $variable) {
variable_realm_set('language', $language, $name, $variable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment