-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguage.php
103 lines (82 loc) · 2.84 KB
/
language.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// Check browser language and redirect
$supportedLanguages = [
"en" => ["English", "en_US"]
];
$URI = explode("/", value(Main::class, URI));
if (array_key_exists($URI[1], $supportedLanguages)) {
$lang = $URI[1];
} else {
$lang = preferred_language($supportedLanguages, $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$lang = isset($lang) ? $lang : 'en';
}
Main::set(LANGUAGE, $supportedLanguages[$lang][1]);
if (0 === strcmp(value(Main::class, URI), "/")) {
$target = "/$lang/";
if (isset($_SERVER["QUERY_STRING"])) {
$target .= "?".$_SERVER["QUERY_STRING"];
}
relocate($target, false, true);
exit;
} else {
if (!array_key_exists($URI[1], $supportedLanguages)) {
if ((stripos($URI[1], "=") === false) && (0 !== strcmp($URI[1], "sitemap.xml"))) {
if (2 === strlen($URI[1])) {
# replace unknown language
array_splice($URI, 1, 1, $lang);
} else {
# prepend default language
array_splice($URI, 1, 0, $lang);
}
$target = implode("/", $URI);
if (isset($_SERVER["QUERY_STRING"])) {
$target .= "?".$_SERVER["QUERY_STRING"];
}
relocate($target, false, true);
exit;
}
}
}
// Check choosen language and choose correct config files
switch ($lang) {
default:
include 'language/en/videos.php';
include 'language/en/theme.php';
include 'language/en/calendar.php';
break;
}
// preferred_language source: https://gist.github.com/Xeoncross/dc2ebf017676ae946082
function preferred_language($available_languages, $http_accept_language) {
$result = null;
$available_languages = array_keys($available_languages);
$available_languages = array_flip($available_languages);
$langs = [];
if (false !== preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($http_accept_language), $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
list($a, $b) = explode('-', $match[1]) + array('', '');
$value = isset($match[2]) ? (float) $match[2] : 1.0;
if(isset($available_languages[$match[1]])) {
$langs[$match[1]] = $value;
continue;
}
if(isset($available_languages[$a])) {
$langs[$a] = $value - 0.1;
}
}
if(0 < count($langs)) {
arsort($langs);
$result = key($langs); // We don't need the whole array of choices since we have a match
}
}
return $result;
}
function getLanguagePicker($languages) {
$currentURI = explode("/", value(Main::class, URI));
unset($currentURI[1]);
$currentURI = implode("/", $currentURI);
$html = "";
foreach ($languages as $short => $name) {
$html .= "<a href=\"/".html($short.$currentURI)."\">".html($name[0])."</a> · ";
}
return rtrim($html, " · ");
}