Removed eval(), redone get & load language functions
This commit is contained in:
parent
b0ea7eb272
commit
a96e1e5a2e
|
@ -39,67 +39,55 @@
|
||||||
*/
|
*/
|
||||||
function psm_get_lang() {
|
function psm_get_lang() {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
|
|
||||||
if (empty($args)) return $GLOBALS['sm_lang'];
|
if (empty($args))
|
||||||
|
return isset($GLOBALS['sm_lang']) ? $GLOBALS['sm_lang'] : $GLOBALS['sm_lang_default'];
|
||||||
$result = null;
|
|
||||||
$resultDefault = null;
|
if(isset($GLOBALS['sm_lang'])) {
|
||||||
$node = null;
|
$lang = $GLOBALS['sm_lang'];
|
||||||
$nodeDefault = null;
|
$not_found = false;
|
||||||
|
foreach($args as $translation) {
|
||||||
if ($args) {
|
// if translation does not exist, use default translation
|
||||||
$node = '$GLOBALS[\'sm_lang\'][\'' . implode('\'][\'', $args) . '\']';
|
if(!isset($lang[$translation])) {
|
||||||
$nodeDefault = '$GLOBALS[\'sm_lang_default\'][\'' . implode('\'][\'', $args) . '\']';
|
$not_found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$lang = $lang[$translation];
|
||||||
|
}
|
||||||
|
if(!$not_found) return $lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
eval('if (isset(' . $node . ')) $result = ' . $node . ';');
|
$lang = $GLOBALS['sm_lang_default'];
|
||||||
eval('if (isset(' . $nodeDefault . ')) $resultDefault = ' . $nodeDefault . ';');
|
foreach($args as $translation) {
|
||||||
|
$lang = $lang[$translation];
|
||||||
if (empty($result)) {
|
|
||||||
return $resultDefault;
|
|
||||||
} else {
|
|
||||||
return $result;
|
|
||||||
}
|
}
|
||||||
|
return $lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load language from the language file to the $GLOBALS['sm_lang'] variable
|
* Load default language from the English (en_US) language file to the $GLOBALS['sm_lang_default'] variable
|
||||||
|
* Load language from the language file to the $GLOBALS['sm_lang'] variable if language is different from default
|
||||||
*
|
*
|
||||||
* @param string $lang language
|
* @param string $lang language
|
||||||
* @see psm_get_lang()
|
* @see psm_get_lang()
|
||||||
*/
|
*/
|
||||||
function psm_load_lang($lang) {
|
function psm_load_lang($lang) {
|
||||||
// if not in the language translation must always be available starting translation - English
|
// load default language - English (en_US)
|
||||||
|
// this will also fill in every part that is not translated in other translation files
|
||||||
$default_lang_file = PSM_PATH_LANG . 'en_US.lang.php';
|
$default_lang_file = PSM_PATH_LANG . 'en_US.lang.php';
|
||||||
|
|
||||||
if (file_exists($default_lang_file)) {
|
file_exists($default_lang_file) ? require $default_lang_file : die('English translation needs to be intalled at all time!');
|
||||||
require $default_lang_file;
|
isset($sm_lang) ? $GLOBALS['sm_lang_default'] = $sm_lang : die('$sm_lang not found in English translation!');
|
||||||
|
unset($sm_lang);
|
||||||
|
|
||||||
if (isset($sm_lang)) {
|
// load translation is the selected language is not English (en_US)
|
||||||
$GLOBALS['sm_lang_default'] = $sm_lang;
|
if($lang != "en_US"){
|
||||||
unset($sm_lang);
|
$lang_file = PSM_PATH_LANG . $lang . '.lang.php';
|
||||||
}
|
file_exists($lang_file) ? require $lang_file : trigger_error("Translation file could not be found! Default language will be used.", E_USER_WARNING);
|
||||||
|
|
||||||
|
isset($sm_lang) ? $GLOBALS['sm_lang'] = $sm_lang : trigger_error("\$sm_lang not found in translation file! Default language will be used.", E_USER_WARNING);
|
||||||
|
isset($sm_lang['locale']) ? setlocale(LC_TIME, $sm_lang['locale']) : trigger_error("locale could not ben found in translation file.", E_USER_WARNING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// translated language
|
|
||||||
$lang_file = PSM_PATH_LANG . $lang . '.lang.php';
|
|
||||||
|
|
||||||
if (!file_exists($lang_file)) {
|
|
||||||
// If the file has been removed, we use the english one
|
|
||||||
$en_file = PSM_PATH_LANG . 'en_US.lang.php';
|
|
||||||
if (!file_exists($en_file)) {
|
|
||||||
// OK, nothing we can do
|
|
||||||
die('unable to load language file: ' . $lang_file);
|
|
||||||
}
|
|
||||||
$lang_file = $en_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
require $lang_file;
|
|
||||||
if (isset($sm_lang['locale'])) {
|
|
||||||
setlocale(LC_TIME, $sm_lang['locale']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$GLOBALS['sm_lang'] = $sm_lang;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue