forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.php
164 lines (136 loc) · 4.51 KB
/
locale.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
// Return all locale directory from all modules.
// If one module has a language it will be detected
function directoryLocaleScan($dir)
{
$directoryList = array();
if (isset($dir) && is_readable($dir)) {
$dir = realpath($dir);
$directoryList = glob($dir."/Modules/*/locale/*", GLOB_ONLYDIR);
$directoryList = array_merge($directoryList, glob($dir."/Theme/*/locale/*", GLOB_ONLYDIR));
$directoryList = array_map(
function ($item) {
return basename($item);
},
$directoryList
);
}
return array_unique($directoryList);
}
function get_available_languages()
{
return directoryLocaleScan(dirname(__FILE__));
}
function get_available_languages_with_names()
{
$available_languages = get_available_languages();
static $language_names = null;
if ($language_names === null) {
$json_data = file_get_contents(__DIR__.'/Lib/language_country.json');
$language_names = json_decode($json_data, true);
}
$available_languages_with_names = array();
foreach ($available_languages as $code){
$available_languages_with_names[$code] = $language_names[$code];
}
return $available_languages_with_names;
}
function languagecode_to_name($langs) {
static $lang_names = null;
if ($lang_names === null) {
$json_data = file_get_contents(__DIR__.'/Lib/language_country.json');
$lang_names = json_decode($json_data, true);
}
foreach ($langs as $key=>$val){
$lang[$key]=$lang_names[$val];
}
asort($lang);
return $lang;
}
function lang_http_accept()
{
$langs = array();
if (!$http_accept_language = server('HTTP_ACCEPT_LANGUAGE')) {
return $langs;
}
foreach (explode(',',$http_accept_language) as $lang) {
$pattern = '/^(?P<primarytag>[a-zA-Z]{2,8})'.
'(?:-(?P<subtag>[a-zA-Z]{2,8}))?(?:(?:;q=)'.
'(?P<quantifier>\d\.\d))?$/';
$splits = array();
if (preg_match($pattern, $lang, $splits)) {
$langs[] = !empty($splits['subtag']) ? $splits["primarytag"] . "_" . $splits['subtag'] : $splits["primarytag"];
}
}
return $langs;
}
/***
* take the values from the given list and save it as the user's language
* only takes supported language values.
* @param array $language - array returned by lang_http_accept() - without the validating values
*/
function set_lang($language)
{
global $settings;
// DEFAULT - from settings.php (if not in file use 'en_GB')
$fallback_language = $settings['interface']['default_language'];
$supported_languages = array(
'cy' => 'cy_GB',
'da' => 'da_DK',
'es' => 'es_ES',
'fr' => 'fr_FR',
'it' => 'it_IT',
'nl' => 'nl_NL',
'en' => 'en_GB'
);
/**
* ORDER OF PREFERENCE WITH LANGUAGE SELECTION
* -------------------------------------------
* 1. non logged in users use the browser's language
* 2. logged in users use their saved language preference
* 3. logged in users without language saved uses `$default_language` from settings.php
* 4. else fallback is set to 'en_GB'
*/
$lang = $fallback_language; // if not found use fallback
// loop through all given $language values
// if given language is a key or value in the above list use it
foreach ($language as $lang_code) {
$lang_code = htmlspecialchars($lang_code);
if (isset($supported_languages[$lang_code])) { // key check
$lang = $supported_languages[$lang_code];
break;
} elseif (in_array($lang_code, $supported_languages)) { // value check
$lang = $lang_code;
break;
}
}
set_lang_by_user($lang);
}
function set_lang_by_user($lang)
{
$locale = $lang.'.UTF8';
define(LC_MESSAGES, $locale);
putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
}
function set_emoncms_lang($lang)
{
// If no language defined use the browser language
if ($lang == '') {
$browser_languages = lang_http_accept();
set_lang($browser_languages);
} else {
set_lang_by_user($lang);
}
global $session;
}