| 12345 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Internationalization
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @package Cake.I18n
|
|
|
15 |
* @since CakePHP(tm) v 1.2.0.4116
|
|
|
16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
17 |
*/
|
|
|
18 |
|
|
|
19 |
App::uses('CakePlugin', 'Core');
|
|
|
20 |
App::uses('L10n', 'I18n');
|
|
|
21 |
App::uses('Multibyte', 'I18n');
|
|
|
22 |
App::uses('CakeSession', 'Model/Datasource');
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* I18n handles translation of Text and time format strings.
|
|
|
26 |
*
|
|
|
27 |
* @package Cake.I18n
|
|
|
28 |
*/
|
|
|
29 |
class I18n {
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Instance of the L10n class for localization
|
|
|
33 |
*
|
|
|
34 |
* @var L10n
|
|
|
35 |
*/
|
|
|
36 |
public $l10n = null;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Default domain of translation
|
|
|
40 |
*
|
|
|
41 |
* @var string
|
|
|
42 |
*/
|
|
|
43 |
public static $defaultDomain = 'default';
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Current domain of translation
|
|
|
47 |
*
|
|
|
48 |
* @var string
|
|
|
49 |
*/
|
|
|
50 |
public $domain = null;
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Current category of translation
|
|
|
54 |
*
|
|
|
55 |
* @var string
|
|
|
56 |
*/
|
|
|
57 |
public $category = 'LC_MESSAGES';
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Current language used for translations
|
|
|
61 |
*
|
|
|
62 |
* @var string
|
|
|
63 |
*/
|
|
|
64 |
protected $_lang = null;
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Translation strings for a specific domain read from the .mo or .po files
|
|
|
68 |
*
|
|
|
69 |
* @var array
|
|
|
70 |
*/
|
|
|
71 |
protected $_domains = array();
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Set to true when I18N::_bindTextDomain() is called for the first time.
|
|
|
75 |
* If a translation file is found it is set to false again
|
|
|
76 |
*
|
|
|
77 |
* @var bool
|
|
|
78 |
*/
|
|
|
79 |
protected $_noLocale = false;
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Translation categories
|
|
|
83 |
*
|
|
|
84 |
* @var array
|
|
|
85 |
*/
|
|
|
86 |
protected $_categories = array(
|
|
|
87 |
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
|
|
|
88 |
);
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Constants for the translation categories.
|
|
|
92 |
*
|
|
|
93 |
* The constants may be used in translation fetching
|
|
|
94 |
* instead of hardcoded integers.
|
|
|
95 |
* Example:
|
|
|
96 |
* {{{
|
|
|
97 |
* I18n::translate('CakePHP is awesome.', null, null, I18n::LC_MESSAGES)
|
|
|
98 |
* }}}
|
|
|
99 |
*
|
|
|
100 |
* To keep the code more readable, I18n constants are preferred over
|
|
|
101 |
* hardcoded integers.
|
|
|
102 |
*/
|
|
|
103 |
/**
|
|
|
104 |
* Constant for LC_ALL.
|
|
|
105 |
*
|
|
|
106 |
* @var int
|
|
|
107 |
*/
|
|
|
108 |
const LC_ALL = 0;
|
|
|
109 |
|
|
|
110 |
/**
|
|
|
111 |
* Constant for LC_COLLATE.
|
|
|
112 |
*
|
|
|
113 |
* @var int
|
|
|
114 |
*/
|
|
|
115 |
const LC_COLLATE = 1;
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Constant for LC_CTYPE.
|
|
|
119 |
*
|
|
|
120 |
* @var int
|
|
|
121 |
*/
|
|
|
122 |
const LC_CTYPE = 2;
|
|
|
123 |
|
|
|
124 |
/**
|
|
|
125 |
* Constant for LC_MONETARY.
|
|
|
126 |
*
|
|
|
127 |
* @var int
|
|
|
128 |
*/
|
|
|
129 |
const LC_MONETARY = 3;
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* Constant for LC_NUMERIC.
|
|
|
133 |
*
|
|
|
134 |
* @var int
|
|
|
135 |
*/
|
|
|
136 |
const LC_NUMERIC = 4;
|
|
|
137 |
|
|
|
138 |
/**
|
|
|
139 |
* Constant for LC_TIME.
|
|
|
140 |
*
|
|
|
141 |
* @var int
|
|
|
142 |
*/
|
|
|
143 |
const LC_TIME = 5;
|
|
|
144 |
|
|
|
145 |
/**
|
|
|
146 |
* Constant for LC_MESSAGES.
|
|
|
147 |
*
|
|
|
148 |
* @var int
|
|
|
149 |
*/
|
|
|
150 |
const LC_MESSAGES = 6;
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Escape string
|
|
|
154 |
*
|
|
|
155 |
* @var string
|
|
|
156 |
*/
|
|
|
157 |
protected $_escape = null;
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Constructor, use I18n::getInstance() to get the i18n translation object.
|
|
|
161 |
*/
|
|
|
162 |
public function __construct() {
|
|
|
163 |
$this->l10n = new L10n();
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
/**
|
|
|
167 |
* Return a static instance of the I18n class
|
|
|
168 |
*
|
|
|
169 |
* @return I18n
|
|
|
170 |
*/
|
|
|
171 |
public static function getInstance() {
|
|
|
172 |
static $instance = array();
|
|
|
173 |
if (!$instance) {
|
|
|
174 |
$instance[0] = new I18n();
|
|
|
175 |
}
|
|
|
176 |
return $instance[0];
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Used by the translation functions in basics.php
|
|
|
181 |
* Returns a translated string based on current language and translation files stored in locale folder
|
|
|
182 |
*
|
|
|
183 |
* @param string $singular String to translate
|
|
|
184 |
* @param string $plural Plural string (if any)
|
|
|
185 |
* @param string $domain Domain The domain of the translation. Domains are often used by plugin translations.
|
|
|
186 |
* If null, the default domain will be used.
|
|
|
187 |
* @param string $category Category The integer value of the category to use.
|
|
|
188 |
* @param int $count Count Count is used with $plural to choose the correct plural form.
|
|
|
189 |
* @param string $language Language to translate string to.
|
|
|
190 |
* If null it checks for language in session followed by Config.language configuration variable.
|
|
|
191 |
* @return string translated string.
|
|
|
192 |
* @throws CakeException When '' is provided as a domain.
|
|
|
193 |
*/
|
|
|
194 |
public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES, $count = null, $language = null) {
|
|
|
195 |
$_this = I18n::getInstance();
|
|
|
196 |
|
|
|
197 |
if (strpos($singular, "\r\n") !== false) {
|
|
|
198 |
$singular = str_replace("\r\n", "\n", $singular);
|
|
|
199 |
}
|
|
|
200 |
if ($plural !== null && strpos($plural, "\r\n") !== false) {
|
|
|
201 |
$plural = str_replace("\r\n", "\n", $plural);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
if (is_numeric($category)) {
|
|
|
205 |
$_this->category = $_this->_categories[$category];
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
if (empty($language)) {
|
|
|
209 |
if (CakeSession::started()) {
|
|
|
210 |
$language = CakeSession::read('Config.language');
|
|
|
211 |
}
|
|
|
212 |
if (empty($language)) {
|
|
|
213 |
$language = Configure::read('Config.language');
|
|
|
214 |
}
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
if (($_this->_lang && $_this->_lang !== $language) || !$_this->_lang) {
|
|
|
218 |
$lang = $_this->l10n->get($language);
|
|
|
219 |
$_this->_lang = $lang;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
if ($domain === null) {
|
|
|
223 |
$domain = self::$defaultDomain;
|
|
|
224 |
}
|
|
|
225 |
if ($domain === '') {
|
|
|
226 |
throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
$_this->domain = $domain . '_' . $_this->l10n->lang;
|
|
|
230 |
|
|
|
231 |
if (!isset($_this->_domains[$domain][$_this->_lang])) {
|
|
|
232 |
$_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
|
|
|
236 |
$_this->_bindTextDomain($domain);
|
|
|
237 |
Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
if ($_this->category === 'LC_TIME') {
|
|
|
241 |
return $_this->_translateTime($singular, $domain);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
if (!isset($count)) {
|
|
|
245 |
$plurals = 0;
|
|
|
246 |
} elseif (!empty($_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"]) && $_this->_noLocale === false) {
|
|
|
247 |
$header = $_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"];
|
|
|
248 |
$plurals = $_this->_pluralGuess($header, $count);
|
|
|
249 |
} else {
|
|
|
250 |
if ($count != 1) {
|
|
|
251 |
$plurals = 1;
|
|
|
252 |
} else {
|
|
|
253 |
$plurals = 0;
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
if (!empty($_this->_domains[$domain][$_this->_lang][$_this->category][$singular])) {
|
|
|
258 |
if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular]) || ($plurals) && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural])) {
|
|
|
259 |
if (is_array($trans)) {
|
|
|
260 |
if (isset($trans[$plurals])) {
|
|
|
261 |
$trans = $trans[$plurals];
|
|
|
262 |
} else {
|
|
|
263 |
trigger_error(
|
|
|
264 |
__d('cake_dev',
|
|
|
265 |
'Missing plural form translation for "%s" in "%s" domain, "%s" locale. ' .
|
|
|
266 |
' Check your po file for correct plurals and valid Plural-Forms header.',
|
|
|
267 |
$singular,
|
|
|
268 |
$domain,
|
|
|
269 |
$_this->_lang
|
|
|
270 |
),
|
|
|
271 |
E_USER_WARNING
|
|
|
272 |
);
|
|
|
273 |
$trans = $trans[0];
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
if (strlen($trans)) {
|
|
|
277 |
return $trans;
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
if (!empty($plurals)) {
|
|
|
283 |
return $plural;
|
|
|
284 |
}
|
|
|
285 |
return $singular;
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Clears the domains internal data array. Useful for testing i18n.
|
|
|
290 |
*
|
|
|
291 |
* @return void
|
|
|
292 |
*/
|
|
|
293 |
public static function clear() {
|
|
|
294 |
$self = I18n::getInstance();
|
|
|
295 |
$self->_domains = array();
|
|
|
296 |
}
|
|
|
297 |
|
|
|
298 |
/**
|
|
|
299 |
* Get the loaded domains cache.
|
|
|
300 |
*
|
|
|
301 |
* @return array
|
|
|
302 |
*/
|
|
|
303 |
public static function domains() {
|
|
|
304 |
$self = I18n::getInstance();
|
|
|
305 |
return $self->_domains;
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
/**
|
|
|
309 |
* Attempts to find the plural form of a string.
|
|
|
310 |
*
|
|
|
311 |
* @param string $header Type
|
|
|
312 |
* @param int $n Number
|
|
|
313 |
* @return int plural match
|
|
|
314 |
*/
|
|
|
315 |
protected function _pluralGuess($header, $n) {
|
|
|
316 |
if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
|
|
|
317 |
return 0;
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
if ($header === "nplurals=2;plural=n!=1;") {
|
|
|
321 |
return $n != 1 ? 1 : 0;
|
|
|
322 |
} elseif ($header === "nplurals=2;plural=n>1;") {
|
|
|
323 |
return $n > 1 ? 1 : 0;
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
if (strpos($header, "plurals=3")) {
|
|
|
327 |
if (strpos($header, "100!=11")) {
|
|
|
328 |
if (strpos($header, "10<=4")) {
|
|
|
329 |
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
|
|
330 |
} elseif (strpos($header, "100<10")) {
|
|
|
331 |
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
|
|
332 |
}
|
|
|
333 |
return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
|
|
|
334 |
} elseif (strpos($header, "n==2")) {
|
|
|
335 |
return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
|
|
|
336 |
} elseif (strpos($header, "n==0")) {
|
|
|
337 |
return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
|
|
|
338 |
} elseif (strpos($header, "n>=2")) {
|
|
|
339 |
return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
|
|
|
340 |
} elseif (strpos($header, "10>=2")) {
|
|
|
341 |
return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
|
|
|
342 |
}
|
|
|
343 |
return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
|
|
|
344 |
} elseif (strpos($header, "plurals=4")) {
|
|
|
345 |
if (strpos($header, "100==2")) {
|
|
|
346 |
return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
|
|
|
347 |
} elseif (strpos($header, "n>=3")) {
|
|
|
348 |
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
|
|
|
349 |
} elseif (strpos($header, "100>=1")) {
|
|
|
350 |
return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
|
|
|
351 |
}
|
|
|
352 |
} elseif (strpos($header, "plurals=5")) {
|
|
|
353 |
return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
|
|
|
354 |
}
|
|
|
355 |
}
|
|
|
356 |
|
|
|
357 |
/**
|
|
|
358 |
* Binds the given domain to a file in the specified directory.
|
|
|
359 |
*
|
|
|
360 |
* @param string $domain Domain to bind
|
|
|
361 |
* @return string Domain binded
|
|
|
362 |
*/
|
|
|
363 |
protected function _bindTextDomain($domain) {
|
|
|
364 |
$this->_noLocale = true;
|
|
|
365 |
$core = true;
|
|
|
366 |
$merge = array();
|
|
|
367 |
$searchPaths = App::path('locales');
|
|
|
368 |
$plugins = CakePlugin::loaded();
|
|
|
369 |
|
|
|
370 |
if (!empty($plugins)) {
|
|
|
371 |
foreach ($plugins as $plugin) {
|
|
|
372 |
$pluginDomain = Inflector::underscore($plugin);
|
|
|
373 |
if ($pluginDomain === $domain) {
|
|
|
374 |
$searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
|
|
|
375 |
$searchPaths = array_reverse($searchPaths);
|
|
|
376 |
break;
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
}
|
|
|
380 |
|
|
|
381 |
foreach ($searchPaths as $directory) {
|
|
|
382 |
foreach ($this->l10n->languagePath as $lang) {
|
|
|
383 |
$localeDef = $directory . $lang . DS . $this->category;
|
|
|
384 |
if (is_file($localeDef)) {
|
|
|
385 |
$definitions = self::loadLocaleDefinition($localeDef);
|
|
|
386 |
if ($definitions !== false) {
|
|
|
387 |
$this->_domains[$domain][$this->_lang][$this->category] = $definitions;
|
|
|
388 |
$this->_noLocale = false;
|
|
|
389 |
return $domain;
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
if ($core) {
|
|
|
394 |
$app = $directory . $lang . DS . $this->category . DS . 'core';
|
|
|
395 |
$translations = false;
|
|
|
396 |
|
|
|
397 |
if (is_file($app . '.mo')) {
|
|
|
398 |
$translations = self::loadMo($app . '.mo');
|
|
|
399 |
}
|
|
|
400 |
if ($translations === false && is_file($app . '.po')) {
|
|
|
401 |
$translations = self::loadPo($app . '.po');
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
if ($translations !== false) {
|
|
|
405 |
$this->_domains[$domain][$this->_lang][$this->category] = $translations;
|
|
|
406 |
$merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
|
|
|
407 |
$this->_noLocale = false;
|
|
|
408 |
$core = null;
|
|
|
409 |
}
|
|
|
410 |
}
|
|
|
411 |
|
|
|
412 |
$file = $directory . $lang . DS . $this->category . DS . $domain;
|
|
|
413 |
$translations = false;
|
|
|
414 |
|
|
|
415 |
if (is_file($file . '.mo')) {
|
|
|
416 |
$translations = self::loadMo($file . '.mo');
|
|
|
417 |
}
|
|
|
418 |
if ($translations === false && is_file($file . '.po')) {
|
|
|
419 |
$translations = self::loadPo($file . '.po');
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
if ($translations !== false) {
|
|
|
423 |
$this->_domains[$domain][$this->_lang][$this->category] = $translations;
|
|
|
424 |
$this->_noLocale = false;
|
|
|
425 |
break 2;
|
|
|
426 |
}
|
|
|
427 |
}
|
|
|
428 |
}
|
|
|
429 |
|
|
|
430 |
if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
|
|
|
431 |
$this->_domains[$domain][$this->_lang][$this->category] = array();
|
|
|
432 |
return $domain;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
|
|
|
436 |
$head = $this->_domains[$domain][$this->_lang][$this->category][""];
|
|
|
437 |
|
|
|
438 |
foreach (explode("\n", $head) as $line) {
|
|
|
439 |
$header = strtok($line, ':');
|
|
|
440 |
$line = trim(strtok("\n"));
|
|
|
441 |
$this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
|
|
|
442 |
}
|
|
|
443 |
|
|
|
444 |
if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) {
|
|
|
445 |
$switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]);
|
|
|
446 |
$this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
|
|
|
447 |
unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
|
|
|
448 |
}
|
|
|
449 |
$this->_domains = Hash::mergeDiff($this->_domains, $merge);
|
|
|
450 |
|
|
|
451 |
if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
|
|
|
452 |
unset($this->_domains[$domain][$this->_lang][$this->category][null]);
|
|
|
453 |
}
|
|
|
454 |
}
|
|
|
455 |
|
|
|
456 |
return $domain;
|
|
|
457 |
}
|
|
|
458 |
|
|
|
459 |
/**
|
|
|
460 |
* Loads the binary .mo file and returns array of translations
|
|
|
461 |
*
|
|
|
462 |
* @param string $filename Binary .mo file to load
|
|
|
463 |
* @return mixed Array of translations on success or false on failure
|
|
|
464 |
*/
|
|
|
465 |
public static function loadMo($filename) {
|
|
|
466 |
$translations = false;
|
|
|
467 |
|
|
|
468 |
// @codingStandardsIgnoreStart
|
|
|
469 |
// Binary files extracted makes non-standard local variables
|
|
|
470 |
if ($data = file_get_contents($filename)) {
|
|
|
471 |
$translations = array();
|
|
|
472 |
$header = substr($data, 0, 20);
|
|
|
473 |
$header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
|
|
|
474 |
extract($header);
|
|
|
475 |
|
|
|
476 |
if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
|
|
|
477 |
for ($n = 0; $n < $count; $n++) {
|
|
|
478 |
$r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
|
|
|
479 |
$msgid = substr($data, $r["offs"], $r["len"]);
|
|
|
480 |
unset($msgid_plural);
|
|
|
481 |
|
|
|
482 |
if (strpos($msgid, "\000")) {
|
|
|
483 |
list($msgid, $msgid_plural) = explode("\000", $msgid);
|
|
|
484 |
}
|
|
|
485 |
$r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
|
|
|
486 |
$msgstr = substr($data, $r["offs"], $r["len"]);
|
|
|
487 |
|
|
|
488 |
if (strpos($msgstr, "\000")) {
|
|
|
489 |
$msgstr = explode("\000", $msgstr);
|
|
|
490 |
}
|
|
|
491 |
$translations[$msgid] = $msgstr;
|
|
|
492 |
|
|
|
493 |
if (isset($msgid_plural)) {
|
|
|
494 |
$translations[$msgid_plural] =& $translations[$msgid];
|
|
|
495 |
}
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
}
|
|
|
499 |
// @codingStandardsIgnoreEnd
|
|
|
500 |
|
|
|
501 |
return $translations;
|
|
|
502 |
}
|
|
|
503 |
|
|
|
504 |
/**
|
|
|
505 |
* Loads the text .po file and returns array of translations
|
|
|
506 |
*
|
|
|
507 |
* @param string $filename Text .po file to load
|
|
|
508 |
* @return mixed Array of translations on success or false on failure
|
|
|
509 |
*/
|
|
|
510 |
public static function loadPo($filename) {
|
|
|
511 |
if (!$file = fopen($filename, 'r')) {
|
|
|
512 |
return false;
|
|
|
513 |
}
|
|
|
514 |
|
|
|
515 |
$type = 0;
|
|
|
516 |
$translations = array();
|
|
|
517 |
$translationKey = '';
|
|
|
518 |
$plural = 0;
|
|
|
519 |
$header = '';
|
|
|
520 |
|
|
|
521 |
do {
|
|
|
522 |
$line = trim(fgets($file));
|
|
|
523 |
if ($line === '' || $line[0] === '#') {
|
|
|
524 |
continue;
|
|
|
525 |
}
|
|
|
526 |
if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
|
|
|
527 |
$type = 1;
|
|
|
528 |
$translationKey = stripcslashes($regs[1]);
|
|
|
529 |
} elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
|
|
|
530 |
$type = 2;
|
|
|
531 |
$translationKey = '';
|
|
|
532 |
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
|
|
|
533 |
$type = 3;
|
|
|
534 |
$translationKey .= stripcslashes($regs[1]);
|
|
|
535 |
} elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
|
|
|
536 |
$translations[$translationKey] = stripcslashes($regs[1]);
|
|
|
537 |
$type = 4;
|
|
|
538 |
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
|
|
|
539 |
$type = 4;
|
|
|
540 |
$translations[$translationKey] = '';
|
|
|
541 |
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
|
|
|
542 |
$translations[$translationKey] .= stripcslashes($regs[1]);
|
|
|
543 |
} elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
|
|
|
544 |
$type = 6;
|
|
|
545 |
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
|
|
|
546 |
$type = 6;
|
|
|
547 |
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
|
|
|
548 |
$plural = $regs[1];
|
|
|
549 |
$translations[$translationKey][$plural] = stripcslashes($regs[2]);
|
|
|
550 |
$type = 7;
|
|
|
551 |
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
|
|
|
552 |
$plural = $regs[1];
|
|
|
553 |
$translations[$translationKey][$plural] = '';
|
|
|
554 |
$type = 7;
|
|
|
555 |
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
|
|
|
556 |
$translations[$translationKey][$plural] .= stripcslashes($regs[1]);
|
|
|
557 |
} elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
|
|
|
558 |
$header .= stripcslashes($regs[1]);
|
|
|
559 |
$type = 5;
|
|
|
560 |
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
|
|
|
561 |
$header = '';
|
|
|
562 |
$type = 5;
|
|
|
563 |
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
|
|
|
564 |
$header .= stripcslashes($regs[1]);
|
|
|
565 |
} else {
|
|
|
566 |
unset($translations[$translationKey]);
|
|
|
567 |
$type = 0;
|
|
|
568 |
$translationKey = '';
|
|
|
569 |
$plural = 0;
|
|
|
570 |
}
|
|
|
571 |
} while (!feof($file));
|
|
|
572 |
fclose($file);
|
|
|
573 |
|
|
|
574 |
$merge[''] = $header;
|
|
|
575 |
return array_merge($merge, $translations);
|
|
|
576 |
}
|
|
|
577 |
|
|
|
578 |
/**
|
|
|
579 |
* Parses a locale definition file following the POSIX standard
|
|
|
580 |
*
|
|
|
581 |
* @param string $filename Locale definition filename
|
|
|
582 |
* @return mixed Array of definitions on success or false on failure
|
|
|
583 |
*/
|
|
|
584 |
public static function loadLocaleDefinition($filename) {
|
|
|
585 |
if (!$file = fopen($filename, 'r')) {
|
|
|
586 |
return false;
|
|
|
587 |
}
|
|
|
588 |
|
|
|
589 |
$definitions = array();
|
|
|
590 |
$comment = '#';
|
|
|
591 |
$escape = '\\';
|
|
|
592 |
$currentToken = false;
|
|
|
593 |
$value = '';
|
|
|
594 |
$_this = I18n::getInstance();
|
|
|
595 |
while ($line = fgets($file)) {
|
|
|
596 |
$line = trim($line);
|
|
|
597 |
if (empty($line) || $line[0] === $comment) {
|
|
|
598 |
continue;
|
|
|
599 |
}
|
|
|
600 |
$parts = preg_split("/[[:space:]]+/", $line);
|
|
|
601 |
if ($parts[0] === 'comment_char') {
|
|
|
602 |
$comment = $parts[1];
|
|
|
603 |
continue;
|
|
|
604 |
}
|
|
|
605 |
if ($parts[0] === 'escape_char') {
|
|
|
606 |
$escape = $parts[1];
|
|
|
607 |
continue;
|
|
|
608 |
}
|
|
|
609 |
$count = count($parts);
|
|
|
610 |
if ($count === 2) {
|
|
|
611 |
$currentToken = $parts[0];
|
|
|
612 |
$value = $parts[1];
|
|
|
613 |
} elseif ($count === 1) {
|
|
|
614 |
$value = is_array($value) ? $parts[0] : $value . $parts[0];
|
|
|
615 |
} else {
|
|
|
616 |
continue;
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
$len = strlen($value) - 1;
|
|
|
620 |
if ($value[$len] === $escape) {
|
|
|
621 |
$value = substr($value, 0, $len);
|
|
|
622 |
continue;
|
|
|
623 |
}
|
|
|
624 |
|
|
|
625 |
$mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
|
|
|
626 |
$replacements = array_map('crc32', $mustEscape);
|
|
|
627 |
$value = str_replace($mustEscape, $replacements, $value);
|
|
|
628 |
$value = explode(';', $value);
|
|
|
629 |
$_this->_escape = $escape;
|
|
|
630 |
foreach ($value as $i => $val) {
|
|
|
631 |
$val = trim($val, '"');
|
|
|
632 |
$val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
|
|
|
633 |
$val = str_replace($replacements, $mustEscape, $val);
|
|
|
634 |
$value[$i] = $val;
|
|
|
635 |
}
|
|
|
636 |
if (count($value) === 1) {
|
|
|
637 |
$definitions[$currentToken] = array_pop($value);
|
|
|
638 |
} else {
|
|
|
639 |
$definitions[$currentToken] = $value;
|
|
|
640 |
}
|
|
|
641 |
}
|
|
|
642 |
|
|
|
643 |
return $definitions;
|
|
|
644 |
}
|
|
|
645 |
|
|
|
646 |
/**
|
|
|
647 |
* Auxiliary function to parse a symbol from a locale definition file
|
|
|
648 |
*
|
|
|
649 |
* @param string $string Symbol to be parsed
|
|
|
650 |
* @return string parsed symbol
|
|
|
651 |
*/
|
|
|
652 |
protected function _parseLiteralValue($string) {
|
|
|
653 |
$string = $string[1];
|
|
|
654 |
if (substr($string, 0, 2) === $this->_escape . 'x') {
|
|
|
655 |
$delimiter = $this->_escape . 'x';
|
|
|
656 |
return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
|
|
|
657 |
}
|
|
|
658 |
if (substr($string, 0, 2) === $this->_escape . 'd') {
|
|
|
659 |
$delimiter = $this->_escape . 'd';
|
|
|
660 |
return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
|
|
|
661 |
}
|
|
|
662 |
if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
|
|
|
663 |
$delimiter = $this->_escape;
|
|
|
664 |
return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
|
|
|
665 |
}
|
|
|
666 |
if (substr($string, 0, 3) === 'U00') {
|
|
|
667 |
$delimiter = 'U00';
|
|
|
668 |
return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
|
|
|
669 |
}
|
|
|
670 |
if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
|
|
|
671 |
return Multibyte::ascii(array(hexdec($match[1])));
|
|
|
672 |
}
|
|
|
673 |
return $string;
|
|
|
674 |
}
|
|
|
675 |
|
|
|
676 |
/**
|
|
|
677 |
* Returns a Time format definition from corresponding domain
|
|
|
678 |
*
|
|
|
679 |
* @param string $format Format to be translated
|
|
|
680 |
* @param string $domain Domain where format is stored
|
|
|
681 |
* @return mixed translated format string if only value or array of translated strings for corresponding format.
|
|
|
682 |
*/
|
|
|
683 |
protected function _translateTime($format, $domain) {
|
|
|
684 |
if (!empty($this->_domains[$domain][$this->_lang]['LC_TIME'][$format])) {
|
|
|
685 |
if (($trans = $this->_domains[$domain][$this->_lang][$this->category][$format])) {
|
|
|
686 |
return $trans;
|
|
|
687 |
}
|
|
|
688 |
}
|
|
|
689 |
return $format;
|
|
|
690 |
}
|
|
|
691 |
|
|
|
692 |
}
|