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
|
<?php
class Dictionary{
private static $messageArray;
//loads the messages and store them in the class
function build(){
self::$messageArray = json_decode(file_get_contents("lang/" . LANG . "/messages.json"),true);
}
//return an array with the JSON tags from the user's language or a specified language
public static function getArrayTemplate($template,$lang = false){
$language = array('lang'=>LANG);
if(!$lang) $lang = LANG;
return array_merge($language,json_decode(file_get_contents("lang/" . $lang . "/" . $template . ".json"),true));
}
//translate a hardcoded message
public static function translate($string){
$hardcoded = json_decode(file_get_contents("lang/" . LANG . "/messages-hardcoded.json"),true);
return $hardcoded[$string];
}
//returns an array with the JSON message tags
public static function getMessages(){
return self::$messageArray;
}
}
//Array containing the allowed languages for the website
$langArray = unserialize(SITE_LANGUAGES);
//Changes the language in case there is a request to
if(isset($_GET['lang']))
if(in_array($_GET['lang'],$langArray)){
setcookie('lang',$_GET['lang'],time()+60*60*24*30*12);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
//Default language
$language = 'en';
//Language from the browser
$langBrowser = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
//User language
if(isset($_COOKIE['lang']) && in_array($_COOKIE['lang'],$langArray)){
$language = $_COOKIE['lang'];
}else if(in_array($langBrowser,$langArray)){
$language = $langBrowser;
}
define('LANG', $language);
?>
|