Skip to content
Snippets Groups Projects
Commit 4f678fe8 authored by Hanne Moa's avatar Hanne Moa
Browse files

Switch to gettext/gettext w/domain-support

parent e464c395
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,6 @@ ...@@ -31,7 +31,6 @@
"robrichards/xmlseclibs": "~2.0", "robrichards/xmlseclibs": "~2.0",
"whitehat101/apr1-md5": "~1.0", "whitehat101/apr1-md5": "~1.0",
"twig/twig": "~1.0", "twig/twig": "~1.0",
"twig/extensions": "^1.3",
"gettext/gettext": "^3.5" "gettext/gettext": "^3.5"
}, },
"require-dev": { "require-dev": {
......
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
namespace SimpleSAML\Locale; namespace SimpleSAML\Locale;
use Gettext\Translations;
use Gettext\Translator;
class Localization class Localization
{ {
...@@ -22,13 +25,24 @@ class Localization ...@@ -22,13 +25,24 @@ class Localization
/** /**
* The default gettext domain. * The default gettext domain.
*/ */
private $domain = 'ssp'; const DEFAULT_DOMAIN = 'ssp';
/**
* Default 1i18n backend
*/
const DEFAULT_I18NBACKEND = 'twig.gettextgettext';
/* /*
* The locale directory * The default locale directory
*/ */
private $localeDir; private $localeDir;
/*
* Where specific domains are stored
*/
private $localeDomainMap = array();
/** /**
* Constructor * Constructor
* *
...@@ -39,40 +53,95 @@ class Localization ...@@ -39,40 +53,95 @@ class Localization
$this->configuration = $configuration; $this->configuration = $configuration;
$this->localeDir = $this->configuration->resolvePath('locales'); $this->localeDir = $this->configuration->resolvePath('locales');
$this->language = new Language($configuration); $this->language = new Language($configuration);
$this->langcode = $this->language->getPosixLanguage($this->language->getLanguage());
$this->i18nBackend = $this->configuration->getString('language.i18n.backend', null); $this->i18nBackend = $this->configuration->getString('language.i18n.backend', null);
$this->setupL10N(); $this->setupL10N();
} }
/*
* Add a new translation domain
* (We're assuming that each domain only exists in one place)
*
* @param string $localeDir Location of translations
* @param string $domain Domain at location
*/
private function addDomain($localeDir, $domain)
{
$this->localeDomainMap[$domain] = $localeDir;
}
/**
* Load translation domain from Gettext/Gettext using .po
*
* @param string $domain Name of domain
*/
private function loadGettextGettextFromPO($domain = self::DEFAULT_DOMAIN) {
$langcode = explode('_', $this->langcode)[0];
$localeDir = $this->localeDomainMap[$domain];
$poPath = $localeDir.'/'.$langcode.'/LC_MESSAGES/'.$domain.'.po';
$translations = Translations::fromPoFile($poPath);
$t = new Translator();
$t->loadTranslations($translations);
$t->register();
}
/**
* Test to check if backend is set to default
*
* (if false: backend unset/there's an error)
*/
public function isI18NBackendDefault()
{
if ($this->i18nBackend === $this::DEFAULT_I18NBACKEND) {
return true;
}
return false;
}
/**
* Set up L18N if configured or fallback to old system
*/
private function setupL10N() { private function setupL10N() {
// use old system // use old system
if (is_null($this->i18nBackend)) { if (! $this->isI18NBackendDefault()) {
\SimpleSAML\Logger::debug("Localization: using old system");
return; return;
} }
$encoding = "UTF-8"; // setup default domain
$langcode = $this->language->getPosixLanguage($this->language->getLanguage()); $this->addDomain($this->localeDir, self::DEFAULT_DOMAIN);
// use gettext and Twig.I18n $this->activateDomain(self::DEFAULT_DOMAIN);
if ($this->i18nBackend == 'twig.i18n') {
putenv('LC_ALL='.$langcode);
setlocale(LC_ALL, $langcode);
bindtextdomain($this->domain, $this->localeDir);
bind_textdomain_codeset($this->domain, $encoding);
}
} }
/**
* Set which translation domain to use
*
* @param string $domain Name of domain
*/
public function activateDomain($domain) public function activateDomain($domain)
{ {
if ($this->i18nBackend == 'twig.i18n') { \SimpleSAML\Logger::debug("Localization: activate domain");
textdomain($domain); $this->loadGettextGettextFromPO($domain);
} $this->currentDomain = $domain;
} }
/**
* Get current translation domain
*/
public function getCurrentDomain()
{
return $this->currentDomain ? $this->currentDomain : self::DEFAULT_DOMAIN;
}
/**
* Go back to default translation domain
*/
public function restoreDefaultDomain() public function restoreDefaultDomain()
{ {
if ($this->i18nBackend == 'twig.i18n') { $this->loadGettextGettextFromPO(self::DEFAULT_DOMAIN);
textdomain($this->domain); $this->currentDomain = self::DEFAULT_DOMAIN;
}
} }
} }
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
* @author Andreas Åkre Solberg, UNINETT AS. <andreas.solberg@uninett.no> * @author Andreas Åkre Solberg, UNINETT AS. <andreas.solberg@uninett.no>
* @package SimpleSAMLphp * @package SimpleSAMLphp
*/ */
class SimpleSAML_XHTML_Template class SimpleSAML_XHTML_Template
{ {
...@@ -154,9 +156,15 @@ class SimpleSAML_XHTML_Template ...@@ -154,9 +156,15 @@ class SimpleSAML_XHTML_Template
} }
$twig = new \Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => $auto_reload)); $twig = new \Twig_Environment($loader, array('cache' => $cache, 'auto_reload' => $auto_reload));
if ($this->localization->i18nBackend == 'twig.i18n') { // set up translation
$this->localization->activateDomain('ssp'); if ($this->localization->i18nBackend == 'twig.gettextgettext') {
$twig->addExtension(new \Twig_Extensions_Extension_I18n()); /* if something like pull request #166 is ever merged with
* twig.extensions.i18n, use the line below:
* $twig->addExtension(new \Twig_Extensions_Extension_I18n('__', 'n__'));
* instead of the two lines after this comment
*/
$twig->addFilter(new Twig_SimpleFilter('trans', '__'));
$twig->addTokenParser(new \SimpleSAML_Twig_TokenParser_Trans());
} }
return $twig; return $twig;
} }
...@@ -264,6 +272,7 @@ class SimpleSAML_XHTML_Template ...@@ -264,6 +272,7 @@ class SimpleSAML_XHTML_Template
*/ */
private function twigDefaultContext() private function twigDefaultContext()
{ {
$this->data['localeBackend'] = $this->configuration->getString('language.i18n.backend', 'ssp');
$this->data['currentLanguage'] = $this->translator->getLanguage()->getLanguage(); $this->data['currentLanguage'] = $this->translator->getLanguage()->getLanguage();
// show language bar by default // show language bar by default
if (!isset($this->data['hideLanguageBar'])) { if (!isset($this->data['hideLanguageBar'])) {
......
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
{% block content %} {% block content %}
<p>This page exists as a sandbox to play with twig without affecting anything else. The template is in ./templates.</p> <p>This page exists as a sandbox to play with twig without affecting anything else. The template is in ./templates.</p>
<p>{{ sometext }}</p> <p>{{ sometext }}</p>
<h2>And now for some localization</h2>
<p>Locale backend in use: {{ localeBackend }}</p>
<p>Original: Hello, Untranslated World!</p> <p>Original: Hello, Untranslated World!</p>
<p>Translated: {% trans 'Hello, Untranslated World!' %}</p> <p>Translated: {% trans 'Hello, Untranslated World!' %}</p>
<p>Filtertrans-test: {{ 'Hello, Untranslated World!'|trans }}</p>
<p>Current locale set: {{ currentLanguage }}</p> <p>Current locale set: {{ currentLanguage }}</p>
{% endblock content %} {% endblock content %}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment