RETROTEC-AG/OpenXE#17 Locale des GUI-Users ermitteln

Bessere Überprüfung der gegebenen locale
This commit is contained in:
Roland Rusch 2023-08-17 23:22:46 +02:00
parent 551c0c74c0
commit 484b96b481
2 changed files with 29 additions and 23 deletions

View File

@ -61,7 +61,7 @@ final class Bootstrap
$subject = strtolower($lang);
foreach ((new Iso639()) as $key => $val) {
if (array_filter($val, function ($str) use ($subject) {
return $str && ((strtolower($str) == $subject) || (self::replaceUmlauts(strtolower($str)) == $subject));
return $str && ((strtolower($str) == $subject) || (strtolower(self::replaceUmlauts($str)) == $subject));
})) {
return $val;
}
@ -83,7 +83,7 @@ final class Bootstrap
$subject = strtolower($region);
foreach ((new Iso3166()) as $key => $val) {
if (array_filter($val, function ($str) use ($subject) {
return $str && ((strtolower($str) == $subject) || (self::replaceUmlauts(strtolower($str)) == $subject));
return $str && ((strtolower($str) == $subject) || (strtolower(self::replaceUmlauts($str)) == $subject));
})) {
return $val;
}
@ -112,8 +112,6 @@ final class Bootstrap
$db = $container->get('Database');
$config=[];
$firmaLang=null;
$firmaRegion=null;
// Get language from system settings and normalize to 3-letter-code and 2-letter-code
if ($firmaLang = self::findLanguage(strval($app->erp->Firmendaten('preferredLanguage')))) {
$config[Localization::LANGUAGE_DEFAULT] = $firmaLang[Iso639\Key::ALPHA_3];
@ -140,7 +138,7 @@ final class Bootstrap
}
// Get region from user account and normalize to 2-letter-code
if ($userLang && ($userRegion = self::findRegion(strval($userAddress['land'] ?? '')))) {
if ($userLang && ($userRegion = self::findRegion(strval($userAddress['land'])))) {
$usersettings['locale'] = "{$userLang[Iso639\Key::ALPHA_2]}_{$userRegion[Iso3166\Key::ALPHA_2]}";
}
}

View File

@ -42,6 +42,11 @@ final class Localization implements LocalizationInterface
$this->session = $session;
$this->usersettings = $usersettings;
$this->config = $config;
// Set config if no default is given
$this->config[Localization::LOCALE_DEFAULT]=$this->config[Localization::LOCALE_DEFAULT] ?? 'de_DE';
$this->config[Localization::LANGUAGE_DEFAULT]=$this->config[Localization::LANGUAGE_DEFAULT] ?? 'deu';
$this->process();
}
@ -57,11 +62,11 @@ final class Localization implements LocalizationInterface
$segmentName = 'i18n';
$this->setLocale((string)$this->config[Localization::LOCALE_DEFAULT], 'de_DE');
$this->setLocale((string)$this->config[Localization::LOCALE_DEFAULT]);
// Get the locale from the session, if available
if ($this->session) {
$this->setLocale((string)$this->session->getValue($segmentName, $localeAttrName, ''), $this->getLocale());
if ($this->session && ($sessionLocale = $this->session->getValue($segmentName, $localeAttrName))) {
$this->setLocale((string)$sessionLocale, $this->getLocale());
} else {
// Get locale from request, fallback to the user's browser preference
if ($this->request) {
@ -124,7 +129,6 @@ final class Localization implements LocalizationInterface
// Store the locale and language to the LocalizationInterface
$this->setLanguage($language);
// $this->setLocale($locale);
// Store the locale and language to the session
if ($this->session) {
@ -194,20 +198,24 @@ final class Localization implements LocalizationInterface
*/
public function setLocale(string $locale, string|null $fallbackLocale = null): void
{
// Check if locale is already set
try {
if ($locale == $this->getLocale()) {
return;
}
} catch (LocaleNotSetException $e) {
};
// Parse and re-compose locale to make sure, it is sane
$parsedLocale = Locale::parseLocale($locale);
$composedLocale = Locale::composeLocale([
'language' => $parsedLocale['language'],
'region' => $parsedLocale['region'],
]);
if(!empty($locale)) {
// Check if locale is already set
try {
if ($locale == $this->getLocale()) {
return;
}
} catch (LocaleNotSetException $e) {
};
// Parse and re-compose locale to make sure, it is sane
$parsedLocale = Locale::parseLocale($locale);
$composedLocale = Locale::composeLocale([
'language' => $parsedLocale['language'],
'region' => $parsedLocale['region'],
]);
} else {
$composedLocale=null;
}
// If sanity check fails, set fallbackLocale if present
// throw exception otherwise