forked from nisargjhaveri/minimalMVC
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi18n.php
78 lines (60 loc) · 1.97 KB
/
i18n.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
<?php
class i18n {
function __construct() {
global $cfg;
if ($cfg['i18n']['gettext']) {
$domain = $cfg['i18n']['gettext']['domain'];
$directory = $cfg['i18n']['gettext']['directory'];
bindtextdomain($domain, $directory);
textdomain($domain);
load_helper('gettext');
}
$client_locale = $this->get_client_locale();
if ($client_locale) {
$this->set_locale($client_locale);
}
setcookie('locale_path_prefix', '', 0, '/');
}
private function _set_locale($locale) {
return
putenv("LANG=" . $locale)
&& (setlocale(LC_ALL, $locale) !== false);
}
function set_locale($locale_string) {
global $cfg;
$locales = $cfg['i18n']['locales'];
$languages = $cfg['i18n']['languages'];
$locale = false;
if (in_array($locale_string, $locales)) {
$locale = $locale_string;
} elseif (isset($languages[$locale_string])) {
$locale = $languages[$locale_string];
}
if ($locale === false) {
return false;
}
return $this->_set_locale($locale);
}
function get_locale_from_path($path) {
global $_locale_base_url;
$pathInfo = empty($path)? array() : explode('/', $path);
if (!empty($pathInfo[1])) {
if ($this->set_locale($pathInfo[1])) {
$_locale_base_url .= $pathInfo[1] . '/';
setcookie('locale_path_prefix', $pathInfo[1], 0, '/');
array_splice($pathInfo, 1, 1);
} else {
header('Vary: Accept-Language');
}
}
return implode('/', $pathInfo);
}
function get_client_locale() {
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
return locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
else {
return false;
}
}
}