- * @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
- */
- public static function getBrowserLanguage()
- {
- $lang_list = [];
-
- if (x($_SERVER, 'HTTP_ACCEPT_LANGUAGE')) {
- // break up string into pieces (languages and q factors)
- preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
-
- if (count($lang_parse[1])) {
- // go through the list of prefered languages and add a generic language
- // for sub-linguas (e.g. de-ch will add de) if not already in array
- for ($i = 0; $i < count($lang_parse[1]); $i++) {
- $lang_list[] = strtolower($lang_parse[1][$i]);
- if (strlen($lang_parse[1][$i])>3) {
- $dashpos = strpos($lang_parse[1][$i], '-');
- if (!in_array(substr($lang_parse[1][$i], 0, $dashpos), $lang_list)) {
- $lang_list[] = strtolower(substr($lang_parse[1][$i], 0, $dashpos));
- }
- }
- }
- }
- }
-
- // check if we have translations for the preferred languages and pick the 1st that has
- foreach ($lang_list as $lang) {
- if ($lang === 'en' || (file_exists("view/lang/$lang") && is_dir("view/lang/$lang"))) {
- $preferred = $lang;
- break;
- }
- }
- if (isset($preferred)) {
- return $preferred;
- }
-
- // in case none matches, get the system wide configured language, or fall back to English
- return Config::get('system', 'language', 'en');
- }
-
- /**
- * @param string $language language
- */
- public static function pushLang($language)
- {
- global $lang, $a;
-
- $a->langsave = $lang;
-
- if ($language === $lang) {
- return;
- }
-
- if (isset($a->strings) && count($a->strings)) {
- $a->stringsave = $a->strings;
- }
- $a->strings = [];
- self::loadTranslationTable($language);
- $lang = $language;
- }
-
- /**
- * Pop language off the top of the stack