PHP_SMART_HOME_V3/www/app/models/managers/LanguageManager.php

61 lines
1.1 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()
{
2020-04-21 15:59:37 +00:00
$file = '../lang/en.php';
2019-10-11 12:12:05 +00:00
if (!file_exists($file)){
2020-04-21 15:59:37 +00:00
echo 'ERROR: en.php not found';
2019-10-11 12:12:05 +00:00
die();
//TODO add lng EXEPTIONS
}
2019-10-11 10:24:20 +00:00
$arrayFirst = include($file);
2020-04-21 15:59:37 +00:00
$file = '../lang/' . $this->lngCode . '.php';
2019-10-11 12:12:05 +00:00
$arraySecond = [];
if (file_exists($file)){
$arraySecond = include($file);
}
2020-04-21 15:59:37 +00:00
$this->lngDatabase = array_merge($arrayFirst, $arraySecond);
2019-10-11 10:24:20 +00:00
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
}
}