ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Language.php
Go to the documentation of this file.
1<?php
2
12
14
16{
17
21 private static $defaultLanguageMap = array('nb' => 'no');
22
29
36
42 private $language = null;
43
50
57
64
71
80 private $language_names = array(
81 'no' => 'Bokmål', // Norwegian Bokmål
82 'nn' => 'Nynorsk', // Norwegian Nynorsk
83 'se' => 'Sámegiella', // Northern Sami
84 'sma' => 'Åarjelh-saemien giele', // Southern Sami
85 'da' => 'Dansk', // Danish
86 'en' => 'English',
87 'de' => 'Deutsch', // German
88 'sv' => 'Svenska', // Swedish
89 'fi' => 'Suomeksi', // Finnish
90 'es' => 'Español', // Spanish
91 'fr' => 'Français', // French
92 'it' => 'Italiano', // Italian
93 'nl' => 'Nederlands', // Dutch
94 'lb' => 'Lëtzebuergesch', // Luxembourgish
95 'cs' => 'Čeština', // Czech
96 'sl' => 'Slovenščina', // Slovensk
97 'lt' => 'Lietuvių kalba', // Lithuanian
98 'hr' => 'Hrvatski', // Croatian
99 'hu' => 'Magyar', // Hungarian
100 'pl' => 'Język polski', // Polish
101 'pt' => 'Português', // Portuguese
102 'pt-br' => 'Português brasileiro', // Portuguese
103 'ru' => 'русский язык', // Russian
104 'et' => 'eesti keel', // Estonian
105 'tr' => 'Türkçe', // Turkish
106 'el' => 'ελληνικά', // Greek
107 'ja' => '日本語', // Japanese
108 'zh' => '简体中文', // Chinese (simplified)
109 'zh-tw' => '繁體中文', // Chinese (traditional)
110 'ar' => 'العربية', // Arabic
111 'fa' => 'پارسی', // Persian
112 'ur' => 'اردو', // Urdu
113 'he' => 'עִבְרִית', // Hebrew
114 'id' => 'Bahasa Indonesia', // Indonesian
115 'sr' => 'Srpski', // Serbian
116 'lv' => 'Latviešu', // Latvian
117 'ro' => 'Românește', // Romanian
118 'eu' => 'Euskara', // Basque
119 'af' => 'Afrikaans', // Afrikaans
120 );
121
127 private $languagePosixMapping = array(
128 'no' => 'nb_NO',
129 'nn' => 'nn_NO',
130 );
131
132
139 {
140 $this->configuration = $configuration;
141 $this->availableLanguages = $this->getInstalledLanguages();
142 $this->defaultLanguage = $this->configuration->getString('language.default', 'en');
143 $this->languageParameterName = $this->configuration->getString('language.parameter.name', 'language');
144 $this->customFunction = $this->configuration->getArray('language.get_language_function', null);
145 $this->rtlLanguages = $this->configuration->getArray('language.rtl', array());
146 if (isset($_GET[$this->languageParameterName])) {
147 $this->setLanguage(
148 $_GET[$this->languageParameterName],
149 $this->configuration->getBoolean('language.parameter.setcookie', true)
150 );
151 }
152 }
153
154
160 private function getInstalledLanguages()
161 {
162 $configuredAvailableLanguages = $this->configuration->getArray('language.available', array('en'));
163 $availableLanguages = array();
164 foreach ($configuredAvailableLanguages as $code) {
165 if (array_key_exists($code, $this->language_names) && isset($this->language_names[$code])) {
167 } else {
168 \SimpleSAML\Logger::error("Language \"$code\" not installed. Check config.");
169 }
170 }
171 return $availableLanguages;
172 }
173
174
183 {
184 if (isset($this->languagePosixMapping[$language])) {
185 return $this->languagePosixMapping[$language];
186 }
187 return $language;
188 }
189
190
197 public function setLanguage($language, $setLanguageCookie = true)
198 {
199 $language = strtolower($language);
200 if (in_array($language, $this->availableLanguages, true)) {
201 $this->language = $language;
202 if ($setLanguageCookie === true) {
204 }
205 }
206 }
207
208
217 public function getLanguage()
218 {
219 // language is set in object
220 if (isset($this->language)) {
221 return $this->language;
222 }
223
224 // run custom getLanguage function if defined
225 if (isset($this->customFunction) && is_callable($this->customFunction)) {
226 $customLanguage = call_user_func($this->customFunction, $this);
227 if ($customLanguage !== null && $customLanguage !== false) {
228 return $customLanguage;
229 }
230 }
231
232 // language is provided in a stored cookie
233 $languageCookie = Language::getLanguageCookie();
234 if ($languageCookie !== null) {
235 $this->language = $languageCookie;
236 return $languageCookie;
237 }
238
239 // check if we can find a good language from the Accept-Language HTTP header
240 $httpLanguage = $this->getHTTPLanguage();
241 if ($httpLanguage !== null) {
242 return $httpLanguage;
243 }
244
245 // language is not set, and we get the default language from the configuration
246 return $this->getDefaultLanguage();
247 }
248
249
258 {
259 if (array_key_exists($code, $this->language_names) && isset($this->language_names[$code])) {
260 return $this->language_names[$code];
261 }
262 \SimpleSAML\Logger::error("Name for language \"$code\" not found. Check config.");
263 return null;
264 }
265
266
272 public function getLanguageParameterName()
273 {
275 }
276
277
284 private function getHTTPLanguage()
285 {
286 $languageScore = HTTP::getAcceptLanguage();
287
288 // for now we only use the default language map. We may use a configurable language map in the future
289 $languageMap = self::$defaultLanguageMap;
290
291 // find the available language with the best score
292 $bestLanguage = null;
293 $bestScore = -1.0;
294
295 foreach ($languageScore as $language => $score) {
296 // apply the language map to the language code
297 if (array_key_exists($language, $languageMap)) {
298 $language = $languageMap[$language];
299 }
300
301 if (!in_array($language, $this->availableLanguages, true)) {
302 // skip this language - we don't have it
303 continue;
304 }
305
306 /* Some user agents use very limited precision of the quality value, but order the elements in descending
307 * order. Therefore we rely on the order of the output from getAcceptLanguage() matching the order of the
308 * languages in the header when two languages have the same quality.
309 */
310 if ($score > $bestScore) {
311 $bestLanguage = $language;
312 $bestScore = $score;
313 }
314 }
315
316 return $bestLanguage;
317 }
318
319
325 public function getDefaultLanguage()
326 {
328 }
329
330
336 public function getLanguageCodeAlias($langcode)
337 {
338 if (isset(self::$defaultLanguageMap[$langcode])) {
339 return self::$defaultLanguageMap[$langcode];
340 }
341 // No alias found, which is fine
342 return null;
343 }
344
345
352 public function getLanguageList()
353 {
354 $current = $this->getLanguage();
355 $list = array_fill_keys($this->availableLanguages, false);
356 $list[$current] = true;
357 return $list;
358 }
359
360
366 public function isLanguageRTL()
367 {
368 return in_array($this->getLanguage(), $this->rtlLanguages, true);
369 }
370
371
377 public static function getLanguageCookie()
378 {
380 $availableLanguages = $config->getArray('language.available', array('en'));
381 $name = $config->getString('language.cookie.name', 'language');
382
383 if (isset($_COOKIE[$name])) {
384 $language = strtolower((string) $_COOKIE[$name]);
385 if (in_array($language, $availableLanguages, true)) {
386 return $language;
387 }
388 }
389
390 return null;
391 }
392
393
400 public static function setLanguageCookie($language)
401 {
402 assert('is_string($language)');
403
404 $language = strtolower($language);
406 $availableLanguages = $config->getArray('language.available', array('en'));
407
408 if (!in_array($language, $availableLanguages, true) || headers_sent()) {
409 return;
410 }
411
412 $name = $config->getString('language.cookie.name', 'language');
413 $params = array(
414 'lifetime' => ($config->getInteger('language.cookie.lifetime', 60 * 60 * 24 * 900)),
415 'domain' => ($config->getString('language.cookie.domain', null)),
416 'path' => ($config->getString('language.cookie.path', '/')),
417 'secure' => ($config->getBoolean('language.cookie.secure', false)),
418 'httponly' => ($config->getBoolean('language.cookie.httponly', false)),
419 );
420
422 }
423}
$_COOKIE['client_id']
Definition: server.php:9
$_GET["client_id"]
An exception for terminatinating execution or to throw for unit testing.
static getLanguageCookie()
Retrieve the user-selected language from a cookie.
Definition: Language.php:377
static setLanguageCookie($language)
This method will attempt to set the user-selected language in a cookie.
Definition: Language.php:400
getLanguageCodeAlias($langcode)
Return an alias for a language code, if any.
Definition: Language.php:336
getLanguageList()
Return an indexed list of all languages available.
Definition: Language.php:352
isLanguageRTL()
Check whether a language is written from the right to the left or not.
Definition: Language.php:366
getDefaultLanguage()
Return the default language according to configuration.
Definition: Language.php:325
getInstalledLanguages()
Filter configured (available) languages against installed languages.
Definition: Language.php:160
getLanguage()
This method will return the language selected by the user, or the default language.
Definition: Language.php:217
getPosixLanguage($language)
Rename to non-idiosyncratic language code.
Definition: Language.php:182
__construct(\SimpleSAML_Configuration $configuration)
Constructor.
Definition: Language.php:138
getLanguageLocalizedName($code)
Get the localized name of a language, by ISO 639-2 code.
Definition: Language.php:257
getLanguageParameterName()
Get the language parameter name.
Definition: Language.php:272
getHTTPLanguage()
This method returns the preferred language for the user based on the Accept-Language HTTP header.
Definition: Language.php:284
static $defaultLanguageMap
This is the default language map.
Definition: Language.php:21
setLanguage($language, $setLanguageCookie=true)
This method will set a cookie for the user's browser to remember what language was selected.
Definition: Language.php:197
static error($string)
Definition: Logger.php:168
static setCookie($name, $value, $params=null, $throw=true)
Set a cookie.
Definition: HTTP.php:1107
static getAcceptLanguage()
This function parses the Accept-Language HTTP header and returns an associative array with each langu...
Definition: HTTP.php:499
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
$code
Definition: example_050.php:99
if($format !==null) $name
Definition: metadata.php:146
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
$params
Definition: disable.php:11