+ $lang = Strings::sanitizeFilePathItem($lang);
+
+ // Don't override the language setting with empty languages
+ if (empty($lang)) {
+ return;
+ }
+
+ $a = new \stdClass();
+ $a->strings = [];
+
+ // load enabled addons strings
+ $addons = $this->dba->select('addon', ['name'], ['installed' => true]);
+ while ($p = $this->dba->fetch($addons)) {
+ $name = Strings::sanitizeFilePathItem($p['name']);
+ if (file_exists(__DIR__ . "/../../addon/$name/lang/$lang/strings.php")) {
+ include __DIR__ . "/../../addon/$name/lang/$lang/strings.php";
+ }
+ }
+
+ if (file_exists(__DIR__ . "/../../view/lang/$lang/strings.php")) {
+ include __DIR__ . "/../../view/lang/$lang/strings.php";
+ }
+
+ $this->lang = $lang;
+ $this->strings = $a->strings;
+
+ unset($a);
+ }
+
+ /**
+ * Returns the preferred language from the HTTP_ACCEPT_LANGUAGE header
+ *
+ * @param string $sysLang The default fallback language
+ * @param array $server The $_SERVER array
+ * @param array $get The $_GET array
+ *
+ * @return string The two-letter language code
+ */
+ public static function detectLanguage(array $server, array $get, string $sysLang = 'en')
+ {
+ $lang_variable = $server['HTTP_ACCEPT_LANGUAGE'] ?? null;
+
+ $acceptedLanguages = preg_split('/,\s*/', $lang_variable);
+
+ if (empty($acceptedLanguages)) {
+ $acceptedLanguages = [];
+ }
+
+ // Add get as absolute quality accepted language (except this language isn't valid)
+ if (!empty($get['lang'])) {
+ $acceptedLanguages[] = $get['lang'];
+ }
+
+ // return the sys language in case there's nothing to do
+ if (empty($acceptedLanguages)) {
+ return $sysLang;
+ }
+
+ // 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;