forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.php
109 lines (89 loc) · 2.93 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
<?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) {
if (isset($dir) && is_readable($dir)) {
$dlist = Array();
$dir = realpath($dir);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $entry => $object){
$entry = str_replace($dir, '', $entry);
if (basename(dirname($entry))=='locale' && basename($entry)!='.' && basename($entry)!='..') $dlist[] = basename($entry);
}
return array_unique($dlist);
}
}
function get_available_languages()
{
return directoryLocaleScan(dirname(__FILE__));
}
function lang_http_accept()
{
$langs = array();
foreach (explode(',', server('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)) {
$a = $splits["primarytag"];
if (isset($splits["subtag"]) && $splits["subtag"]<> "") $a = $a."_".$splits["subtag"];
$langs[]=$a;
} else {
// No match
}
}
return $langs;
}
function set_lang($language)
{
// set the first browser selected language
// TODO: iterate to find a suitable available language
// Chrome returns different HTTP_ACCEPT_LANGUAGE code than firefox!!!
// Firefox Chrome
// -------------------
// en_EN en
// es_ES es
// ... so translation system does not work in Chrome!!!
// lets try to fix quickly
if (isset($language[0]))
{
$lang=$language[0];
switch($lang) {
case 'cy': $lang='cy_GB'; break;
case 'da': $lang='da_DK'; break;
case 'en': $lang='en_EN'; break;
case 'es': $lang='es_ES'; break;
case 'fr': $lang='fr_FR'; break;
case 'it': $lang='it_IT'; break;
case 'nl': $lang='nl_NL'; break;
//case 'nl': $lang='nl_BE'; break; //to do??
default : $lang='en_EN';
}
set_lang_by_user($lang);
}
}
function set_lang_by_user($lang)
{
putenv("LC_ALL=$lang".'.UTF8');
setlocale(LC_ALL,$lang.'.UTF8');
}
function set_emoncms_lang($lang)
{
// If no language defined use the language browser
if ($lang == '')
set_lang(lang_http_accept());
else
set_lang_by_user($lang);
}