Skip to content
Snippets Groups Projects
Commit 25d2f1c3 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

add lang class

parent 756968d4
No related branches found
No related tags found
No related merge requests found
<?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]";
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment