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

This commit is contained in:
Roland Rusch 2023-08-14 13:51:19 +02:00
parent bd0392698d
commit fdafc13e2c
5 changed files with 120 additions and 31 deletions

View File

@ -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]}";
}
}

View File

@ -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;
}
}

View File

@ -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'],
];

View File

@ -149,11 +149,12 @@ $(document).ready(function(){
<tr valign="top"><td>{|Liefersperre Grund|}:</td><td>[LIEFERSPERREGRUND][MSGLIEFERSPERREGRUND]</td></tr>
<tr><td colspan="2"><br></td></tr>
<tr><td>{|Sprache f&uuml;r Belege|}:</td><td>[SPRACHE][MSGSPRACHE]</td></tr>
<tr><td>{|Sprache und Region|}:</td><td>[LOCALE][MSGLOCALE]</td></tr>
<tr><td>{|Kundenfreigabe|}:</td><td>[KUNDENFREIGABE][MSGKUNDENFREIGABE] </td></tr>
<tr><td colspan="2"><br></td></tr>
<tr><td>{|Folgebest&auml;tigungsperre|}:</td><td>[FOLGEBESTAETIGUNGSPERRE][MSGFOLGEBESTAETIGUNGSPERRE]</td></tr>
<tr><td>{|Trackingmailsperre|}:</td><td>[TRACKINGSPERRE][MSGTRACKINGSPERRE]</td></tr>
<tr><td>{|Folgebest&auml;tigungsperre|}:</td><td>[FOLGEBESTAETIGUNGSPERRE][MSGFOLGEBESTAETIGUNGSPERRE]</td></tr>
<tr><td>{|Trackingmailsperre|}:</td><td>[TRACKINGSPERRE][MSGTRACKINGSPERRE]</td></tr>
<tr><td>{|Marketingsperre|}:</td><td>[MARKETINGSPERRE][MSGMARKETINGSPERRE]</td></tr>
<tr><td>{|Lead|}:</td><td>[LEAD][MSGLEAD]</td></tr>

View File

@ -1,4 +1,7 @@
<?php
use Xentral\Components\Database\Database;
include ("_gen/widget.gen.adresse.php");
class WidgetAdresse extends WidgetGenAdresse
@ -285,6 +288,19 @@ class WidgetAdresse extends WidgetGenAdresse
$field->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);