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