web-base/core/Objects/Language.class.php

138 lines
3.3 KiB
PHP
Raw Normal View History

2020-02-09 23:02:19 +01:00
<?php
2020-02-10 00:52:25 +01:00
namespace Objects {
2020-02-09 23:02:19 +01:00
2020-06-19 17:16:32 +02:00
use Objects\lang\LanguageModule;
2020-04-03 15:56:04 +02:00
class Language extends ApiObject {
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
const LANG_CODE_PATTERN = "/^[a-zA-Z]+_[a-zA-Z]+$/";
2020-02-09 23:02:19 +01:00
2020-04-03 15:56:04 +02:00
private int $languageId;
private string $langCode;
private string $langName;
private array $modules;
2020-02-09 23:02:19 +01:00
2020-04-03 15:56:04 +02:00
protected array $entries;
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
public function __construct($languageId, $langCode, $langName) {
$this->languageId = $languageId;
$this->langCode = $langCode;
$this->langName = $langName;
$this->entries = array();
$this->modules = array();
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
public function getId() { return $this->languageId; }
2021-04-02 22:47:11 +02:00
public function getCode(): string { return $this->langCode; }
2020-02-10 00:52:25 +01:00
public function getShortCode() { return substr($this->langCode, 0, 2); }
public function getName() { return $this->langName; }
/**
* @param $module LanguageModule class or object
*/
public function loadModule($module) {
2020-02-10 00:52:25 +01:00
if(!is_object($module))
$module = new $module;
$aModuleEntries = $module->getEntries($this->langCode);
$this->entries = array_merge($this->entries, $aModuleEntries);
$this->modules[] = $module;
}
2020-02-09 23:02:19 +01:00
public function translate(string $key): string {
2020-02-10 00:52:25 +01:00
if(isset($this->entries[$key]))
return $this->entries[$key];
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
return $key;
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
public function sendCookie() {
setcookie('lang', $this->langCode, 0, "/", "");
}
2020-02-09 23:02:19 +01:00
public function jsonSerialize(): array {
2020-02-10 00:52:25 +01:00
return array(
'uid' => $this->languageId,
'code' => $this->langCode,
'name' => $this->langName,
);
2020-02-09 23:02:19 +01:00
}
2020-02-10 00:52:25 +01:00
public static function newInstance($languageId, $langCode, $langName) {
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
if(!preg_match(Language::LANG_CODE_PATTERN, $langCode)) {
return false;
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
// TODO: include dynamically wanted Language
return new Language($languageId, $langCode, $langName);
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
// $className = $langCode
// return new $className($languageId, $langCode);
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
public function load() {
global $LANGUAGE;
$LANGUAGE = $this;
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
public static function DEFAULT_LANGUAGE() {
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$aSplit = explode(',',$acceptLanguage);
foreach($aSplit as $code) {
if(strlen($code) == 2) {
$code = $code . '_' . strtoupper($code);
}
$code = str_replace("-", "_", $code);
if(strlen($code) != 5)
continue;
$lang = Language::newInstance(0, $code, "");
if($lang)
return $lang;
}
2020-02-09 23:02:19 +01:00
}
2020-02-10 00:52:25 +01:00
return Language::newInstance(1, "en_US", "American English");
}
};
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
namespace {
2020-04-03 15:56:04 +02:00
function L($key) {
2020-02-10 00:52:25 +01:00
if(!array_key_exists('LANGUAGE', $GLOBALS))
return $key;
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
global $LANGUAGE;
return $LANGUAGE->translate($key);
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
function LANG_NAME() {
if(!array_key_exists('LANGUAGE', $GLOBALS))
return "LANG_NAME";
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
global $LANGUAGE;
return $LANGUAGE->getName();
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
function LANG_CODE() {
if(!array_key_exists('LANGUAGE', $GLOBALS))
return "LANG_CODE";
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
global $LANGUAGE;
return $LANGUAGE->getCode();
}
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
function SHORT_LANG_CODE() {
if(!array_key_exists('LANGUAGE', $GLOBALS))
return "SHORT_LANG_CODE";
2020-02-09 23:02:19 +01:00
2020-02-10 00:52:25 +01:00
global $LANGUAGE;
return $LANGUAGE->getShortCode();
}
2020-02-09 23:02:19 +01:00
}