1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Copyright (c) 2005 Steven Armstrong <sa at c-area dot ch> |
4
|
|
|
Copyright (c) 2009 Danilo Segan <[email protected]> |
5
|
|
|
Copyright (c) 2016 Michal Čihař <[email protected]> |
6
|
|
|
|
7
|
|
|
This file is part of MoTranslator. |
8
|
|
|
|
9
|
|
|
This program is free software; you can redistribute it and/or modify |
10
|
|
|
it under the terms of the GNU General Public License as published by |
11
|
|
|
the Free Software Foundation; either version 2 of the License, or |
12
|
|
|
(at your option) any later version. |
13
|
|
|
|
14
|
|
|
This program is distributed in the hope that it will be useful, |
15
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
GNU General Public License for more details. |
18
|
|
|
|
19
|
|
|
You should have received a copy of the GNU General Public License along |
20
|
|
|
with this program; if not, write to the Free Software Foundation, Inc., |
21
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace MoTranslator; |
25
|
|
|
|
26
|
|
|
class MoLoader { |
27
|
|
|
/** |
28
|
|
|
* Loader instance |
29
|
|
|
* |
30
|
|
|
* @access private |
31
|
|
|
* @static |
32
|
|
|
* @var MoLoader |
33
|
|
|
*/ |
34
|
|
|
private static $_instance; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string Default gettext domain to use. |
38
|
|
|
*/ |
39
|
|
|
private $default_domain = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string Configured locale. |
43
|
|
|
*/ |
44
|
|
|
private $locale = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array Loaded domains |
48
|
|
|
*/ |
49
|
|
|
private $domains = array(); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array Bound paths for domains |
53
|
|
|
*/ |
54
|
|
|
private $paths = array('' => './'); |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the singleton MoLoader object |
58
|
|
|
* |
59
|
|
|
* @return MoLoader object |
60
|
|
|
*/ |
61
|
3 |
|
public static function getInstance() |
62
|
|
|
{ |
63
|
3 |
|
if (empty(self::$_instance)) { |
64
|
1 |
|
self::$_instance = new MoLoader(); |
65
|
1 |
|
} |
66
|
3 |
|
return self::$_instance; |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Loads global localizaton functions. |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
2 |
|
public static function load_functions() |
75
|
|
|
{ |
76
|
2 |
|
require_once __DIR__ . '/functions.php'; |
77
|
2 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Figure out all possible locale names and start with the most |
81
|
|
|
* specific ones. I.e. for sr_CS.UTF-8@latin, look through all of |
82
|
|
|
* sr_CS.UTF-8@latin, sr_CS@latin, sr@latin, sr_CS.UTF-8, sr_CS, sr. |
83
|
|
|
* |
84
|
|
|
* @param string $locale Locale code |
85
|
|
|
* |
86
|
|
|
* @return array list of locales to try for any POSIX-style locale specification. |
87
|
|
|
*/ |
88
|
18 |
|
public static function list_locales($locale) { |
89
|
18 |
|
$locale_names = array(); |
90
|
|
|
|
91
|
18 |
|
$lang = NULL; |
92
|
18 |
|
$country = NULL; |
93
|
18 |
|
$charset = NULL; |
94
|
18 |
|
$modifier = NULL; |
95
|
|
|
|
96
|
18 |
|
if ($locale) { |
97
|
17 |
|
if (preg_match("/^(?P<lang>[a-z]{2,3})" // language code |
98
|
|
|
."(?:_(?P<country>[A-Z]{2}))?" // country code |
99
|
17 |
|
."(?:\.(?P<charset>[-A-Za-z0-9_]+))?" // charset |
100
|
17 |
|
."(?:@(?P<modifier>[-A-Za-z0-9_]+))?$/", // @ modifier |
101
|
17 |
|
$locale, $matches)) { |
102
|
|
|
|
103
|
16 |
|
extract($matches); |
104
|
|
|
|
105
|
16 |
|
if ($modifier) { |
106
|
5 |
|
if ($country) { |
107
|
2 |
|
if ($charset) { |
108
|
2 |
|
array_push($locale_names, "${lang}_$country.$charset@$modifier"); |
109
|
2 |
|
} |
110
|
2 |
|
array_push($locale_names, "${lang}_$country@$modifier"); |
111
|
5 |
|
} elseif ($charset) { |
112
|
1 |
|
array_push($locale_names, "${lang}.$charset@$modifier"); |
113
|
1 |
|
} |
114
|
5 |
|
array_push($locale_names, "$lang@$modifier"); |
115
|
5 |
|
} |
116
|
|
|
if ($country) { |
117
|
|
|
if ($charset) { |
118
|
|
|
array_push($locale_names, "${lang}_$country.$charset"); |
119
|
|
|
} |
120
|
|
|
array_push($locale_names, "${lang}_$country"); |
121
|
16 |
|
} elseif ($charset) { |
122
|
2 |
|
array_push($locale_names, "${lang}.$charset"); |
123
|
2 |
|
} |
124
|
16 |
|
array_push($locale_names, $lang); |
125
|
16 |
|
} |
126
|
|
|
|
127
|
|
|
// If the locale name doesn't match POSIX style, just include it as-is. |
128
|
17 |
|
if (!in_array($locale, $locale_names)) { |
129
|
1 |
|
array_push($locale_names, $locale); |
130
|
1 |
|
} |
131
|
17 |
|
} |
132
|
18 |
|
return $locale_names; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function get_translator($domain = '') |
136
|
|
|
{ |
137
|
9 |
|
if (empty($domain)) { |
138
|
6 |
|
$domain = $this->default_domain; |
139
|
6 |
|
} |
140
|
|
|
|
141
|
9 |
|
if (!isset($this->domains[$domain])) { |
142
|
|
|
|
143
|
7 |
|
if (isset($this->paths[$domain])) { |
144
|
5 |
|
$base = $this->paths[$domain]; |
145
|
5 |
|
} else { |
146
|
2 |
|
$base = './'; |
147
|
|
|
} |
148
|
|
|
|
149
|
7 |
|
$locale_names = $this->list_locales($this->locale); |
150
|
|
|
|
151
|
7 |
|
$filename = ''; |
152
|
7 |
|
foreach ($locale_names as $locale) { |
153
|
7 |
|
$filename = "$base/$locale/LC_MESSAGES/$domain.mo"; |
154
|
7 |
|
if (file_exists($filename)) { |
155
|
5 |
|
break; |
156
|
|
|
} |
157
|
7 |
|
} |
158
|
|
|
|
159
|
|
|
// We don't care about invalid path, we will get fallback |
160
|
|
|
// translator here |
161
|
7 |
|
$this->domains[$domain] = new MoTranslator($filename); |
162
|
7 |
|
} |
163
|
|
|
|
164
|
9 |
|
return $this->domains[$domain]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function bindtextdomain($domain, $path) |
168
|
|
|
{ |
169
|
9 |
|
$this->paths[$domain] = $path; |
170
|
9 |
|
} |
171
|
|
|
|
172
|
|
|
public function textdomain($domain) |
173
|
|
|
{ |
174
|
9 |
|
$this->default_domain = $domain; |
175
|
9 |
|
} |
176
|
|
|
|
177
|
|
|
public function setlocale($locale) |
178
|
|
|
{ |
179
|
9 |
|
if (!empty($locale)) { |
180
|
9 |
|
$this->locale = $locale; |
181
|
|
|
// Set system locales as well |
182
|
9 |
|
if (function_exists('setlocale')) { |
183
|
9 |
|
setlocale(LC_MESSAGES, $locale); |
184
|
9 |
|
} |
185
|
9 |
|
} |
186
|
9 |
|
return $this->locale; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|