ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Localization.php
Go to the documentation of this file.
1<?php
2
10namespace SimpleSAML\Locale;
11
14
16{
23
27 const DEFAULT_DOMAIN = 'messages';
28
32 const SSP_I18N_BACKEND = 'SimpleSAMLphp';
33
37 const GETTEXT_I18N_BACKEND = 'gettext/gettext';
38
42 private $localeDir;
43
47 private $localeDomainMap = array();
48
52 private $translator;
53
57 private $language;
58
62 private $langcode;
63
64
69
76 {
77 $this->configuration = $configuration;
78 $this->localeDir = $this->configuration->resolvePath('locales');
79 $this->language = new Language($configuration);
80 $this->langcode = $this->language->getPosixLanguage($this->language->getLanguage());
81 $this->i18nBackend = $this->configuration->getString('language.i18n.backend', self::SSP_I18N_BACKEND);
82 $this->setupL10N();
83 }
84
85
89 public function getLocaleDir()
90 {
91 return $this->localeDir;
92 }
93
94
101 {
102 $localeDir = $this->configuration->resolvePath('modules') . '/' . $domain . '/locales';
103 return $localeDir;
104 }
105
106
107 /*
108 * Add a new translation domain from a module
109 * (We're assuming that each domain only exists in one place)
110 *
111 * @param string $module Module name
112 * @param string $localeDir Absolute path if the module is housed elsewhere
113 */
114 public function addModuleDomain($module, $localeDir = null)
115 {
116 if (!$localeDir) {
118 }
120 }
121
122
123 /*
124 * Add a new translation domain
125 * (We're assuming that each domain only exists in one place)
126 *
127 * @param string $localeDir Location of translations
128 * @param string $domain Domain at location
129 */
130 public function addDomain($localeDir, $domain)
131 {
132 $this->localeDomainMap[$domain] = $localeDir;
133 \SimpleSAML\Logger::debug("Localization: load domain '$domain' at '$localeDir'");
135 }
136
137 /*
138 * Get and check path of localization file
139 *
140 * @param string $domain Name of localization domain
141 * @throws Exception If the path does not exist even for the default, fallback language
142 */
143 public function getLangPath($domain = self::DEFAULT_DOMAIN)
144 {
145 $langcode = explode('_', $this->langcode);
146 $langcode = $langcode[0];
147 $localeDir = $this->localeDomainMap[$domain];
148 $langPath = $localeDir.'/'.$langcode.'/LC_MESSAGES/';
149 \SimpleSAML\Logger::debug("Trying langpath for '$langcode' as '$langPath'");
150 if (is_dir($langPath) && is_readable($langPath)) {
151 return $langPath;
152 }
153
154 // Some langcodes have aliases..
155 $alias = $this->language->getLanguageCodeAlias($langcode);
156 if (isset($alias)) {
157 $langPath = $localeDir.'/'.$alias.'/LC_MESSAGES/';
158 \SimpleSAML\Logger::debug("Trying langpath for alternative '$alias' as '$langPath'");
159 if (is_dir($langPath) && is_readable($langPath)) {
160 return $langPath;
161 }
162 }
163
164 // Language not found, fall back to default
165 $defLangcode = $this->language->getDefaultLanguage();
166 $langPath = $localeDir.'/'.$defLangcode.'/LC_MESSAGES/';
167 if (is_dir($langPath) && is_readable($langPath)) {
168 // Report that the localization for the preferred language is missing
169 $error = "Localization not found for langcode '$langcode' at '$langPath', falling back to langcode '".
170 $defLangcode."'";
171 \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
172 return $langPath;
173 }
174
175 // Locale for default language missing even, error out
176 $error = "Localization directory missing/broken for langcode '$langcode' and domain '$domain'";
177 \SimpleSAML\Logger::critical($_SERVER['PHP_SELF'].' - '.$error);
178 throw new \Exception($error);
179 }
180
181
185 private function setupTranslator()
186 {
187 $this->translator = new Translator();
188 $this->translator->register();
189 }
190
191
203 private function loadGettextGettextFromPO($domain = self::DEFAULT_DOMAIN, $catchException = true)
204 {
205 try {
206 $langPath = $this->getLangPath($domain);
207 } catch (\Exception $e) {
208 $error = "Something went wrong when trying to get path to language file, cannot load domain '$domain'.";
209 \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
210 if ($catchException) {
211 // bail out!
212 return;
213 } else {
214 throw $e;
215 }
216 }
217 $poFile = $domain.'.po';
218 $poPath = $langPath.$poFile;
219 if (file_exists($poPath) && is_readable($poPath)) {
220 $translations = Translations::fromPoFile($poPath);
221 $this->translator->loadTranslations($translations);
222 } else {
223 $error = "Localization file '$poFile' not found in '$langPath', falling back to default";
224 \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
225 }
226 }
227
228
234 public function isI18NBackendDefault()
235 {
236 if ($this->i18nBackend === $this::SSP_I18N_BACKEND) {
237 return true;
238 }
239 return false;
240 }
241
242
246 private function setupL10N()
247 {
248 if ($this->i18nBackend === self::SSP_I18N_BACKEND) {
249 \SimpleSAML\Logger::debug("Localization: using old system");
250 return;
251 }
252
253 $this->setupTranslator();
254 // setup default domain
255 $this->addDomain($this->localeDir, self::DEFAULT_DOMAIN);
256 }
257
261 public function getRegisteredDomains()
262 {
264 }
265}
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
An exception for terminatinating execution or to throw for unit testing.
Class to manage a collection of translations.
loadGettextGettextFromPO($domain=self::DEFAULT_DOMAIN, $catchException=true)
Load translation domain from Gettext/Gettext using .po.
getLangPath($domain=self::DEFAULT_DOMAIN)
$localeDomainMap
Where specific domains are stored.
__construct(\SimpleSAML_Configuration $configuration)
Constructor.
setupL10N()
Set up L18N if configured or fallback to old system.
$localeDir
The default locale directory.
const GETTEXT_I18N_BACKEND
An internationalization backend implemented purely in PHP.
getLocaleDir()
Dump the default locale directory.
$i18nBackend
The language backend to use.
const SSP_I18N_BACKEND
Old internationalization backend included in SimpleSAMLphp.
getRegisteredDomains()
Show which domains are registered.
setupTranslator()
Setup the translator.
addDomain($localeDir, $domain)
$language
Pointer to current Language.
addModuleDomain($module, $localeDir=null)
const DEFAULT_DOMAIN
The default gettext domain.
$langcode
Language code representing the current Language.
getDomainLocaleDir($domain)
Get the default locale dir for a specific module aka.
isI18NBackendDefault()
Test to check if backend is set to default.
$translator
Pointer to currently active translator.
static critical($string)
Definition: Logger.php:144
static error($string)
Definition: Logger.php:166
static debug($string)
Definition: Logger.php:211
if($modEnd===false) $module
Definition: module.php:59
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']