default localization fix

This commit is contained in:
Roman 2024-05-06 15:30:43 +02:00
parent 623e5dbbc8
commit 975d434add
3 changed files with 39 additions and 23 deletions

@ -102,7 +102,7 @@ namespace Core\API {
$sql = $this->context->getSQL(); $sql = $this->context->getSQL();
$user = new User(); $user = new User();
$user->language = Language::DEFAULT_LANGUAGE(false); $user->language = Language::DEFAULT_LANGUAGE();
$user->registeredAt = new \DateTime(); $user->registeredAt = new \DateTime();
$user->password = $this->hashPassword($password); $user->password = $this->hashPassword($password);
$user->name = $username; $user->name = $username;

@ -63,6 +63,17 @@ class Context {
} }
public function setLanguage(Language $language): void { 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 = $language;
$this->language->activate(); $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"])) { if (isset($_GET['lang']) && is_string($_GET["lang"]) && !empty($_GET["lang"])) {
$this->updateLanguage($_GET['lang']); $this->updateLanguage($_GET['lang']);
} else if (isset($_COOKIE['lang']) && is_string($_COOKIE["lang"]) && !empty($_COOKIE["lang"])) { } else if (isset($_COOKIE['lang']) && is_string($_COOKIE["lang"]) && !empty($_COOKIE["lang"])) {
$this->updateLanguage($_COOKIE['lang']); $this->updateLanguage($_COOKIE['lang']);
} else if ($this->user) { } else if ($this->user) {
$this->setLanguage($this->user->language); $this->setLanguage($this->user->language);
} else {
$this->setLanguage(Language::fromHeader());
} }
} }

@ -6,7 +6,6 @@ namespace Core\Objects\DatabaseEntity {
use Core\Objects\DatabaseEntity\Attribute\Transient; use Core\Objects\DatabaseEntity\Attribute\Transient;
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity; use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
// TODO: language from cookie?
class Language extends DatabaseEntity { class Language extends DatabaseEntity {
const AMERICAN_ENGLISH = 1; const AMERICAN_ENGLISH = 1;
@ -20,7 +19,7 @@ namespace Core\Objects\DatabaseEntity {
#[Transient] protected array $entries = []; #[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); parent::__construct($id);
$this->code = $code; $this->code = $code;
$this->name = $name; $this->name = $name;
@ -62,24 +61,7 @@ namespace Core\Objects\DatabaseEntity {
return $LANGUAGE; return $LANGUAGE;
} }
public static function DEFAULT_LANGUAGE(bool $fromCookie = true): Language { public static function DEFAULT_LANGUAGE(): 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, "");
}
}
return self::getPredefinedValues()[0]; 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) { if ($this->hasModule($module) && !$forceReload) {
return true; return true;
} }
@ -146,6 +128,27 @@ namespace Core\Objects\DatabaseEntity {
new Language(Language::GERMAN_STANDARD, "de_DE", 'Deutsch (Standard)'), 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;
}
} }
} }