-
Hahn Axel (hahn) authoredHahn Axel (hahn) authored
lang.class.php 1.42 KiB
<?php
class lang
{
/**
* Language code with 2 digits
* @var string
*/
protected string $sLang = 'en';
/**
* Array with all translation texts of a language
* @var array
*/
protected array $aLang = [];
/**
* Constructor
* @param string $sLang optional: language to set
*/
public function __construct(string $sLang='')
{
if($sLang){
$this->setLang($sLang);
}
}
/**
* Set a Language; it loads a JSON file with translations
* @param string $sLanguageCode 2 digit language code
* @return bool
*/
public function setLang(string $sLanguageCode = ''): bool
{
$sLlangfile = dirname((__DIR__))."/lang/$sLanguageCode.json";
if (!file_exists($sLlangfile)) {
new Exception("ERROR: Language file $sLlangfile does not exist.");
return false;
}
$aTmp = json_decode(file_get_contents($sLlangfile), 1);
if(!is_array($aTmp)){
new Exception("ERROR: Language file $sLlangfile is not valid JSON.");
return false;
}
$this->aLang = $aTmp;
$this->sLang = $sLanguageCode;
return true;
}
/**
* Get the translated text for a given key
* @param string $s
* @return string
*/
public function t($sKeyword): string
{
return $this->aLang[$sKeyword] ?? "MISS [$sKeyword]";
}
}