-
Notifications
You must be signed in to change notification settings - Fork 4
/
_intl.php
60 lines (52 loc) · 1.56 KB
/
_intl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
function getLanguageRequestParam()
{
if (array_key_exists("lang", $_REQUEST)) return "?lang=".$_REQUEST["lang"];
else return "";
}
function getPreferredLanguages()
{
if (array_key_exists("lang", $_REQUEST)) return array($_REQUEST["lang"]);
$httpAcceptLanguage = array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : "";
$languagesWithWeights = explode(',', $httpAcceptLanguage);
$result = array();
foreach ($languagesWithWeights as $languageWithWeight) {
$language = explode(';q=', $languageWithWeight);
// ;q=... is optional
$result[$language[0]] = isset($language[1]) ? floatval($language[1]) : 1;
}
arsort($result);
return array_keys($result);
}
function getSupportedLanguages()
{
return array_filter(scandir("res"), function($f) {
return is_dir("res/".$f) && !str_starts_with($f, ".");
});
}
function findPreferredSupportedLanguage()
{
$languages = getPreferredLanguages();
$supported = getSupportedLanguages();
foreach ($languages as $language) {
if (in_array($language, $supported)) {
return $language;
}
}
return "en";
}
function getStrings($language)
{
return json_decode(file_get_contents("res/".$language."/strings.json"), true);
}
$language = findPreferredSupportedLanguage();
$supportedLanguages = getSupportedLanguages();
$strings = getStrings($language);
// fill missing strings in chosen language with default string from "en"
if ($language != "en") {
$stringsEn = getStrings("en");
foreach($stringsEn as $key => $string) {
if (!array_key_exists($key, $strings)) $strings[$key] = $string;
}
}
?>