Skip to content
Snippets Groups Projects
Unverified Commit 840f2242 authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Add a "translateFromArray" filter to twig.

This allows us to use this new filter to translate strings from a given array of translations, where every translation is indexed by its ISO 639 code. A new configuration option ('language' -> 'priorities') is available too to control the alternative languages that can be used instead of a given language, when the latter is not found. The filter returns null when no suitable translation is found, so that it can be combined with "default()" to set a default translation for a given string.
parent aa6999bf
No related branches found
No related tags found
No related merge requests found
...@@ -655,6 +655,38 @@ $config = array( ...@@ -655,6 +655,38 @@ $config = array(
| LANGUAGE AND INTERNATIONALIZATION | | LANGUAGE AND INTERNATIONALIZATION |
*************************************/ *************************************/
/*
* Language-related options.
*/
'language' => array(
/*
* An array in the form 'language' => <list of alternative languages>.
*
* Each key in the array is the ISO 639 two-letter code for a language,
* and its value is an array with a list of alternative languages that
* can be used if the given language is not available at some point.
* Each alternative language is also specified by its ISO 639 code.
*
* For example, for the "no" language code (Norwegian), we would have:
*
* 'priorities' => array(
* 'no' => array('nb', 'nn', 'en', 'se'),
* ...
* ),
*
* establishing that if a translation for the "no" language code is
* not available, we look for translations in "nb" (Norwegian Bokmål),
* and so on, in that order.
*/
'priorities' => array(
'no' => array('nb', 'nn', 'en', 'se'),
'nb' => array('no', 'nn', 'en', 'se'),
'nn' => array('no', 'nb', 'en', 'se'),
'se' => array('nb', 'no', 'nn', 'en'),
'en' => array('nb', 'no', 'nn', 'se'),
),
),
/* /*
* Languages available, RTL languages, and what language is the default. * Languages available, RTL languages, and what language is the default.
*/ */
......
...@@ -496,4 +496,48 @@ class Translate ...@@ -496,4 +496,48 @@ class Translate
return strtr($text, is_array($args[0]) ? $args[0] : $args); return strtr($text, is_array($args[0]) ? $args[0] : $args);
} }
/**
* Pick a translation from a given array of translations for the current language.
*
* @param array $context An array of options. The current language must be specified as an ISO 639 code accessible
* with the key "currentLanguage" in the array.
* @param array $translations An array of translations. Each translation has an ISO 639 code as its key, identifying
* the language it corresponds to.
*
* @return null|string The translation appropriate for the current language, or null if none found. If the
* $context or $translations arrays are null, or $context['currentLanguage'] is not defined, null is also returned.
*/
public static function translateFromArray($context, $translations)
{
if (!is_array($translations) || $translations === null) {
return null;
}
if (!is_array($context) || !isset($context['currentLanguage'])) {
return null;
}
if (isset($translations[$context['currentLanguage']])) {
return $translations[$context['currentLanguage']];
}
// we don't have a translation for the current language, load alternative priorities
$sspcfg = \SimpleSAML_Configuration::getInstance();
$langcfg = $sspcfg->getConfigItem('language', null);
$priorities = array();
if ($langcfg instanceof \SimpleSAML_Configuration) {
$priorities = $langcfg->getArray('priorities', array());
}
foreach ($priorities[$context['currentLanguage']] as $lang) {
if (isset($translations[$lang])) {
return $translations[$lang];
}
}
// nothing we can use, return null so that we can set a default
return null;
}
} }
...@@ -248,6 +248,15 @@ class SimpleSAML_XHTML_Template ...@@ -248,6 +248,15 @@ class SimpleSAML_XHTML_Template
$twig->addGlobal('queryParams', $queryParams); $twig->addGlobal('queryParams', $queryParams);
$twig->addGlobal('templateId', str_replace('.twig', '', $this->normalizeTemplateName($this->template))); $twig->addGlobal('templateId', str_replace('.twig', '', $this->normalizeTemplateName($this->template)));
// add a filter for translations out of arrays
$twig->addFilter(
new \Twig_SimpleFilter(
'translateFromArray',
array('\SimpleSAML\Locale\Translate', 'translateFromArray'),
array('needs_context' => true)
)
);
if ($this->controller) { if ($this->controller) {
$this->controller->setUpTwig($twig); $this->controller->setUpTwig($twig);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment