web-base/Core/Objects/DatabaseEntity/Language.class.php

165 lines
4.4 KiB
PHP
Raw Permalink Normal View History

2022-06-20 19:52:31 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Objects\DatabaseEntity {
2022-06-20 19:52:31 +02:00
2022-11-18 18:06:46 +01:00
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
use Core\Objects\DatabaseEntity\Attribute\Transient;
2022-11-20 17:13:53 +01:00
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
2022-06-20 19:52:31 +02:00
class Language extends DatabaseEntity {
2022-11-20 17:13:53 +01:00
const AMERICAN_ENGLISH = 1;
const GERMAN_STANDARD = 2;
2022-06-20 19:52:31 +02:00
const LANG_CODE_PATTERN = "/^[a-zA-Z]{2}_[a-zA-Z]{2}$/";
2022-11-30 16:42:24 +01:00
const LANG_MODULE_PATTERN = "/[a-zA-Z0-9_-]/";
2022-06-20 19:52:31 +02:00
#[MaxLength(5)] private string $code;
#[MaxLength(32)] private string $name;
2022-08-20 22:17:17 +02:00
#[Transient] protected array $entries = [];
2022-06-20 19:52:31 +02:00
2024-05-06 15:30:43 +02:00
public function __construct(?int $id, string $code, string $name) {
2022-06-20 19:52:31 +02:00
parent::__construct($id);
$this->code = $code;
$this->name = $name;
}
public function getCode(): string {
return $this->code;
}
public function getShortCode(): string {
return substr($this->code, 0, 2);
}
public function getName(): string {
return $this->name;
}
public function sendCookie(string $domain) {
setcookie('lang', $this->code, 0, "/", $domain, false, false);
}
2023-01-07 15:34:05 +01:00
public function jsonSerialize(?array $propertyNames = null): array {
$jsonData = parent::jsonSerialize($propertyNames);
if ($propertyNames === null || in_array("shortCode", $propertyNames)) {
$jsonData["shortCode"] = explode("_", $this->code)[0];
}
return $jsonData;
2022-06-20 19:52:31 +02:00
}
public function activate() {
global $LANGUAGE;
$LANGUAGE = $this;
}
2022-11-30 16:42:24 +01:00
public static function getInstance(): Language {
global $LANGUAGE;
return $LANGUAGE;
}
2024-05-06 15:30:43 +02:00
public static function DEFAULT_LANGUAGE(): Language {
2023-01-25 14:15:34 +01:00
return self::getPredefinedValues()[0];
2022-06-20 19:52:31 +02:00
}
2022-08-20 22:17:17 +02:00
2022-11-30 16:42:24 +01:00
public function getEntries(?string $module = null): ?array {
if (!$module) {
return $this->entries;
} else {
return $this->entries[$module] ?? null;
}
}
public function translate(string $key): string {
if (preg_match("/(\w+)\.(\w+)/", $key, $matches)) {
$module = $matches[1];
$moduleKey = $matches[2];
if ($this->hasModule($module) && array_key_exists($moduleKey, $this->entries[$module])) {
return $this->entries[$module][$moduleKey];
}
}
2023-01-25 14:15:34 +01:00
return $key ? "[$key]" : "";
2022-11-30 16:42:24 +01:00
}
public function addModule(string $module, array $entries) {
if ($this->hasModule($module)) {
$this->entries[$module] = array_merge($this->entries[$module], $entries);
} else {
$this->entries[$module] = $entries;
}
}
2024-05-06 15:30:43 +02:00
public function loadModule(string $module, bool $forceReload = false): bool {
2022-11-30 16:42:24 +01:00
if ($this->hasModule($module) && !$forceReload) {
return true;
}
if (!preg_match(self::LANG_MODULE_PATTERN, $module)) {
return false;
}
if (!preg_match(self::LANG_CODE_PATTERN, $this->code)) {
return false;
}
foreach (["Site", "Core"] as $baseDir) {
$filePath = realpath(implode("/", [$baseDir, "Localization", $this->code, "$module.php"]));
if ($filePath && is_file($filePath)) {
$moduleEntries = @include_once $filePath;
$this->addModule($module, $moduleEntries);
return true;
}
}
return false;
}
public function hasModule(string $module): bool {
return array_key_exists($module, $this->entries);
2022-08-20 22:17:17 +02:00
}
2023-01-09 20:27:01 +01:00
public static function getPredefinedValues(): array {
return [
2023-01-25 14:15:34 +01:00
new Language(Language::AMERICAN_ENGLISH, "en_US", 'English (US)'),
new Language(Language::GERMAN_STANDARD, "de_DE", 'Deutsch (Standard)'),
];
}
2024-05-06 15:30:43 +02:00
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;
}
2022-06-20 19:52:31 +02:00
}
}
namespace {
2022-11-30 16:42:24 +01:00
function L(string $key): string {
if (!array_key_exists('LANGUAGE', $GLOBALS)) {
2023-01-25 14:15:34 +01:00
return $key ? "[$key]" : "";
2022-11-30 16:42:24 +01:00
}
2022-06-20 19:52:31 +02:00
global $LANGUAGE;
return $LANGUAGE->translate($key);
}
}