PHP_SMART_HOME_V3/app/class/LangManager.php

60 lines
1.0 KiB
PHP
Raw Normal View History

2019-10-11 10:24:20 +00:00
<?php
/**
* Language Manager
*/
class LanguageManager
{
private $lngCode = 'en';
private $lngDatabase = [];
2019-10-11 12:12:05 +00:00
private $debug = false;
2019-10-11 10:24:20 +00:00
2019-10-11 12:12:05 +00:00
function __construct(string $lngCode, bool $debug = false)
2019-10-11 10:24:20 +00:00
{
$this->lngCode = $lngCode;
2019-10-11 12:12:05 +00:00
$this->debug = $debug;
2019-10-11 10:24:20 +00:00
}
function load()
{
$file = './app/lang/en.php';
2019-10-11 12:12:05 +00:00
if (!file_exists($file)){
die();
//TODO add lng EXEPTIONS
}
2019-10-11 10:24:20 +00:00
$arrayFirst = include($file);
$file = './app/lang/' . $this->lngCode . '.php';
2019-10-11 12:12:05 +00:00
$arraySecond = [];
if (file_exists($file)){
$arraySecond = include($file);
}
2019-10-11 10:24:20 +00:00
$this->lngDatabase = array_merge($arrayFirst,$arraySecond);
return true;
}
function get(string $stringKey)
{
2019-10-11 12:12:05 +00:00
if ($this->debug) {
return $stringKey;
}
2019-10-11 10:24:20 +00:00
if (isset($this->lngDatabase[$stringKey])) {
return $this->lngDatabase[$stringKey];
}
return $stringKey;
}
function echo(string $stringKey)
{
2019-10-11 12:12:05 +00:00
if ($this->debug) {
echo $stringKey;
return;
}
2019-10-11 10:24:20 +00:00
if (isset($this->lngDatabase[$stringKey])) {
2019-10-11 12:12:05 +00:00
echo $this->lngDatabase[$stringKey];
return;
2019-10-11 10:24:20 +00:00
}
2019-10-11 12:12:05 +00:00
echo $stringKey;
return;
2019-10-11 10:24:20 +00:00
}
}