Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
 * @param string $context Context The context of the translation, e.g a verb or a noun.
192
 * @return string translated string.
193
 * @throws CakeException When '' is provided as a domain.
194
 */
195
	public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES,
196
		$count = null, $language = null, $context = null
197
	) {
198
		$_this = I18n::getInstance();
199
 
200
		if (strpos($singular, "\r\n") !== false) {
201
			$singular = str_replace("\r\n", "\n", $singular);
202
		}
203
		if ($plural !== null && strpos($plural, "\r\n") !== false) {
204
			$plural = str_replace("\r\n", "\n", $plural);
205
		}
206
 
207
		if (is_numeric($category)) {
208
			$_this->category = $_this->_categories[$category];
209
		}
210
 
211
		if (empty($language)) {
212
			if (CakeSession::started()) {
213
				$language = CakeSession::read('Config.language');
214
			}
215
			if (empty($language)) {
216
				$language = Configure::read('Config.language');
217
			}
218
		}
219
 
220
		if (($_this->_lang && $_this->_lang !== $language) || !$_this->_lang) {
221
			$lang = $_this->l10n->get($language);
222
			$_this->_lang = $lang;
223
		}
224
 
225
		if ($domain === null) {
226
			$domain = self::$defaultDomain;
227
		}
228
		if ($domain === '') {
229
			throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
230
		}
231
 
232
		$_this->domain = $domain . '_' . $_this->l10n->lang;
233
 
234
		if (!isset($_this->_domains[$domain][$_this->_lang])) {
235
			$_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
236
		}
237
 
238
		if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
239
			$_this->_bindTextDomain($domain);
240
			Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
241
		}
242
 
243
		if ($_this->category === 'LC_TIME') {
244
			return $_this->_translateTime($singular, $domain);
245
		}
246
 
247
		if (!isset($count)) {
248
			$plurals = 0;
249
		} elseif (!empty($_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"]) && $_this->_noLocale === false) {
250
			$header = $_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"];
251
			$plurals = $_this->_pluralGuess($header, $count);
252
		} else {
253
			if ($count != 1) {
254
				$plurals = 1;
255
			} else {
256
				$plurals = 0;
257
			}
258
		}
259
 
260
		if (!empty($_this->_domains[$domain][$_this->_lang][$_this->category][$singular][$context])) {
261
			if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular][$context]) ||
262
				($plurals) && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural][$context])
263
			) {
264
				if (is_array($trans)) {
265
					if (isset($trans[$plurals])) {
266
						$trans = $trans[$plurals];
267
					} else {
268
						trigger_error(
269
							__d('cake_dev',
270
								'Missing plural form translation for "%s" in "%s" domain, "%s" locale. ' .
271
								' Check your po file for correct plurals and valid Plural-Forms header.',
272
								$singular,
273
								$domain,
274
								$_this->_lang
275
							),
276
							E_USER_WARNING
277
						);
278
						$trans = $trans[0];
279
					}
280
				}
281
				if (strlen($trans)) {
282
					return $trans;
283
				}
284
			}
285
		}
286
 
287
		if (!empty($plurals)) {
288
			return $plural;
289
		}
290
		return $singular;
291
	}
292
 
293
/**
294
 * Clears the domains internal data array. Useful for testing i18n.
295
 *
296
 * @return void
297
 */
298
	public static function clear() {
299
		$self = I18n::getInstance();
300
		$self->_domains = array();
301
	}
302
 
303
/**
304
 * Get the loaded domains cache.
305
 *
306
 * @return array
307
 */
308
	public static function domains() {
309
		$self = I18n::getInstance();
310
		return $self->_domains;
311
	}
312
 
313
/**
314
 * Attempts to find the plural form of a string.
315
 *
316
 * @param string $header Type
317
 * @param int $n Number
318
 * @return int plural match
319
 * @link http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
320
 * @link https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals#List_of_Plural_Rules
321
 */
322
	protected function _pluralGuess($header, $n) {
323
		if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
324
			return 0;
325
		}
326
 
327
		if ($header === "nplurals=2;plural=n!=1;") {
328
			return $n != 1 ? 1 : 0;
329
		} elseif ($header === "nplurals=2;plural=n>1;") {
330
			return $n > 1 ? 1 : 0;
331
		}
332
 
333
		if (strpos($header, "plurals=3")) {
334
			if (strpos($header, "100!=11")) {
335
				if (strpos($header, "10<=4")) {
336
					return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
337
				} elseif (strpos($header, "100<10")) {
338
					return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
339
				}
340
				return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
341
			} elseif (strpos($header, "n==2")) {
342
				return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
343
			} elseif (strpos($header, "n==0")) {
344
				return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
345
			} elseif (strpos($header, "n>=2")) {
346
				return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
347
			} elseif (strpos($header, "10>=2")) {
348
				return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
349
			}
350
			return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
351
		} elseif (strpos($header, "plurals=4")) {
352
			if (strpos($header, "100==2")) {
353
				return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
354
			} elseif (strpos($header, "n>=3")) {
355
				return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
356
			} elseif (strpos($header, "100>=1")) {
357
				return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
358
			}
359
		} elseif (strpos($header, "plurals=5")) {
360
			return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
361
		} elseif (strpos($header, "plurals=6")) {
362
			return $n == 0 ? 0 :
363
				($n == 1 ? 1 :
364
				($n == 2 ? 2 :
365
				($n % 100 >= 3 && $n % 100 <= 10 ? 3 :
366
				($n % 100 >= 11 ? 4 : 5))));
367
		}
368
 
369
		return 0;
370
	}
371
 
372
/**
373
 * Binds the given domain to a file in the specified directory.
374
 *
375
 * @param string $domain Domain to bind
376
 * @return string Domain binded
377
 */
378
	protected function _bindTextDomain($domain) {
379
		$this->_noLocale = true;
380
		$core = true;
381
		$merge = array();
382
		$searchPaths = App::path('locales');
383
		$plugins = CakePlugin::loaded();
384
 
385
		if (!empty($plugins)) {
386
			foreach ($plugins as $plugin) {
387
				$pluginDomain = Inflector::underscore($plugin);
388
				if ($pluginDomain === $domain) {
389
					$searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
390
					if (!Configure::read('I18n.preferApp')) {
391
						$searchPaths = array_reverse($searchPaths);
392
					}
393
					break;
394
				}
395
			}
396
		}
397
 
398
		foreach ($searchPaths as $directory) {
399
			foreach ($this->l10n->languagePath as $lang) {
400
				$localeDef = $directory . $lang . DS . $this->category;
401
				if (is_file($localeDef)) {
402
					$definitions = self::loadLocaleDefinition($localeDef);
403
					if ($definitions !== false) {
404
						$this->_domains[$domain][$this->_lang][$this->category] = $definitions;
405
						$this->_noLocale = false;
406
						return $domain;
407
					}
408
				}
409
 
410
				if ($core) {
411
					$app = $directory . $lang . DS . $this->category . DS . 'core';
412
					$translations = false;
413
 
414
					if (is_file($app . '.mo')) {
415
						$translations = self::loadMo($app . '.mo');
416
					}
417
					if ($translations === false && is_file($app . '.po')) {
418
						$translations = self::loadPo($app . '.po');
419
					}
420
 
421
					if ($translations !== false) {
422
						$this->_domains[$domain][$this->_lang][$this->category] = $translations;
423
						$merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
424
						$this->_noLocale = false;
425
						$core = null;
426
					}
427
				}
428
 
429
				$file = $directory . $lang . DS . $this->category . DS . $domain;
430
				$translations = false;
431
 
432
				if (is_file($file . '.mo')) {
433
					$translations = self::loadMo($file . '.mo');
434
				}
435
				if ($translations === false && is_file($file . '.po')) {
436
					$translations = self::loadPo($file . '.po');
437
				}
438
 
439
				if ($translations !== false) {
440
					$this->_domains[$domain][$this->_lang][$this->category] = $translations;
441
					$this->_noLocale = false;
442
					break 2;
443
				}
444
			}
445
		}
446
 
447
		if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
448
			$this->_domains[$domain][$this->_lang][$this->category] = array();
449
			return $domain;
450
		}
451
 
452
		if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
453
			$head = $this->_domains[$domain][$this->_lang][$this->category][""];
454
 
455
			foreach (explode("\n", $head) as $line) {
456
				$header = strtok($line, ':');
457
				$line = trim(strtok("\n"));
458
				$this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
459
			}
460
 
461
			if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) {
462
				$switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]);
463
				$this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
464
				unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
465
			}
466
			$this->_domains = Hash::mergeDiff($this->_domains, $merge);
467
 
468
			if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
469
				unset($this->_domains[$domain][$this->_lang][$this->category][null]);
470
			}
471
		}
472
 
473
		return $domain;
474
	}
475
 
476
/**
477
 * Loads the binary .mo file and returns array of translations
478
 *
479
 * @param string $filename Binary .mo file to load
480
 * @return mixed Array of translations on success or false on failure
481
 */
482
	public static function loadMo($filename) {
483
		$translations = false;
484
 
485
		// @codingStandardsIgnoreStart
486
		// Binary files extracted makes non-standard local variables
487
		if ($data = file_get_contents($filename)) {
488
			$translations = array();
489
			$context = null;
490
			$header = substr($data, 0, 20);
491
			$header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
492
			extract($header);
493
 
494
			if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
495
				for ($n = 0; $n < $count; $n++) {
496
					$r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
497
					$msgid = substr($data, $r["offs"], $r["len"]);
498
					unset($msgid_plural);
499
 
500
					if (strpos($msgid, "\000")) {
501
						list($msgid, $msgid_plural) = explode("\000", $msgid);
502
					}
503
					$r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
504
					$msgstr = substr($data, $r["offs"], $r["len"]);
505
 
506
					if (strpos($msgstr, "\000")) {
507
						$msgstr = explode("\000", $msgstr);
508
					}
509
 
510
					if ($msgid != '') {
511
						$msgstr = array($context => $msgstr);
512
					}
513
					$translations[$msgid] = $msgstr;
514
 
515
					if (isset($msgid_plural)) {
516
						$translations[$msgid_plural] =& $translations[$msgid];
517
					}
518
				}
519
			}
520
		}
521
		// @codingStandardsIgnoreEnd
522
 
523
		return $translations;
524
	}
525
 
526
/**
527
 * Loads the text .po file and returns array of translations
528
 *
529
 * @param string $filename Text .po file to load
530
 * @return mixed Array of translations on success or false on failure
531
 */
532
	public static function loadPo($filename) {
533
		if (!$file = fopen($filename, 'r')) {
534
			return false;
535
		}
536
 
537
		$type = 0;
538
		$translations = array();
539
		$translationKey = '';
540
		$translationContext = null;
541
		$plural = 0;
542
		$header = '';
543
 
544
		do {
545
			$line = trim(fgets($file));
546
			if ($line === '' || $line[0] === '#') {
547
				$translationContext = null;
548
 
549
				continue;
550
			}
551
			if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
552
				$type = 1;
553
				$translationKey = stripcslashes($regs[1]);
554
			} elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
555
				$type = 2;
556
				$translationKey = '';
557
			} elseif (preg_match("/msgctxt[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
558
				$translationContext = $regs[1];
559
			} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
560
				$type = 3;
561
				$translationKey .= stripcslashes($regs[1]);
562
			} elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
563
				$translations[$translationKey][$translationContext] = stripcslashes($regs[1]);
564
				$type = 4;
565
			} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
566
				$type = 4;
567
				$translations[$translationKey][$translationContext] = '';
568
			} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
569
				$translations[$translationKey][$translationContext] .= stripcslashes($regs[1]);
570
			} elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
571
				$type = 6;
572
			} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
573
				$type = 6;
574
			} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
575
				$plural = $regs[1];
576
				$translations[$translationKey][$translationContext][$plural] = stripcslashes($regs[2]);
577
				$type = 7;
578
			} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
579
				$plural = $regs[1];
580
				$translations[$translationKey][$translationContext][$plural] = '';
581
				$type = 7;
582
			} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
583
				$translations[$translationKey][$translationContext][$plural] .= stripcslashes($regs[1]);
584
			} elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
585
				$header .= stripcslashes($regs[1]);
586
				$type = 5;
587
			} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
588
				$header = '';
589
				$type = 5;
590
			} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
591
				$header .= stripcslashes($regs[1]);
592
			} else {
593
				unset($translations[$translationKey][$translationContext]);
594
				$type = 0;
595
				$translationKey = '';
596
				$translationContext = null;
597
				$plural = 0;
598
			}
599
		} while (!feof($file));
600
		fclose($file);
601
 
602
		$merge[''] = $header;
603
		return array_merge($merge, $translations);
604
	}
605
 
606
/**
607
 * Parses a locale definition file following the POSIX standard
608
 *
609
 * @param string $filename Locale definition filename
610
 * @return mixed Array of definitions on success or false on failure
611
 */
612
	public static function loadLocaleDefinition($filename) {
613
		if (!$file = fopen($filename, 'r')) {
614
			return false;
615
		}
616
 
617
		$definitions = array();
618
		$comment = '#';
619
		$escape = '\\';
620
		$currentToken = false;
621
		$value = '';
622
		$_this = I18n::getInstance();
623
		while ($line = fgets($file)) {
624
			$line = trim($line);
625
			if (empty($line) || $line[0] === $comment) {
626
				continue;
627
			}
628
			$parts = preg_split("/[[:space:]]+/", $line);
629
			if ($parts[0] === 'comment_char') {
630
				$comment = $parts[1];
631
				continue;
632
			}
633
			if ($parts[0] === 'escape_char') {
634
				$escape = $parts[1];
635
				continue;
636
			}
637
			$count = count($parts);
638
			if ($count === 2) {
639
				$currentToken = $parts[0];
640
				$value = $parts[1];
641
			} elseif ($count === 1) {
642
				$value = is_array($value) ? $parts[0] : $value . $parts[0];
643
			} else {
644
				continue;
645
			}
646
 
647
			$len = strlen($value) - 1;
648
			if ($value[$len] === $escape) {
649
				$value = substr($value, 0, $len);
650
				continue;
651
			}
652
 
653
			$mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
654
			$replacements = array_map('crc32', $mustEscape);
655
			$value = str_replace($mustEscape, $replacements, $value);
656
			$value = explode(';', $value);
657
			$_this->_escape = $escape;
658
			foreach ($value as $i => $val) {
659
				$val = trim($val, '"');
660
				$val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
661
				$val = str_replace($replacements, $mustEscape, $val);
662
				$value[$i] = $val;
663
			}
664
			if (count($value) === 1) {
665
				$definitions[$currentToken] = array_pop($value);
666
			} else {
667
				$definitions[$currentToken] = $value;
668
			}
669
		}
670
 
671
		return $definitions;
672
	}
673
 
674
/**
675
 * Puts the parameters in raw translated strings
676
 *
677
 * @param string $translated The raw translated string
678
 * @param array $args The arguments to put in the translation
679
 * @return string Translated string with arguments
680
 */
681
	public static function insertArgs($translated, array $args) {
682
		$len = count($args);
683
		if ($len === 0 || ($len === 1 && $args[0] === null)) {
684
			return $translated;
685
		}
686
 
687
		if (is_array($args[0])) {
688
			$args = $args[0];
689
		}
690
 
691
		$translated = preg_replace('/(?<!%)%(?![%\'\-+bcdeEfFgGosuxX\d\.])/', '%%', $translated);
692
		return vsprintf($translated, $args);
693
	}
694
 
695
/**
696
 * Auxiliary function to parse a symbol from a locale definition file
697
 *
698
 * @param string $string Symbol to be parsed
699
 * @return string parsed symbol
700
 */
701
	protected function _parseLiteralValue($string) {
702
		$string = $string[1];
703
		if (substr($string, 0, 2) === $this->_escape . 'x') {
704
			$delimiter = $this->_escape . 'x';
705
			return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
706
		}
707
		if (substr($string, 0, 2) === $this->_escape . 'd') {
708
			$delimiter = $this->_escape . 'd';
709
			return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
710
		}
711
		if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
712
			$delimiter = $this->_escape;
713
			return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
714
		}
715
		if (substr($string, 0, 3) === 'U00') {
716
			$delimiter = 'U00';
717
			return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
718
		}
719
		if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
720
			return Multibyte::ascii(array(hexdec($match[1])));
721
		}
722
		return $string;
723
	}
724
 
725
/**
726
 * Returns a Time format definition from corresponding domain
727
 *
728
 * @param string $format Format to be translated
729
 * @param string $domain Domain where format is stored
730
 * @return mixed translated format string if only value or array of translated strings for corresponding format.
731
 */
732
	protected function _translateTime($format, $domain) {
733
		if (!empty($this->_domains[$domain][$this->_lang]['LC_TIME'][$format])) {
734
			if (($trans = $this->_domains[$domain][$this->_lang][$this->category][$format])) {
735
				return $trans;
736
			}
737
		}
738
		return $format;
739
	}
740
 
741
}