From 6854cb3f4d8219cf1829e32122eb2502a916eae9 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 1 Feb 2020 09:05:48 +0100 Subject: initial checkin --- includes/i18n.inc.php | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 includes/i18n.inc.php (limited to 'includes/i18n.inc.php') diff --git a/includes/i18n.inc.php b/includes/i18n.inc.php new file mode 100644 index 0000000..47d6852 --- /dev/null +++ b/includes/i18n.inc.php @@ -0,0 +1,166 @@ +Only once here for $lang!"; + $language = BASEDIR . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR . $lang . '.php'; + if (is_readable($language)) { + if ((@require $language) !== FALSE) { + // echo "
Loaded: $lang!
"; + self::$translations[$lang] = $translation; + } + else { + $lang = 'default'; + } + } + else { + $lang = 'default'; + } + } + if (empty($key)) { + return ''; + } + if (isset(self::$translations[$lang][$key])) { + // echo "
Case 1: $lang!
"; + return self::$translations[$lang][$key]; + } + if (isset(self::$translations['default'][$key])) { + // echo "
Case 2: $lang!
"; + return self::$translations['default'][$key]; + } + if (isset(self::$translations['en'][$key])) { + // echo "
Case 3: $lang!
"; + return self::$translations['en'][$key]; + } + // echo "
Case 4: $lang!". var_dump(self::$translations['en']) ."
"; + return "[[$key]]"; + } +} +/** + * get the language string $key + * return string + */ + +function L($key){ + global $language; + if (empty($key)) { + return ''; + } + if (isset($language[$key])) { + return $language[$key]; + } + return "[[$key]]"; +} + +/** + * get the language string $key in $lang + * or current default language if $lang + * is not given. + * return string + */ + +function tL($key, $lang = null) { + return FlySprayI18N::L($key, $lang); +} +/** + * html escaped variant of the previous + * return $string + */ +function eL($key){ + return htmlspecialchars(L($key), ENT_QUOTES, 'utf-8'); +} + +function load_translations(){ + global $proj, $language, $user, $fs; + # Load translations + # if no valid lang_code, return english + # valid == a-z and "_" case insensitive + + if (isset($user) && array_key_exists('lang_code', $user->infos)){ + $lang_code=$user->infos['lang_code']; + } + + # 20150211 add language preferences detection of visitors + # locale_accept_from_http() not available on every hosting, so we must parse it self. + # TODO ..and we can loop later through $langs until we find a matching translation file + if((!isset($lang_code) || $lang_code=='' || $lang_code=='browser') && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){ + foreach( explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']) as $lang) { + # taken from a php.net comment + $pattern = '/^(?P[a-zA-Z]{2,8})'. + '(?:-(?P[a-zA-Z]{2,8}))?(?:(?:;q=)'. + '(?P\d\.\d))?$/'; + $splits = array(); + if (preg_match($pattern, $lang, $splits)) { + $langs[]=$splits; + } + } + # TODO maybe sort $langs-array by quantifiers, but for most browsers it should be ok, because they sent it in right order. + if(isset($langs)){ + $lang_code=$langs[0]['primarytag']; + if(isset($langs[0]['subtag'])){ + $lang_code.='_'.$langs[0]['subtag']; # '_' for our language files, '-' in HTTP_ACCEPT_LANGUAGE + } + } + } + + if(!isset($lang_code) || $lang_code=='' || $lang_code=='project'){ + if($proj->prefs['lang_code']){ + $lang_code = $proj->prefs['lang_code']; + }else{ + $lang_code = 'en'; + } + } + + if (!preg_match('/^[a-z_]+$/iD', $lang_code)) { + $lang_code ='en'; + } + + $lang_code = strtolower($lang_code); + $translation = BASEDIR.'/lang/'.$lang_code.'.php'; + if ($lang_code != 'en' && is_readable($translation)) { + include_once($translation); + $language = is_array($translation) ? array_merge($language, $translation) : $language; + FlySprayI18N::init($lang_code, $language); + }elseif( 'en'!=substr($lang_code, 0, strpos($lang_code, '_')) && is_readable(BASEDIR.'/lang/'.(substr($lang_code, 0, strpos($lang_code, '_'))).'.php') ){ + # fallback 'de_AT' to 'de', but not for 'en_US' + $translation=BASEDIR.'/lang/'.(substr($lang_code, 0, strpos($lang_code, '_'))).'.php'; + include_once($translation); + $language = is_array($translation) ? array_merge($language, $translation) : $language; + } + + FlySprayI18N::setDefault($language); + // correctly translate title since language not set when initialising the project + if (isset($proj) && !$proj->id) { + $proj->prefs['project_title'] = L('allprojects'); + $proj->prefs['feed_description'] = L('feedforall'); + } + + for ($i = 6; $i >= 1; $i--) { + $fs->priorities[$i] = L('priority' . $i); + } + for ($i = 5; $i >= 1; $i--) { + $fs->severities[$i] = L('severity' . $i); + } +} -- cgit v1.2.3-70-g09d2