+ // Set the syslang as default fallback
+ $current_lang = $sysLang;
+ // start with quality zero (every guessed language is more acceptable ..)
+ $current_q = 0;
+
+ foreach ($acceptedLanguages as $acceptedLanguage) {
+ $res = preg_match(
+ '/^([a-z]{1,8}(?:-[a-z]{1,8})*)(?:;\s*q=(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?))?$/i',
+ $acceptedLanguage,
+ $matches
+ );
+
+ // Invalid language? -> skip
+ if (!$res) {
+ continue;
+ }
+
+ // split language codes based on it's "-"
+ $lang_code = explode('-', $matches[1]);
+
+ // determine the quality of the guess
+ if (isset($matches[2])) {
+ $lang_quality = (float)$matches[2];
+ } else {
+ // fallback so without a quality parameter, it's probably the best
+ $lang_quality = 1;
+ }
+
+ // loop through each part of the code-parts
+ while (count($lang_code)) {
+ // try to mix them so we can get double-code parts too
+ $match_lang = strtolower(join('-', $lang_code));
+ if (file_exists(__DIR__ . "/../../view/lang/$match_lang") &&
+ is_dir(__DIR__ . "/../../view/lang/$match_lang")) {
+ if ($lang_quality > $current_q) {
+ $current_lang = $match_lang;
+ $current_q = $lang_quality;
+ break;
+ }
+ }
+
+ // remove the most right code-part
+ array_pop($lang_code);
+ }
+ }
+
+ return $current_lang;