ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 138 of file Language.php.

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 }
$_GET["client_id"]
getInstalledLanguages()
Filter configured (available) languages against installed languages.
Definition: Language.php:160
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

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

+ 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 325 of file Language.php.

326 {
328 }

References SimpleSAML\Locale\Language\$defaultLanguage.

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

+ 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 284 of file Language.php.

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 }
static $defaultLanguageMap
This is the default language map.
Definition: Language.php:21
static getAcceptLanguage()
This function parses the Accept-Language HTTP header and returns an associative array with each langu...
Definition: HTTP.php:499

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

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

+ 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 160 of file Language.php.

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 }
static error($string)
Definition: Logger.php:168
$code
Definition: example_050.php:99

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

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

+ 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 217 of file Language.php.

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 }
static getLanguageCookie()
Retrieve the user-selected language from a cookie.
Definition: Language.php:377
getDefaultLanguage()
Return the default language according to configuration.
Definition: Language.php:325
getHTTPLanguage()
This method returns the preferred language for the user based on the Accept-Language HTTP header.
Definition: Language.php:284

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().

+ 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 336 of file Language.php.

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

◆ 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 377 of file Language.php.

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 }
$_COOKIE['client_id']
Definition: server.php:9
static getInstance($instancename='simplesaml')
Get a configuration file by its instance name.
if($format !==null) $name
Definition: metadata.php:146

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().

+ 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 352 of file Language.php.

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

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

+ 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 257 of file Language.php.

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 }

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

+ 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 272 of file Language.php.

References SimpleSAML\Locale\Language\$languageParameterName.

◆ 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 182 of file Language.php.

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

References SimpleSAML\Locale\Language\$language.

◆ 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 366 of file Language.php.

367 {
368 return in_array($this->getLanguage(), $this->rtlLanguages, true);
369 }

References SimpleSAML\Locale\Language\getLanguage().

+ 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 197 of file Language.php.

198 {
199 $language = strtolower($language);
200 if (in_array($language, $this->availableLanguages, true)) {
201 $this->language = $language;
202 if ($setLanguageCookie === true) {
204 }
205 }
206 }
static setLanguageCookie($language)
This method will attempt to set the user-selected language in a cookie.
Definition: Language.php:400

References SimpleSAML\Locale\Language\$language, and SimpleSAML\Locale\Language\setLanguageCookie().

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

+ Here is the call graph for this function:
+ 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 400 of file Language.php.

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 }
static setCookie($name, $value, $params=null, $throw=true)
Set a cookie.
Definition: HTTP.php:1107
$params
Definition: disable.php:11

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

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

+ 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.

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

◆ $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 127 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: