From 15fb7f86b0aa1d4780d0545ffdbcd1dee6471159 Mon Sep 17 00:00:00 2001 From: Max Pozdeev Date: Thu, 24 Sep 2020 19:21:13 +0300 Subject: [PATCH] dont die if language file not found --- src/includes/class.lang.php | 48 +++++++++++++++++++++++++++++-------- src/init.php | 8 +++---- src/mytinytodo_lang.php | 2 ++ src/settings.php | 10 ++++---- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/includes/class.lang.php b/src/includes/class.lang.php index 2b766c4..ae59432 100644 --- a/src/includes/class.lang.php +++ b/src/includes/class.lang.php @@ -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; } } diff --git a/src/init.php b/src/init.php index 8c8d44b..df2982f 100644 --- a/src/init.php +++ b/src/init.php @@ -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(); diff --git a/src/mytinytodo_lang.php b/src/mytinytodo_lang.php index d239fb1..3908bbb 100644 --- a/src/mytinytodo_lang.php +++ b/src/mytinytodo_lang.php @@ -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'); diff --git a/src/settings.php b/src/settings.php index c83f02c..460e511 100644 --- a/src/settings.php +++ b/src/settings.php @@ -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 '';