-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtranslate.php
71 lines (57 loc) · 1.86 KB
/
translate.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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$translations = array(
"en" => "./langs/en/static.json",
"fr" => "./langs/fr/static.json",
"it" => "./langs/it/static.json",
"es" => "./langs/es/static.json",
"zh" => "./langs/zh/static.json",
"ru" => "./langs/ru/static.json"
);
/* Get the language from the browser HTTP headers, in the 639-1 format */
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
class i18n {
private $_data;
public $_page_type;
public $_lang;
public function loadLanguage($array) {
foreach($array as $code => $language_file) {
$this->_data[$code] = json_decode(file_get_contents($language_file), true);
}
}
public function setLang($language_code) {
if(array_key_exists($language_code, $this->_data)) {
$this->_lang = $language_code;
} else {
$this->_lang = "en";
}
}
public function get($string) {
if(substr($string, 0, 6) === "[html]") {
$string = substr($string, 6);
return str_replace("{{SITENAME}}", $this->_data[$this->_lang]["sitename"], $this->_data[$this->_lang][$string]);
} else {
return htmlspecialchars(str_replace("{{SITENAME}}", $this->_data[$this->_lang]["sitename"], $this->_data[$this->_lang][$string]));
}
}
public function getMeta($string) {
return $this->get($this->_page_type."_".$string);
}
public function getLangs() {
return array_keys($this->_data);
}
}
$i18n = new i18n;
$i18n->loadLanguage($translations);
if (strpos($_SERVER['HTTP_HOST'], "politiscales.fr") !== false) {
$i18n->setLang("fr");
} elseif(isset($_GET["lang"]) && in_array($_GET["lang"], $i18n->getLangs())) {
$i18n->setLang($_GET["lang"]);
} elseif(in_array($browser_lang, $i18n->getLangs())) {
$i18n->setLang($browser_lang);
} else {
$i18n->setLang("en");
}
$i18n->_page_type = $page_type;