preg_match('/.*\.ini/', $x) === 1); // And we remove the ".ini" part $langs = array_map(fn ($x) => substr($x, 0, 2), $langs); //////////////////////////////////// /// Get the optimal language to use //////////////////////////////////// /* Try to get the language from browser-sent headers. The * format for the header is like this: "en_US,es;q=0.5,ar;q=0.1". * q=N represents the weight of preference, no q means q=1.0. */ if ($_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? false) { // Divide language string per language $http_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); // Sanitize lang string and cast weight $http_langs = array_map(function ($x) { $x = explode(';', $x); $x[0] = strtolower(substr($x[0], 0, 2)); $x[1] = (float)(explode('=', $x[1] ?? 'q=1.0')[1] ?? 1.0); return $x; }, $http_langs); // Sort by weight usort($http_langs, function ($a, $b) { if ($a[1] === $b[1]) return 0; return ($a[1] > $b[1]) ? -1 : 1; }); // Create new neat array like this: ['en', 'es', 'ar'] $user_langs = []; array_walk($http_langs, function ($arr) use (&$user_langs) { if (!in_array($arr[0], $user_langs)) $user_langs[] = $arr[0]; }); // Assign the first match foreach ($user_langs as $ilang) { if (in_array($ilang, $langs)) { $lang = $ilang; break; } } } // Manully selected lang > HTTP lang > fallback lang $lang = ($_GET['lang'] ?? null) ?: $lang ?: 'en'; /////////////////////////////////////////////////// /// Get file and build function to get the strings /////////////////////////////////////////////////// $text = parse_ini_file("$basedir/$lang.ini", true); $trans = function (string $index) use ($text): string { // return htmlspecialchars($text[$index] ?? 'N/A', ENT_QUOTES); return $text[$index] ?? 'N/A'; }; }; // Burn after reading $__langfunc(); unset($__langfunc);