Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 14 additions & 15 deletions module/VuFind/src/VuFind/I18n/Locale/LocaleSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
namespace VuFind\I18n\Locale;

use Psr\Http\Message\ServerRequestInterface;
use VuFind\Config\Config;

use function array_key_exists;
use function floatval;
Expand Down Expand Up @@ -106,12 +105,12 @@ class LocaleSettings
/**
* Constructor.
*
* @param Config $config Configuration object
* @param array $config Configuration object
*/
public function __construct(Config $config)
public function __construct(array $config)
{
$this->enabledLocales = $config->Languages ? $config->Languages->toArray() : [];
$this->browserDetectLanguage = (bool)($config->Site->browserDetectLanguage ?? true);
$this->enabledLocales = $config['Languages'] ?? [];
$this->browserDetectLanguage = (bool)($config['Site']['browserDetectLanguage'] ?? true);
$this->defaultLocale = $this->parseDefaultLocale($config);
$this->fallbackLocales = $this->parseFallbackLocales($config);
$this->rightToLeftLocales = $this->parseRightToLeftLocales($config);
Expand Down Expand Up @@ -197,14 +196,14 @@ public function getRightToLeftLocales(): array
/**
* Extract and validate default locale from configuration.
*
* @param Config $config Configuration
* @param array $config Configuration
*
* @return string
* @throws \Exception
*/
protected function parseDefaultLocale(Config $config): string
protected function parseDefaultLocale(array $config): string
{
$locale = $config->Site->language ?? null;
$locale = $config['Site']['language'] ?? null;
if (empty($locale)) {
throw new \Exception('Default locale not configured!');
}
Expand All @@ -217,18 +216,18 @@ protected function parseDefaultLocale(Config $config): string
/**
* Parses the configured language fallbacks.
*
* @param Config $config Configuration
* @param array $config Configuration
*
* @return string[]
*/
protected function parseFallbackLocales(Config $config): array
protected function parseFallbackLocales(array $config): array
{
$value = trim($config->Site->fallback_languages ?? '', ',');
$value = trim($config['Site']['fallback_languages'] ?? '', ',');
$languages = $value ? array_map('trim', explode(',', $value)) : [];
return array_unique(
[
...$languages,
$config->Site->language,
$config['Site']['language'],
'en',
]
);
Expand All @@ -237,13 +236,13 @@ protected function parseFallbackLocales(Config $config): array
/**
* Parses the right-to-left language configuration.
*
* @param Config $config Configuration
* @param array $config Configuration
*
* @return string[]
*/
protected function parseRightToLeftLocales(Config $config): array
protected function parseRightToLeftLocales(array $config): array
{
$value = trim($config->LanguageSettings->rtl_langs ?? '', ',');
$value = trim($config['LanguageSettings']['rtl_langs'] ?? '', ',');
return $value ? array_map('trim', explode(',', $value)) : [];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

use GuzzleHttp\Psr7\ServerRequest;
use Psr\Http\Message\ServerRequestInterface;
use VuFind\Config\Config;
use VuFind\I18n\Locale\LocaleSettings;

/**
Expand All @@ -56,7 +55,7 @@ class LocaleSettingsTest extends \PHPUnit\Framework\TestCase
public function testDefaultLocaleRequired(): void
{
$this->expectExceptionMessage('Default locale not configured!');
new LocaleSettings(new Config([]));
new LocaleSettings([]);
}

/**
Expand All @@ -68,7 +67,7 @@ public function testDefaultLocaleRequired(): void
public function testDefaultMustBeEnabled(): void
{
$this->expectExceptionMessage("Configured default locale 'en' not enabled!");
new LocaleSettings(new Config(['Site' => ['language' => 'en']]));
new LocaleSettings(['Site' => ['language' => 'en']]);
}

/**
Expand All @@ -79,12 +78,10 @@ public function testDefaultMustBeEnabled(): void
public function testDefaultConfigs(): void
{
$settings = new LocaleSettings(
new Config(
[
[
Comment thread
demiankatz marked this conversation as resolved.
'Site' => ['language' => 'en'],
'Languages' => ['en' => 'English'],
]
)
);
$this->assertTrue($settings->browserLanguageDetectionEnabled());
$this->assertSame(['en'], $settings->getFallbackLocales());
Expand All @@ -98,12 +95,10 @@ public function testDefaultConfigs(): void
public function testDisablingBrowserLanguageDetection(): void
{
$settings = new LocaleSettings(
new Config(
[
[
'Site' => ['language' => 'en', 'browserDetectLanguage' => 0],
'Languages' => ['en' => 'English'],
]
)
);
$this->assertFalse($settings->browserLanguageDetectionEnabled());
}
Expand All @@ -116,13 +111,11 @@ public function testDisablingBrowserLanguageDetection(): void
public function testRightToLeft(): void
{
$settings = new LocaleSettings(
new Config(
[
[
'Site' => ['language' => 'en'],
'Languages' => ['en' => 'English', 'ar' => 'Arabic'],
'LanguageSettings' => ['rtl_langs' => 'ar'],
]
)
);
$this->assertFalse($settings->isRightToLeftLocale('en'));
$this->assertTrue($settings->isRightToLeftLocale('ar'));
Expand All @@ -136,12 +129,10 @@ public function testRightToLeft(): void
public function testInitializationStatusFlagging(): void
{
$settings = new LocaleSettings(
new Config(
[
[
'Site' => ['language' => 'en'],
'Languages' => ['en' => 'English'],
]
)
);
$this->assertFalse($settings->isLocaleInitialized('en'));
$settings->markLocaleInitialized('en');
Expand Down Expand Up @@ -212,7 +203,7 @@ public function testFallbackLocaleConfigs(array $expected, string $language, ?st
$config['Site']['fallback_languages'] = $fallbackLanguages;
}

$settings = new LocaleSettings(new Config($config));
$settings = new LocaleSettings($config);
$this->assertEquals($expected, $settings->getFallbackLocales());
}

Expand Down Expand Up @@ -364,12 +355,10 @@ public function testDetectLocale(
string $expected
): void {
$settings = new LocaleSettings(
new Config(
[
[
'Site' => ['language' => $default],
'Languages' => $enabled,
]
)
);

$this->assertSame(
Expand Down