dont die if language file not found

This commit is contained in:
Max Pozdeev 2020-09-24 19:21:13 +03:00
parent ac0d635ec1
commit 15fb7f86b0
4 changed files with 49 additions and 19 deletions

View file

@ -7,9 +7,9 @@
class Lang
{
protected static $instance;
protected static $langDir = MTTLANG;
protected $code = 'en';
protected $default = 'en';
protected $langDir = MTTLANG;
protected $strings;
public static function instance()
@ -21,20 +21,35 @@ class Lang
return self::$instance;
}
public static function loadLang($code)
public static function loadLangOrDie($code, $die = 1)
{
$lang = self::instance();
//check if new format (json)
if (file_exists("{$lang->langDir}{$code}.json")) {
$jsonString = file_get_contents("{$lang->langDir}{$code}.json");
//check if json file exists
if ( self::langExists($code) ) {
$jsonString = file_get_contents( $this->langDir(). "{$code}.json" );
$lang->loadJsonString($code, $jsonString);
}
else {
else if ( $die == 0 ) {
//notice?
$lang->code = $lang->default; //sure?
$lang->loadDefaultStrings();
}
else if ( $die == 1 ) {
die("Language file not found ($code.json)");
}
}
public static function loadLang($code)
{
self::loadLangOrDie($code, 0);
}
public static function langExists($code)
{
return file_exists(self::$langDir. $code. '.json');
}
function loadJsonString($code, $jsonString)
{
$this->code = $code;
@ -42,15 +57,23 @@ class Lang
//load default language
if ( $code != $this->default ) {
$defStr = file_get_contents("{$this->langDir}{$this->default}.json");
$default_json = json_decode($defStr, true);
$this->strings = array_replace($default_json, $json);
$this->loadDefaultStrings();
$this->strings = array_replace($this->strings, $json);
}
else {
$this->strings = $json;
}
}
function loadDefaultStrings()
{
if ( ! self::langExists($this->default) ) {
die("Default language file not found ({$this->default}.json)");
}
$defStr = file_get_contents($this->langDir(). "{$this->default}.json");
$this->strings = json_decode($defStr, true);
}
function get($key)
{
if ( isset($this->strings[$key]) ) {
@ -114,7 +137,12 @@ class Lang
function langDir()
{
return $this->langDir;
return self::$langDir;
}
function langCode()
{
return $this->code;
}
}

View file

@ -59,14 +59,14 @@ $db->prefix = Config::get('prefix');
//User can override language setting by cookies or query
$forceLang = '';
if( isset($_COOKIE['lang']) ) $forceLang = $_COOKIE['lang'];
else if ( isset($_GET['lang']) ) $forceLang = $_GET['lang'];
//else if ( isset($_GET['lang']) ) $forceLang = $_GET['lang'];
if ( $forceLang != '' && preg_match("/^[a-z-]+$/i", $forceLang) && file_exists(MTTLANG. $forceLang. '.json') ) {
Config::set('lang', $forceLang);
if ( $forceLang != '' && preg_match("/^[a-z-]+$/i", $forceLang) ) {
Config::set('lang', $forceLang); //TODO: special for demo, do not change config
}
require_once(MTTINC. 'class.lang.php');
Lang::loadLang(Config::get('lang'));
Lang::loadLang( Config::get('lang') );
$_mttinfo = array();

View file

@ -4,6 +4,8 @@
if(!defined('MTTPATH')) define('MTTPATH', dirname(__FILE__) .'/');
if(!defined('MTTINC')) define('MTTINC', MTTPATH. 'includes/');
if(!defined('MTTCONTENT')) define('MTTCONTENT', MTTPATH. 'content/');
if(!defined('MTTLANG')) define('MTTLANG', MTTCONTENT. 'lang/');
require_once(MTTPATH. 'db/config.php');

View file

@ -143,11 +143,11 @@ function selectOptions($a, $value, $default=null)
return $s;
}
/*
@param array $a array of id=>array(name, optional title)
@param mixed $key Key of OPTION to be selected
@param mixed $default Default key if $key is not present in $a
*/
/**
* @param array $a array of id=>array(name, optional title)
* @param mixed $key Key of OPTION to be selected
* @param mixed $default Default key if $key is not present in $a
*/
function selectOptionsA($a, $key, $default=null)
{
if(!$a) return '';