diff --git a/classes/Components/I18n/Bootstrap.php b/classes/Components/I18n/Bootstrap.php index f2e321ab..586eb8f2 100644 --- a/classes/Components/I18n/Bootstrap.php +++ b/classes/Components/I18n/Bootstrap.php @@ -23,6 +23,66 @@ final class Bootstrap + /** + * Replaces umlauts with their 2 character representation. + * + * @param string $string + * + * @return array|string|string[] + */ + public static function replaceUmlauts(string $string) + { + $search = ['ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß']; + $replace = ['ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss']; + return str_replace($search, $replace, $string); + } + + + + /** + * Find the language information from the given string. + * + * @param string $lang + * + * @return array|null + */ + public static function findLanguage(string $lang): ?array + { + $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 $val; + } + } + return null; + } + + + + /** + * Find the region information from the given string. + * + * @param string $region + * + * @return array|null + */ + public static function findRegion(string $region): ?array + { + $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 $val; + } + } + return null; + } + + + /** * This is the factory for the Localization object. * @@ -51,36 +111,14 @@ final class Bootstrap ['id' => $user->GetAdresse()] ); - // Get language from user account and normalize to 3-letter-code and 2-letter-code - $userSprache = strtolower($user->GetSprache()); - $userLang2 = null; - $userLang3 = null; - foreach ((new Iso639(new Iso639\Filter\CentralEurope())) as $key => $val) { - if (array_filter($val, function ($str) use ($userSprache) { - return $str && (strtolower($str) == $userSprache); - })) { - $userLang2 = $val[Iso639\Key::ALPHA_2]; - $userLang3 = $val[Iso639\Key::ALPHA_3]; - } + if ($lang = self::findLanguage($user->GetSprache())) { + $usersettings['language'] = $lang[Iso639\Key::ALPHA_3]; } - if ($userLang3) { - $usersettings['language'] = $userLang3; - } - // Get region from user account and normalize to 2-letter-code - $userLand = strtolower($userAddress['land'] ?? ''); - $userRegion = null; - foreach ((new Iso3166(new Iso3166\Filter\CentralEurope())) as $key => $val) { - if (array_filter($val, function ($str) use ($userLand) { - return $str && (strtolower($str) == $userLand); - })) { - $userRegion = $val[Iso3166\Key::ALPHA_2]; - } - } - if ($userLang2 && $userRegion) { - $usersettings['locale'] = "{$userLang2}_{$userRegion}"; + if ($lang && ($region = self::findRegion($userAddress['land']))) { + $usersettings['locale'] = "{$lang[Iso639\Key::ALPHA_2]}_{$region[Iso3166\Key::ALPHA_2]}"; } } diff --git a/classes/Components/I18n/Localization.php b/classes/Components/I18n/Localization.php index 9b6ec431..234d479f 100644 --- a/classes/Components/I18n/Localization.php +++ b/classes/Components/I18n/Localization.php @@ -123,7 +123,6 @@ final class Localization implements LocalizationInterface // Set the default locale Locale::setDefault($locale); - // error_log(self::class . ": {$locale}"); } @@ -203,4 +202,36 @@ final class Localization implements LocalizationInterface } + + /** + * Return a new localization object using the given adresse array as source for language and region. + * + * @param array $adresse + * + * @return $this + */ + public function withAdresse(array $adresse): self + { + $localization = clone $this; + + // Find language from address array or keep current language + if (!$lang = Bootstrap::findLanguage($adresse['sprache'])) { + $lang = Bootstrap::findLanguage($this->getLanguage()); + } + if ($lang) { + $localization->setLanguage($lang[Iso639\Key::ALPHA_3]); + } + + // Find region from address or keep current region + if (!$region = Bootstrap::findRegion($adresse['land'])) { + $parsedLocale = Locale::parseLocale($this->getLocale()); + $region = Bootstrap::findRegion($parsedLocale['region']); + } + if ($lang && $region) { + $localization->setLocale("{$lang[Iso639\Key::ALPHA_2]}_{$region[Iso3166\Key::ALPHA_2]}"); + } + + return $localization; + } + } \ No newline at end of file diff --git a/classes/Components/I18n/data/Iso639data.php b/classes/Components/I18n/data/Iso639data.php index 5ff3ed7b..6bfee932 100644 --- a/classes/Components/I18n/data/Iso639data.php +++ b/classes/Components/I18n/data/Iso639data.php @@ -151,5 +151,8 @@ return [ 'NAME_fra' => 'romanche', 'NAME_deu' => 'Rätoromanisch', ], - + 'dut' => ['639-2' => 'dut', '639-1' => 'nl', 'NAME_eng' => 'Dutch', 'NAME_fra' => 'néerlandais', 'NAME_deu' => 'Niederländisch', 'NAME_deu_alt' => 'Holländisch'], + 'swe' => ['639-2' => 'swe', '639-1' => 'sv', 'NAME_eng' => 'Swedish', 'NAME_fra' => 'suédois', 'NAME_deu' => 'Schwedisch'], + 'dan' => ['639-2' => 'dan', '639-1' => 'da', 'NAME_eng' => 'Danish', 'NAME_fra' => 'danois', 'NAME_deu' => 'Dänisch'], + 'nor' => ['639-2' => 'nor', '639-1' => 'no', 'NAME_eng' => 'Norwegian', 'NAME_fra' => 'norvégien', 'NAME_deu' => 'Norwegisch'], ]; diff --git a/www/widgets/templates/_gen/adresse.tpl b/www/widgets/templates/_gen/adresse.tpl index a78b7a56..b1665378 100644 --- a/www/widgets/templates/_gen/adresse.tpl +++ b/www/widgets/templates/_gen/adresse.tpl @@ -149,11 +149,12 @@ $(document).ready(function(){ {|Liefersperre Grund|}:[LIEFERSPERREGRUND][MSGLIEFERSPERREGRUND]
{|Sprache für Belege|}:[SPRACHE][MSGSPRACHE] + {|Sprache und Region|}:[LOCALE][MSGLOCALE] {|Kundenfreigabe|}:[KUNDENFREIGABE][MSGKUNDENFREIGABE]
- {|Folgebestätigungsperre|}:[FOLGEBESTAETIGUNGSPERRE][MSGFOLGEBESTAETIGUNGSPERRE] - {|Trackingmailsperre|}:[TRACKINGSPERRE][MSGTRACKINGSPERRE] + {|Folgebestätigungsperre|}:[FOLGEBESTAETIGUNGSPERRE][MSGFOLGEBESTAETIGUNGSPERRE] + {|Trackingmailsperre|}:[TRACKINGSPERRE][MSGTRACKINGSPERRE] {|Marketingsperre|}:[MARKETINGSPERRE][MSGMARKETINGSPERRE] {|Lead|}:[LEAD][MSGLEAD] diff --git a/www/widgets/widget.adresse.php b/www/widgets/widget.adresse.php index 0a371d14..32e9018b 100644 --- a/www/widgets/widget.adresse.php +++ b/www/widgets/widget.adresse.php @@ -1,4 +1,7 @@ AddOptionsSimpleArray($sprachenOptions); $this->form->NewField($field); - + + /** @var \Xentral\Components\I18n\Localization $localization */ + $localization=$this->app->Container->get('Localization'); + /** @var Database $db */ + $db = $this->app->Container->get('Database'); + $adresse = $db->fetchRow( + $db->select()->cols(['*'])->from('adresse')->where('id=:id'), + ['id' => $id] + ); + $localization=$localization->withAdresse($adresse); + $field = new HTMLInput("locale","text",$localization->getLocale(), size: '10', disabled: 'disabled'); + $this->form->NewField($field); + + $field = new HTMLInput("vorname","hidden",""); $this->form->NewField($field);