From 975d434adde4f975ae6786885e77bfb1d379cbfd Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 6 May 2024 15:30:43 +0200 Subject: [PATCH] default localization fix --- Core/API/UserAPI.class.php | 2 +- Core/Objects/Context.class.php | 15 ++++++- .../Objects/DatabaseEntity/Language.class.php | 45 ++++++++++--------- 3 files changed, 39 insertions(+), 23 deletions(-) diff --git a/Core/API/UserAPI.class.php b/Core/API/UserAPI.class.php index 949457b..b9fdce6 100644 --- a/Core/API/UserAPI.class.php +++ b/Core/API/UserAPI.class.php @@ -102,7 +102,7 @@ namespace Core\API { $sql = $this->context->getSQL(); $user = new User(); - $user->language = Language::DEFAULT_LANGUAGE(false); + $user->language = Language::DEFAULT_LANGUAGE(); $user->registeredAt = new \DateTime(); $user->password = $this->hashPassword($password); $user->name = $username; diff --git a/Core/Objects/Context.class.php b/Core/Objects/Context.class.php index 5e03ba2..a55f5b2 100644 --- a/Core/Objects/Context.class.php +++ b/Core/Objects/Context.class.php @@ -63,6 +63,17 @@ class Context { } public function setLanguage(Language $language): void { + + // 1st: check if the language really exists... + if ($language->getId() === null) { + $language = Language::findBy(Language::createBuilder($this->sql, true) + ->whereEq("code", $language->getCode())); + if ($language === false || $language === null) { + // if not, load the default language + $language = Language::DEFAULT_LANGUAGE(); + } + } + $this->language = $language; $this->language->activate(); @@ -120,13 +131,15 @@ class Context { } } - // set language by priority: 1. GET parameter, 2. cookie, 3. user's settings + // set language by priority: 1. GET parameter, 2. cookie, 3. user's settings, 4. accept-language header if (isset($_GET['lang']) && is_string($_GET["lang"]) && !empty($_GET["lang"])) { $this->updateLanguage($_GET['lang']); } else if (isset($_COOKIE['lang']) && is_string($_COOKIE["lang"]) && !empty($_COOKIE["lang"])) { $this->updateLanguage($_COOKIE['lang']); } else if ($this->user) { $this->setLanguage($this->user->language); + } else { + $this->setLanguage(Language::fromHeader()); } } diff --git a/Core/Objects/DatabaseEntity/Language.class.php b/Core/Objects/DatabaseEntity/Language.class.php index 6ffafd6..8933cb5 100644 --- a/Core/Objects/DatabaseEntity/Language.class.php +++ b/Core/Objects/DatabaseEntity/Language.class.php @@ -6,7 +6,6 @@ namespace Core\Objects\DatabaseEntity { use Core\Objects\DatabaseEntity\Attribute\Transient; use Core\Objects\DatabaseEntity\Controller\DatabaseEntity; - // TODO: language from cookie? class Language extends DatabaseEntity { const AMERICAN_ENGLISH = 1; @@ -20,7 +19,7 @@ namespace Core\Objects\DatabaseEntity { #[Transient] protected array $entries = []; - public function __construct(int $id, string $code, string $name) { + public function __construct(?int $id, string $code, string $name) { parent::__construct($id); $this->code = $code; $this->name = $name; @@ -62,24 +61,7 @@ namespace Core\Objects\DatabaseEntity { return $LANGUAGE; } - public static function DEFAULT_LANGUAGE(bool $fromCookie = true): Language { - if ($fromCookie && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { - $acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE']; - $acceptedLanguages = explode(',', $acceptLanguage); - foreach ($acceptedLanguages as $code) { - if (strlen($code) == 2) { - $code = $code . '_' . strtoupper($code); - } - - $code = str_replace("-", "_", $code); - if (!preg_match(self::LANG_CODE_PATTERN, $code)) { - continue; - } - - return new Language(0, $code, ""); - } - } - + public static function DEFAULT_LANGUAGE(): Language { return self::getPredefinedValues()[0]; } @@ -111,7 +93,7 @@ namespace Core\Objects\DatabaseEntity { } } - public function loadModule(string $module, bool $forceReload=false): bool { + public function loadModule(string $module, bool $forceReload = false): bool { if ($this->hasModule($module) && !$forceReload) { return true; } @@ -146,6 +128,27 @@ namespace Core\Objects\DatabaseEntity { new Language(Language::GERMAN_STANDARD, "de_DE", 'Deutsch (Standard)'), ]; } + + public static function fromHeader(): ?Language { + if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { + $acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE']; + $acceptedLanguages = explode(',', $acceptLanguage); + foreach ($acceptedLanguages as $code) { + if (strlen($code) == 2) { + $code = $code . '_' . strtoupper($code); + } + + $code = str_replace("-", "_", $code); + if (!preg_match(self::LANG_CODE_PATTERN, $code)) { + continue; + } + + return new Language(NULL, $code, ""); + } + } + + return null; + } } }