ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
SimpleSAML\Locale\Language Class Reference
+ Collaboration diagram for SimpleSAML\Locale\Language:

Public Member Functions

 __construct (\SimpleSAML_Configuration $configuration)
 Constructor. More...
 
 getPosixLanguage ($language)
 Rename to non-idiosyncratic language code. More...
 
 setLanguage ($language, $setLanguageCookie=true)
 This method will set a cookie for the user's browser to remember what language was selected. More...
 
 getLanguage ()
 This method will return the language selected by the user, or the default language. More...
 
 getLanguageLocalizedName ($code)
 Get the localized name of a language, by ISO 639-2 code. More...
 
 getLanguageParameterName ()
 Get the language parameter name. More...
 
 getDefaultLanguage ()
 Return the default language according to configuration. More...
 
 getLanguageCodeAlias ($langcode)
 Return an alias for a language code, if any. More...
 
 getLanguageList ()
 Return an indexed list of all languages available. More...
 
 isLanguageRTL ()
 Check whether a language is written from the right to the left or not. More...
 

Static Public Member Functions

static getLanguageCookie ()
 Retrieve the user-selected language from a cookie. More...
 
static setLanguageCookie ($language)
 This method will attempt to set the user-selected language in a cookie. More...
 

Private Member Functions

 getInstalledLanguages ()
 Filter configured (available) languages against installed languages. More...
 
 getHTTPLanguage ()
 This method returns the preferred language for the user based on the Accept-Language HTTP header. More...
 

Private Attributes

 $configuration
 
 $availableLanguages
 
 $language = null
 
 $defaultLanguage
 
 $rtlLanguages
 
 $languageParameterName
 
 $customFunction
 
 $language_names
 
 $languagePosixMapping
 

Static Private Attributes

static $defaultLanguageMap = array('nb' => 'no')
 This is the default language map. More...
 

Detailed Description

Definition at line 15 of file Language.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\Locale\Language::__construct ( \SimpleSAML_Configuration  $configuration)

Constructor.

Parameters
\SimpleSAML_Configuration$configurationConfiguration object

Definition at line 139 of file Language.php.

References $_GET, SimpleSAML\Locale\Language\$configuration, SimpleSAML\Locale\Language\getInstalledLanguages(), and SimpleSAML\Locale\Language\setLanguage().

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  }
$_GET["client_id"]
setLanguage($language, $setLanguageCookie=true)
This method will set a cookie for the user's browser to remember what language was selected...
Definition: Language.php:198
getInstalledLanguages()
Filter configured (available) languages against installed languages.
Definition: Language.php:161
+ Here is the call graph for this function:

Member Function Documentation

◆ getDefaultLanguage()

SimpleSAML\Locale\Language::getDefaultLanguage ( )

Return the default language according to configuration.

Returns
string The default language that has been configured. Defaults to english if not configured.

Definition at line 326 of file Language.php.

References SimpleSAML\Locale\Language\$defaultLanguage.

Referenced by SimpleSAML\Locale\Language\getLanguage().

327  {
328  return $this->defaultLanguage;
329  }
+ Here is the caller graph for this function:

◆ getHTTPLanguage()

SimpleSAML\Locale\Language::getHTTPLanguage ( )
private

This method returns the preferred language for the user based on the Accept-Language HTTP header.

Returns
string The preferred language based on the Accept-Language HTTP header, or null if none of the languages in the header is available.

Definition at line 285 of file Language.php.

References SimpleSAML\Locale\Language\$language, and SimpleSAML\Utils\HTTP\getAcceptLanguage().

Referenced by SimpleSAML\Locale\Language\getLanguage().

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  }
static getAcceptLanguage()
This function parses the Accept-Language HTTP header and returns an associative array with each langu...
Definition: HTTP.php:498
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInstalledLanguages()

SimpleSAML\Locale\Language::getInstalledLanguages ( )
private

Filter configured (available) languages against installed languages.

Returns
array The set of languages both in 'language.available' and $this->language_names.

Definition at line 161 of file Language.php.

References SimpleSAML\Locale\Language\$availableLanguages, $code, and SimpleSAML\Logger\error().

Referenced by SimpleSAML\Locale\Language\__construct().

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  }
$code
Definition: example_050.php:99
static error($string)
Definition: Logger.php:166
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLanguage()

SimpleSAML\Locale\Language::getLanguage ( )

This method will return the language selected by the user, or the default language.

It looks first for a cached language code, then checks for a language cookie, then it tries to calculate the preferred language from HTTP headers.

Returns
string The language selected by the user according to the processing rules specified, or the default language in any other case.

Definition at line 218 of file Language.php.

References SimpleSAML\Locale\Language\$language, SimpleSAML\Locale\Language\getDefaultLanguage(), SimpleSAML\Locale\Language\getHTTPLanguage(), and SimpleSAML\Locale\Language\getLanguageCookie().

Referenced by SimpleSAML\Locale\Language\getLanguageList(), and SimpleSAML\Locale\Language\isLanguageRTL().

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  }
static getLanguageCookie()
Retrieve the user-selected language from a cookie.
Definition: Language.php:378
getHTTPLanguage()
This method returns the preferred language for the user based on the Accept-Language HTTP header...
Definition: Language.php:285
getDefaultLanguage()
Return the default language according to configuration.
Definition: Language.php:326
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLanguageCodeAlias()

SimpleSAML\Locale\Language::getLanguageCodeAlias (   $langcode)

Return an alias for a language code, if any.

Returns
string The alias, or null if the alias was not found.

Definition at line 337 of file Language.php.

338  {
339  if (isset(self::$defaultLanguageMap[$langcode])) {
340  return self::$defaultLanguageMap[$langcode];
341  }
342  // No alias found, which is fine
343  return null;
344  }

◆ getLanguageCookie()

static SimpleSAML\Locale\Language::getLanguageCookie ( )
static

Retrieve the user-selected language from a cookie.

Returns
string|null The selected language or null if unset.

Definition at line 378 of file Language.php.

References $_COOKIE, SimpleSAML\Locale\Language\$availableLanguages, $config, SimpleSAML\Locale\Language\$language, $name, and SimpleSAML_Configuration\getInstance().

Referenced by SimpleSAML\Locale\Language\getLanguage(), and sspmod_core_Auth_Process_LanguageAdaptor\process().

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  }
$_COOKIE['client_id']
Definition: server.php:9
$config
Definition: bootstrap.php:15
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLanguageList()

SimpleSAML\Locale\Language::getLanguageList ( )

Return an indexed list of all languages available.

Returns
array An array holding all the languages available as the keys of the array. The value for each key is true in case that the language specified by that key is currently active, or false otherwise.

Definition at line 353 of file Language.php.

References $current, $list, and SimpleSAML\Locale\Language\getLanguage().

354  {
355  $current = $this->getLanguage();
356  $list = array_fill_keys($this->availableLanguages, false);
357  $list[$current] = true;
358  return $list;
359  }
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
getLanguage()
This method will return the language selected by the user, or the default language.
Definition: Language.php:218
+ Here is the call graph for this function:

◆ getLanguageLocalizedName()

SimpleSAML\Locale\Language::getLanguageLocalizedName (   $code)

Get the localized name of a language, by ISO 639-2 code.

Parameters
string$codeThe ISO 639-2 code of the language.
Returns
string The localized name of the language.

Definition at line 258 of file Language.php.

References $code, and SimpleSAML\Logger\error().

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  }
$code
Definition: example_050.php:99
static error($string)
Definition: Logger.php:166
+ Here is the call graph for this function:

◆ getLanguageParameterName()

SimpleSAML\Locale\Language::getLanguageParameterName ( )

Get the language parameter name.

Returns
string The language parameter name.

Definition at line 273 of file Language.php.

References SimpleSAML\Locale\Language\$languageParameterName.

274  {
276  }

◆ getPosixLanguage()

SimpleSAML\Locale\Language::getPosixLanguage (   $language)

Rename to non-idiosyncratic language code.

Parameters
string$languageLanguage code for the language to rename, if necessary.
Returns
string The language code.

Definition at line 183 of file Language.php.

References SimpleSAML\Locale\Language\$language.

184  {
185  if (isset($this->languagePosixMapping[$language])) {
186  return $this->languagePosixMapping[$language];
187  }
188  return $language;
189  }

◆ isLanguageRTL()

SimpleSAML\Locale\Language::isLanguageRTL ( )

Check whether a language is written from the right to the left or not.

Returns
boolean True if the language is right-to-left, false otherwise.

Definition at line 367 of file Language.php.

References SimpleSAML\Locale\Language\getLanguage().

368  {
369  return in_array($this->getLanguage(), $this->rtlLanguages, true);
370  }
getLanguage()
This method will return the language selected by the user, or the default language.
Definition: Language.php:218
+ Here is the call graph for this function:

◆ setLanguage()

SimpleSAML\Locale\Language::setLanguage (   $language,
  $setLanguageCookie = true 
)

This method will set a cookie for the user's browser to remember what language was selected.

Parameters
string$languageLanguage code for the language to set.
boolean$setLanguageCookieWhether to set the language cookie or not. Defaults to true.

Definition at line 198 of file Language.php.

References SimpleSAML\Locale\Language\$language.

Referenced by SimpleSAML\Locale\Language\__construct().

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  }
+ Here is the caller graph for this function:

◆ setLanguageCookie()

static SimpleSAML\Locale\Language::setLanguageCookie (   $language)
static

This method will attempt to set the user-selected language in a cookie.

It will do nothing if the language specified is not in the list of available languages, or the headers have already been sent to the browser.

Parameters
string$languageThe language set by the user.

Definition at line 401 of file Language.php.

References SimpleSAML\Locale\Language\$availableLanguages, $config, SimpleSAML\Locale\Language\$language, $name, PHPMailer\PHPMailer\$params, SimpleSAML_Configuration\getInstance(), and SimpleSAML\Utils\HTTP\setCookie().

Referenced by sspmod_core_Auth_Process_LanguageAdaptor\process().

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  }
$config
Definition: bootstrap.php:15
static setCookie($name, $value, $params=null, $throw=true)
Set a cookie.
Definition: HTTP.php:1104
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $availableLanguages

SimpleSAML\Locale\Language::$availableLanguages
private

◆ $configuration

SimpleSAML\Locale\Language::$configuration
private

Definition at line 28 of file Language.php.

Referenced by SimpleSAML\Locale\Language\__construct().

◆ $customFunction

SimpleSAML\Locale\Language::$customFunction
private

Definition at line 70 of file Language.php.

◆ $defaultLanguage

SimpleSAML\Locale\Language::$defaultLanguage
private

Definition at line 49 of file Language.php.

Referenced by SimpleSAML\Locale\Language\getDefaultLanguage().

◆ $defaultLanguageMap

SimpleSAML\Locale\Language::$defaultLanguageMap = array('nb' => 'no')
staticprivate

This is the default language map.

It is used to map languages codes from the user agent to other language codes.

Definition at line 21 of file Language.php.

◆ $language

◆ $language_names

SimpleSAML\Locale\Language::$language_names
private

Definition at line 80 of file Language.php.

◆ $languageParameterName

SimpleSAML\Locale\Language::$languageParameterName
private

Definition at line 63 of file Language.php.

Referenced by SimpleSAML\Locale\Language\getLanguageParameterName().

◆ $languagePosixMapping

SimpleSAML\Locale\Language::$languagePosixMapping
private
Initial value:
= array(
'no' => 'nb_NO',
'nn' => 'nn_NO',
)

Definition at line 128 of file Language.php.

◆ $rtlLanguages

SimpleSAML\Locale\Language::$rtlLanguages
private

Definition at line 56 of file Language.php.


The documentation for this class was generated from the following file: