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); $subject = strtolower($lang);
foreach ((new Iso639()) as $key => $val) { foreach ((new Iso639()) as $key => $val) {
if (array_filter($val, function ($str) use ($subject) { 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; return $val;
} }
@ -83,7 +83,7 @@ final class Bootstrap
$subject = strtolower($region); $subject = strtolower($region);
foreach ((new Iso3166()) as $key => $val) { foreach ((new Iso3166()) as $key => $val) {
if (array_filter($val, function ($str) use ($subject) { 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; return $val;
} }
@ -112,8 +112,6 @@ final class Bootstrap
$db = $container->get('Database'); $db = $container->get('Database');
$config=[]; $config=[];
$firmaLang=null;
$firmaRegion=null;
// Get language from system settings and normalize to 3-letter-code and 2-letter-code // Get language from system settings and normalize to 3-letter-code and 2-letter-code
if ($firmaLang = self::findLanguage(strval($app->erp->Firmendaten('preferredLanguage')))) { if ($firmaLang = self::findLanguage(strval($app->erp->Firmendaten('preferredLanguage')))) {
$config[Localization::LANGUAGE_DEFAULT] = $firmaLang[Iso639\Key::ALPHA_3]; $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 // 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]}"; $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->session = $session;
$this->usersettings = $usersettings; $this->usersettings = $usersettings;
$this->config = $config; $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(); $this->process();
} }
@ -57,11 +62,11 @@ final class Localization implements LocalizationInterface
$segmentName = 'i18n'; $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 // Get the locale from the session, if available
if ($this->session) { if ($this->session && ($sessionLocale = $this->session->getValue($segmentName, $localeAttrName))) {
$this->setLocale((string)$this->session->getValue($segmentName, $localeAttrName, ''), $this->getLocale()); $this->setLocale((string)$sessionLocale, $this->getLocale());
} else { } else {
// Get locale from request, fallback to the user's browser preference // Get locale from request, fallback to the user's browser preference
if ($this->request) { if ($this->request) {
@ -124,7 +129,6 @@ final class Localization implements LocalizationInterface
// Store the locale and language to the LocalizationInterface // Store the locale and language to the LocalizationInterface
$this->setLanguage($language); $this->setLanguage($language);
// $this->setLocale($locale);
// Store the locale and language to the session // Store the locale and language to the session
if ($this->session) { if ($this->session) {
@ -194,20 +198,24 @@ final class Localization implements LocalizationInterface
*/ */
public function setLocale(string $locale, string|null $fallbackLocale = null): void public function setLocale(string $locale, string|null $fallbackLocale = null): void
{ {
// Check if locale is already set if(!empty($locale)) {
try { // Check if locale is already set
if ($locale == $this->getLocale()) { try {
return; if ($locale == $this->getLocale()) {
} return;
} catch (LocaleNotSetException $e) { }
}; } catch (LocaleNotSetException $e) {
};
// Parse and re-compose locale to make sure, it is sane
$parsedLocale = Locale::parseLocale($locale); // Parse and re-compose locale to make sure, it is sane
$composedLocale = Locale::composeLocale([ $parsedLocale = Locale::parseLocale($locale);
'language' => $parsedLocale['language'], $composedLocale = Locale::composeLocale([
'region' => $parsedLocale['region'], 'language' => $parsedLocale['language'],
]); 'region' => $parsedLocale['region'],
]);
} else {
$composedLocale=null;
}
// If sanity check fails, set fallbackLocale if present // If sanity check fails, set fallbackLocale if present
// throw exception otherwise // throw exception otherwise