Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/SimpleSAML/Locale/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,20 @@ class Language
* Constructor
*
* @param \SimpleSAML\Configuration $configuration Configuration object
* @param bool $handleLanguageRequestParameter Whether to handle the language request parameter, if present
* (switch the current language and possibly set the language cookie). Can be disabled by consumers which
* only need to query the instance, like the available languages, without triggering side effects.
*/
public function __construct(
private Configuration $configuration,
bool $handleLanguageRequestParameter = true,
) {
$this->availableLanguages = $this->getInstalledLanguages();
$this->defaultLanguage = $configuration->getOptionalString('language.default', self::FALLBACKLANGUAGE);
$this->languageParameterName = $configuration->getOptionalString('language.parameter.name', 'language');
$this->customFunction = $configuration->getOptionalArray('language.get_language_function', null);
$this->rtlLanguages = $configuration->getOptionalArray('language.rtl', []);
if (isset($_GET[$this->languageParameterName])) {
if ($handleLanguageRequestParameter && isset($_GET[$this->languageParameterName])) {
$this->setLanguage(
$_GET[$this->languageParameterName],
$configuration->getOptionalBoolean('language.parameter.setcookie', true),
Expand Down Expand Up @@ -208,6 +212,19 @@ private function getInstalledLanguages(): array
}


/**
* Return the languages that are both configured to be available (the `language.available` configuration
* option) and known to the translation system, i.e. the set of languages actually usable for the user
* interface.
*
* @return string[] The available languages.
*/
public function getAvailableLanguages(): array
{
return $this->availableLanguages;
}


/**
* Rename to non-idiosyncratic language code.
*
Expand Down
50 changes: 50 additions & 0 deletions tests/src/SimpleSAML/Locale/LanguageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,56 @@ public function testSetLanguage(): void
}


/**
* Test SimpleSAML\Locale\Language::getAvailableLanguages().
*/
public function testGetAvailableLanguages(): void
{
// test default
$c = Configuration::loadFromArray([], '', 'simplesaml');
$l = new Language($c);
$this->assertEquals(['en'], $l->getAvailableLanguages());

// test configured languages
$c = Configuration::loadFromArray([
'language.available' => ['en', 'nn', 'es'],
], '', 'simplesaml');
$l = new Language($c);
$this->assertEquals(['en', 'nn', 'es'], $l->getAvailableLanguages());

// test that configured languages unknown to the translation system are excluded
$c = Configuration::loadFromArray([
'language.available' => ['en', 'foo'],
], '', 'simplesaml');
$l = new Language($c);
$this->assertEquals(['en'], $l->getAvailableLanguages());
}


/**
* Test disabling the handling of the language request parameter (Language constructor).
*/
public function testLanguageRequestParameterHandlingCanBeDisabled(): void
{
unset($_COOKIE['language']);
$c = Configuration::loadFromArray([
'language.available' => ['en', 'nn', 'es'],
'language.parameter.setcookie' => false,
], '', 'simplesaml');
$_GET['language'] = 'es';

// by default, the language request parameter is handled
$l = new Language($c);
$this->assertEquals('es', $l->getLanguage());

// handling of the language request parameter can be disabled
$l = new Language($c, handleLanguageRequestParameter: false);
$this->assertEquals('en', $l->getLanguage());

unset($_GET['language']);
}


public function testGetPreferredLanguages(): void
{
// test defaults
Expand Down
Loading