diff --git a/classes/lang.class.php b/classes/lang.class.php new file mode 100644 index 0000000000000000000000000000000000000000..e5f9d093f85e5d485b5901baa0efa9a91e249165 --- /dev/null +++ b/classes/lang.class.php @@ -0,0 +1,62 @@ +<?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