|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Glue to connect one or more translation/locale systems to the rest |
| 5 | + * |
| 6 | + * @author Hanne Moa, UNINETT AS. <hanne.moa@uninett.no> |
| 7 | + * @package SimpleSAMLphp |
| 8 | + */ |
| 9 | + |
| 10 | +namespace SimpleSAML\Locale; |
| 11 | + |
| 12 | +class Localization |
| 13 | +{ |
| 14 | + |
| 15 | + /** |
| 16 | + * The configuration to use. |
| 17 | + * |
| 18 | + * @var \SimpleSAML_Configuration |
| 19 | + */ |
| 20 | + private $configuration; |
| 21 | + |
| 22 | + /** |
| 23 | + * The default gettext domain. |
| 24 | + */ |
| 25 | + private $domain = 'ssp'; |
| 26 | + |
| 27 | + /* |
| 28 | + * The locale directory |
| 29 | + */ |
| 30 | + private $localeDir; |
| 31 | + |
| 32 | + /** |
| 33 | + * Constructor |
| 34 | + * |
| 35 | + * @param \SimpleSAML_Configuration $configuration Configuration object |
| 36 | + */ |
| 37 | + public function __construct(\SimpleSAML_Configuration $configuration) |
| 38 | + { |
| 39 | + $this->configuration = $configuration; |
| 40 | + $this->localeDir = $this->configuration->resolvePath('locales'); |
| 41 | + $this->language = new Language($configuration); |
| 42 | + $this->i18nBackend = $this->configuration->getString('language.i18n.backend', null); |
| 43 | + $this->setupL10N(); |
| 44 | + } |
| 45 | + |
| 46 | + private function setupL10N() { |
| 47 | + // use old system |
| 48 | + if (is_null($this->i18nBackend)) { |
| 49 | + return; |
| 50 | + } |
| 51 | + $encoding = "UTF-8"; |
| 52 | + $langcode = $this->language->getPosixLanguage($this->language->getLanguage()); |
| 53 | + // use gettext and Twig.I18n |
| 54 | + if ($this->i18nBackend == 'twig.i18n') { |
| 55 | + putenv('LC_ALL='.$langcode); |
| 56 | + setlocale(LC_ALL, $langcode); |
| 57 | + bindtextdomain($this->domain, $this->localeDir); |
| 58 | + bind_textdomain_codeset($this->domain, $encoding); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + public function activateDomain($domain) |
| 64 | + { |
| 65 | + if ($this->i18nBackend == 'twig.i18n') { |
| 66 | + textdomain($domain); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public function restoreDefaultDomain() |
| 72 | + { |
| 73 | + if ($this->i18nBackend == 'twig.i18n') { |
| 74 | + textdomain($this->domain); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments