mirror of
https://github.com/OpenXE-org/OpenXE.git
synced 2024-11-14 20:17:14 +01:00
RETROTEC-AG/OpenXE#17 Locale des GUI-Users ermitteln
This commit is contained in:
parent
bd0392698d
commit
fdafc13e2c
@ -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]}";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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'],
|
||||
];
|
||||
|
@ -149,6 +149,7 @@ $(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ü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>
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user