ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Localization.php
Go to the documentation of this file.
1<?php
2
10namespace SimpleSAML\Locale;
11
14
16{
17
24
28 const DEFAULT_DOMAIN = 'messages';
29
33 const SSP_I18N_BACKEND = 'SimpleSAMLphp';
34
38 const GETTEXT_I18N_BACKEND = 'gettext/gettext';
39
40 /*
41 * The default locale directory
42 */
43 private $localeDir;
44
45 /*
46 * Where specific domains are stored
47 */
48 private $localeDomainMap = array();
49
50 /*
51 * Pointer to currently active translator
52 */
53 private $translator;
54
55
62 {
63 $this->configuration = $configuration;
64 $this->localeDir = $this->configuration->resolvePath('locales');
65 $this->language = new Language($configuration);
66 $this->langcode = $this->language->getPosixLanguage($this->language->getLanguage());
67 $this->i18nBackend = $this->configuration->getString('language.i18n.backend', self::SSP_I18N_BACKEND);
68 $this->setupL10N();
69 }
70
71
75 public function getLocaleDir()
76 {
77 return $this->localeDir;
78 }
79
80
86 public function getDomainLocaleDir($domain)
87 {
88 $localeDir = $this->configuration->resolvePath('modules') . '/' . $domain . '/locales';
89 return $localeDir;
90 }
91
92
93 /*
94 * Add a new translation domain from a module
95 * (We're assuming that each domain only exists in one place)
96 *
97 * @param string $module Module name
98 * @param string $localeDir Absolute path if the module is housed elsewhere
99 */
100 public function addModuleDomain($module, $localeDir = null)
101 {
102 if (!$localeDir) {
104 }
106 }
107
108
109 /*
110 * Add a new translation domain
111 * (We're assuming that each domain only exists in one place)
112 *
113 * @param string $localeDir Location of translations
114 * @param string $domain Domain at location
115 */
116 public function addDomain($localeDir, $domain)
117 {
118 $this->localeDomainMap[$domain] = $localeDir;
119 \SimpleSAML\Logger::debug("Localization: load domain '$domain' at '$localeDir'");
121 }
122
123 /*
124 * Get and check path of localization file
125 *
126 * @param string $domain Name of localization domain
127 * @throws Exception If the path does not exist even for the default, fallback language
128 */
129 public function getLangPath($domain = self::DEFAULT_DOMAIN)
130 {
131 $langcode = explode('_', $this->langcode);
132 $langcode = $langcode[0];
133 $localeDir = $this->localeDomainMap[$domain];
134 $langPath = $localeDir.'/'.$langcode.'/LC_MESSAGES/';
135 \SimpleSAML\Logger::debug("Trying langpath for '$langcode' as '$langPath'");
136 if (is_dir($langPath) && is_readable($langPath)) {
137 return $langPath;
138 }
139
140 // Some langcodes have aliases..
141 $alias = $this->language->getLanguageCodeAlias($langcode);
142 if (isset($alias)) {
143 $langPath = $localeDir.'/'.$alias.'/LC_MESSAGES/';
144 \SimpleSAML\Logger::debug("Trying langpath for alternative '$alias' as '$langPath'");
145 if (is_dir($langPath) && is_readable($langPath)) {
146 return $langPath;
147 }
148 }
149
150 // Language not found, fall back to default
151 $defLangcode = $this->language->getDefaultLanguage();
152 $langPath = $localeDir.'/'.$defLangcode.'/LC_MESSAGES/';
153 if (is_dir($langPath) && is_readable($langPath)) {
154 // Report that the localization for the preferred language is missing
155 $error = "Localization not found for langcode '$langcode' at '$langPath', falling back to langcode '".
156 $defLangcode."'";
157 \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
158 return $langPath;
159 }
160
161 // Locale for default language missing even, error out
162 $error = "Localization directory missing/broken for langcode '$langcode' and domain '$domain'";
164 throw new \Exception($error);
165 }
166
167
171 private function setupTranslator()
172 {
173 $this->translator = new Translator();
174 $this->translator->register();
175 }
176
177
189 private function loadGettextGettextFromPO($domain = self::DEFAULT_DOMAIN, $catchException = true)
190 {
191 try {
192 $langPath = $this->getLangPath($domain);
193 } catch (\Exception $e) {
194 $error = "Something went wrong when trying to get path to language file, cannot load domain '$domain'.";
195 \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
196 if ($catchException) {
197 // bail out!
198 return;
199 } else {
200 throw $e;
201 }
202 }
203 $poFile = $domain.'.po';
204 $poPath = $langPath.$poFile;
205 if (file_exists($poPath) && is_readable($poPath)) {
206 $translations = Translations::fromPoFile($poPath);
207 $this->translator->loadTranslations($translations);
208 } else {
209 $error = "Localization file '$poFile' not found in '$langPath', falling back to default";
210 \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
211 }
212 }
213
214
220 public function isI18NBackendDefault()
221 {
222 if ($this->i18nBackend === $this::SSP_I18N_BACKEND) {
223 return true;
224 }
225 return false;
226 }
227
228
232 private function setupL10N()
233 {
234 if ($this->i18nBackend === self::SSP_I18N_BACKEND) {
235 \SimpleSAML\Logger::debug("Localization: using old system");
236 return;
237 }
238
239 $this->setupTranslator();
240 // setup default domain
241 $this->addDomain($this->localeDir, self::DEFAULT_DOMAIN);
242 }
243
247 public function getRegisteredDomains()
248 {
250 }
251
252}
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)
__construct(\SimpleSAML_Configuration $configuration)
Constructor.
setupL10N()
Set up L18N if configured or fallback to old system.
const GETTEXT_I18N_BACKEND
An internationalization backend implemented purely in PHP.
getLocaleDir()
Dump the default locale directory.
const SSP_I18N_BACKEND
Old internationalization backend included in SimpleSAMLphp.
getRegisteredDomains()
Show which domains are registered.
setupTranslator()
Setup the translator.
addDomain($localeDir, $domain)
addModuleDomain($module, $localeDir=null)
const DEFAULT_DOMAIN
The default gettext domain.
getDomainLocaleDir($domain)
Get the default locale dir for a specific module aka.
isI18NBackendDefault()
Test to check if backend is set to default.
static critical($string)
Definition: Logger.php:146
static error($string)
Definition: Logger.php:168
static debug($string)
Definition: Logger.php:213
$error
Definition: Error.php:17
if($modEnd===false) $module
Definition: module.php:59
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']