Last active
August 29, 2015 14:04
-
-
Save plopesc/71b3dd91d99ec48888eb to your computer and use it in GitHub Desktop.
Enables multilingual support for variables defined in a group.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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