~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/i18n.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Internationalization
 
4
 *
 
5
 * PHP versions 4 and 5
 
6
 *
 
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
8
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
9
 *
 
10
 * Licensed under The MIT License
 
11
 * Redistributions of files must retain the above copyright notice.
 
12
 *
 
13
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
14
 * @link          http://cakephp.org CakePHP(tm) Project
 
15
 * @package       cake
 
16
 * @subpackage    cake.cake.libs
 
17
 * @since         CakePHP(tm) v 1.2.0.4116
 
18
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
19
 */
 
20
 
 
21
/**
 
22
 * Included libraries.
 
23
 */
 
24
App::import('Core', array('l10n', 'Multibyte'));
 
25
 
 
26
/**
 
27
 * I18n handles translation of Text and time format strings.
 
28
 *
 
29
 * @package       cake
 
30
 * @subpackage    cake.cake.libs
 
31
 */
 
32
class I18n extends Object {
 
33
 
 
34
/**
 
35
 * Instance of the I10n class for localization
 
36
 *
 
37
 * @var I10n
 
38
 * @access public
 
39
 */
 
40
        var $l10n = null;
 
41
 
 
42
/**
 
43
 * Current domain of translation
 
44
 *
 
45
 * @var string
 
46
 * @access public
 
47
 */
 
48
        var $domain = null;
 
49
 
 
50
/**
 
51
 * Current category of translation
 
52
 *
 
53
 * @var string
 
54
 * @access public
 
55
 */
 
56
        var $category = 'LC_MESSAGES';
 
57
 
 
58
/**
 
59
 * Current language used for translations
 
60
 *
 
61
 * @var string
 
62
 * @access private
 
63
 */
 
64
        var $__lang = null;
 
65
 
 
66
/**
 
67
 * Translation strings for a specific domain read from the .mo or .po files
 
68
 *
 
69
 * @var array
 
70
 * @access private
 
71
 */
 
72
        var $__domains = array();
 
73
 
 
74
/**
 
75
 * Set to true when I18N::__bindTextDomain() is called for the first time.
 
76
 * If a translation file is found it is set to false again
 
77
 *
 
78
 * @var boolean
 
79
 * @access private
 
80
 */
 
81
        var $__noLocale = false;
 
82
 
 
83
/**
 
84
 * Set to true when I18N::__bindTextDomain() is called for the first time.
 
85
 * If a translation file is found it is set to false again
 
86
 *
 
87
 * @var array
 
88
 * @access private
 
89
 */
 
90
        var $__categories = array(
 
91
                 'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
 
92
        );
 
93
 
 
94
/**
 
95
 * Return a static instance of the I18n class
 
96
 *
 
97
 * @return object I18n
 
98
 * @access public
 
99
 */
 
100
        function &getInstance() {
 
101
                static $instance = array();
 
102
                if (!$instance) {
 
103
                        $instance[0] =& new I18n();
 
104
                        $instance[0]->l10n =& new L10n();
 
105
                }
 
106
                return $instance[0];
 
107
        }
 
108
 
 
109
/**
 
110
 * Used by the translation functions in basics.php
 
111
 * Can also be used like I18n::translate(); but only if the App::import('I18n'); has been used to load the class.
 
112
 *
 
113
 * @param string $singular String to translate
 
114
 * @param string $plural Plural string (if any)
 
115
 * @param string $domain Domain The domain of the translation.  Domains are often used by plugin translations
 
116
 * @param string $category Category The integer value of the category to use.
 
117
 * @param integer $count Count Count is used with $plural to choose the correct plural form.
 
118
 * @return string translated string.
 
119
 * @access public
 
120
 */
 
121
        function translate($singular, $plural = null, $domain = null, $category = 6, $count = null) {
 
122
                $_this =& I18n::getInstance();
 
123
                
 
124
                if (strpos($singular, "\r\n") !== false) {
 
125
                        $singular = str_replace("\r\n", "\n", $singular);
 
126
                }
 
127
                if ($plural !== null && strpos($plural, "\r\n") !== false) {
 
128
                        $plural = str_replace("\r\n", "\n", $plural);
 
129
                }
 
130
 
 
131
                if (is_numeric($category)) {
 
132
                        $_this->category = $_this->__categories[$category];
 
133
                }
 
134
                $language = Configure::read('Config.language');
 
135
 
 
136
                if (!empty($_SESSION['Config']['language'])) {
 
137
                        $language = $_SESSION['Config']['language'];
 
138
                }
 
139
 
 
140
                if (($_this->__lang && $_this->__lang !== $language) || !$_this->__lang) {
 
141
                        $lang = $_this->l10n->get($language);
 
142
                        $_this->__lang = $lang;
 
143
                }
 
144
 
 
145
                if (is_null($domain)) {
 
146
                        $domain = 'default';
 
147
                }
 
148
 
 
149
                $_this->domain = $domain . '_' . $_this->l10n->lang;
 
150
 
 
151
                if (empty($_this->__domains[$domain][$_this->__lang])) {
 
152
                        $_this->__domains[$domain][$_this->__lang] = Cache::read($_this->domain, '_cake_core_');
 
153
                }
 
154
 
 
155
                if (empty($_this->__domains[$domain][$_this->__lang][$_this->category])) {
 
156
                        $_this->__bindTextDomain($domain);
 
157
                        Cache::write($_this->domain, $_this->__domains[$domain][$_this->__lang], '_cake_core_');
 
158
                }
 
159
 
 
160
                if ($_this->category == 'LC_TIME') {
 
161
                        return $_this->__translateTime($singular,$domain);
 
162
                }
 
163
 
 
164
                if (!isset($count)) {
 
165
                        $plurals = 0;
 
166
                } elseif (!empty($_this->__domains[$domain][$_this->__lang][$_this->category]["%plural-c"]) && $_this->__noLocale === false) {
 
167
                        $header = $_this->__domains[$domain][$_this->__lang][$_this->category]["%plural-c"];
 
168
                        $plurals = $_this->__pluralGuess($header, $count);
 
169
                } else {
 
170
                        if ($count != 1) {
 
171
                                $plurals = 1;
 
172
                        } else {
 
173
                                $plurals = 0;
 
174
                        }
 
175
                }
 
176
 
 
177
                if (!empty($_this->__domains[$domain][$_this->__lang][$_this->category][$singular])) {
 
178
                        if (($trans = $_this->__domains[$domain][$_this->__lang][$_this->category][$singular]) || ($plurals) && ($trans = $_this->__domains[$domain][$_this->__lang][$_this->category][$plural])) {
 
179
                                if (is_array($trans)) {
 
180
                                        if (isset($trans[$plurals])) {
 
181
                                                $trans = $trans[$plurals];
 
182
                                        }
 
183
                                }
 
184
                                if (strlen($trans)) {
 
185
                                        return $trans;
 
186
                                }
 
187
                        }
 
188
                }
 
189
 
 
190
                if (!empty($plurals)) {
 
191
                        return $plural;
 
192
                }
 
193
                return $singular;
 
194
        }
 
195
 
 
196
/**
 
197
 * Clears the domains internal data array.  Useful for testing i18n.
 
198
 *
 
199
 * @return void
 
200
 */
 
201
        function clear() {
 
202
                $self =& I18n::getInstance();
 
203
                $self->__domains = array();
 
204
        }
 
205
 
 
206
/**
 
207
 * Attempts to find the plural form of a string.
 
208
 *
 
209
 * @param string $header Type
 
210
 * @param integrer $n Number
 
211
 * @return integer plural match
 
212
 * @access private
 
213
 */
 
214
        function __pluralGuess($header, $n) {
 
215
                if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
 
216
                        return 0;
 
217
                }
 
218
 
 
219
                if ($header === "nplurals=2;plural=n!=1;") {
 
220
                        return $n != 1 ? 1 : 0;
 
221
                } elseif ($header === "nplurals=2;plural=n>1;") {
 
222
                        return $n > 1 ? 1 : 0;
 
223
                }
 
224
 
 
225
                if (strpos($header, "plurals=3")) {
 
226
                        if (strpos($header, "100!=11")) {
 
227
                                if (strpos($header, "10<=4")) {
 
228
                                        return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
 
229
                                } elseif (strpos($header, "100<10")) {
 
230
                                        return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
 
231
                                }
 
232
                                return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
 
233
                        } elseif (strpos($header, "n==2")) {
 
234
                                return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
 
235
                        } elseif (strpos($header, "n==0")) {
 
236
                                return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
 
237
                        } elseif (strpos($header, "n>=2")) {
 
238
                                return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
 
239
                        } elseif (strpos($header, "10>=2")) {
 
240
                                return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
 
241
                        }
 
242
                        return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
 
243
                } elseif (strpos($header, "plurals=4")) {
 
244
                        if (strpos($header, "100==2")) {
 
245
                                return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
 
246
                        } elseif (strpos($header, "n>=3")) {
 
247
                                return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
 
248
                        } elseif (strpos($header, "100>=1")) {
 
249
                                return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
 
250
                        }
 
251
                } elseif (strpos($header, "plurals=5")) {
 
252
                        return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
 
253
                }
 
254
        }
 
255
 
 
256
/**
 
257
 * Binds the given domain to a file in the specified directory.
 
258
 *
 
259
 * @param string $domain Domain to bind
 
260
 * @return string Domain binded
 
261
 * @access private
 
262
 */
 
263
        function __bindTextDomain($domain) {
 
264
                $this->__noLocale = true;
 
265
                $core = true;
 
266
                $merge = array();
 
267
                $searchPaths = App::path('locales');
 
268
                $plugins = App::objects('plugin');
 
269
 
 
270
                if (!empty($plugins)) {
 
271
                        foreach ($plugins as $plugin) {
 
272
                                $plugin = Inflector::underscore($plugin);
 
273
                                if ($plugin === $domain) {
 
274
                                        $searchPaths[] = App::pluginPath($plugin) . DS . 'locale' . DS;
 
275
                                        $searchPaths = array_reverse($searchPaths);
 
276
                                        break;
 
277
                                }
 
278
                        }
 
279
                }
 
280
 
 
281
 
 
282
                foreach ($searchPaths as $directory) {
 
283
 
 
284
                        foreach ($this->l10n->languagePath as $lang) {
 
285
                                $file = $directory . $lang . DS . $this->category . DS . $domain;
 
286
                                $localeDef = $directory . $lang . DS . $this->category;
 
287
 
 
288
                                if ($core) {
 
289
                                        $app = $directory . $lang . DS . $this->category . DS . 'core';
 
290
 
 
291
                                        if (file_exists($fn = "$app.mo")) {
 
292
                                                $this->__loadMo($fn, $domain);
 
293
                                                $this->__noLocale = false;
 
294
                                                $merge[$domain][$this->__lang][$this->category] = $this->__domains[$domain][$this->__lang][$this->category];
 
295
                                                $core = null;
 
296
                                        } elseif (file_exists($fn = "$app.po") && ($f = fopen($fn, "r"))) {
 
297
                                                $this->__loadPo($f, $domain);
 
298
                                                $this->__noLocale = false;
 
299
                                                $merge[$domain][$this->__lang][$this->category] = $this->__domains[$domain][$this->__lang][$this->category];
 
300
                                                $core = null;
 
301
                                        }
 
302
                                }
 
303
 
 
304
                                if (file_exists($fn = "$file.mo")) {
 
305
                                        $this->__loadMo($fn, $domain);
 
306
                                        $this->__noLocale = false;
 
307
                                        break 2;
 
308
                                } elseif (file_exists($fn = "$file.po") && ($f = fopen($fn, "r"))) {
 
309
                                        $this->__loadPo($f, $domain);
 
310
                                        $this->__noLocale = false;
 
311
                                        break 2;
 
312
                                } elseif (is_file($localeDef) && ($f = fopen($localeDef, "r"))) {
 
313
                                        $this->__loadLocaleDefinition($f, $domain);
 
314
                                        $this->__noLocale = false;
 
315
                                        return $domain;
 
316
                                }
 
317
                        }
 
318
                }
 
319
 
 
320
                if (empty($this->__domains[$domain][$this->__lang][$this->category])) {
 
321
                        $this->__domains[$domain][$this->__lang][$this->category] = array();
 
322
                        return $domain;
 
323
                }
 
324
 
 
325
                if ($head = $this->__domains[$domain][$this->__lang][$this->category][""]) {
 
326
                        foreach (explode("\n", $head) as $line) {
 
327
                                $header = strtok($line,":");
 
328
                                $line = trim(strtok("\n"));
 
329
                                $this->__domains[$domain][$this->__lang][$this->category]["%po-header"][strtolower($header)] = $line;
 
330
                        }
 
331
 
 
332
                        if (isset($this->__domains[$domain][$this->__lang][$this->category]["%po-header"]["plural-forms"])) {
 
333
                                $switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->__domains[$domain][$this->__lang][$this->category]["%po-header"]["plural-forms"]);
 
334
                                $this->__domains[$domain][$this->__lang][$this->category]["%plural-c"] = $switch;
 
335
                                unset($this->__domains[$domain][$this->__lang][$this->category]["%po-header"]);
 
336
                        }
 
337
                        $this->__domains = Set::pushDiff($this->__domains, $merge);
 
338
 
 
339
                        if (isset($this->__domains[$domain][$this->__lang][$this->category][null])) {
 
340
                                unset($this->__domains[$domain][$this->__lang][$this->category][null]);
 
341
                        }
 
342
                }
 
343
                return $domain;
 
344
        }
 
345
 
 
346
/**
 
347
 * Loads the binary .mo file for translation and sets the values for this translation in the var I18n::__domains
 
348
 *
 
349
 * @param resource $file Binary .mo file to load
 
350
 * @param string $domain Domain where to load file in
 
351
 * @access private
 
352
 */
 
353
        function __loadMo($file, $domain) {
 
354
                $data = file_get_contents($file);
 
355
 
 
356
                if ($data) {
 
357
                        $header = substr($data, 0, 20);
 
358
                        $header = unpack("L1magic/L1version/L1count/L1o_msg/L1o_trn", $header);
 
359
                        extract($header);
 
360
 
 
361
                        if ((dechex($magic) == '950412de' || dechex($magic) == 'ffffffff950412de') && $version == 0) {
 
362
                                for ($n = 0; $n < $count; $n++) {
 
363
                                        $r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
 
364
                                        $msgid = substr($data, $r["offs"], $r["len"]);
 
365
                                        unset($msgid_plural);
 
366
 
 
367
                                        if (strpos($msgid, "\000")) {
 
368
                                                list($msgid, $msgid_plural) = explode("\000", $msgid);
 
369
                                        }
 
370
                                        $r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
 
371
                                        $msgstr = substr($data, $r["offs"], $r["len"]);
 
372
 
 
373
                                        if (strpos($msgstr, "\000")) {
 
374
                                                $msgstr = explode("\000", $msgstr);
 
375
                                        }
 
376
                                        $this->__domains[$domain][$this->__lang][$this->category][$msgid] = $msgstr;
 
377
 
 
378
                                        if (isset($msgid_plural)) {
 
379
                                                $this->__domains[$domain][$this->__lang][$this->category][$msgid_plural] =& $this->__domains[$domain][$this->__lang][$this->category][$msgid];
 
380
                                        }
 
381
                                }
 
382
                        }
 
383
                }
 
384
        }
 
385
 
 
386
/**
 
387
 * Loads the text .po file for translation and sets the values for this translation in the var I18n::__domains
 
388
 *
 
389
 * @param resource $file Text .po file to load
 
390
 * @param string $domain Domain to load file in
 
391
 * @return array Binded domain elements
 
392
 * @access private
 
393
 */
 
394
        function __loadPo($file, $domain) {
 
395
                $type = 0;
 
396
                $translations = array();
 
397
                $translationKey = "";
 
398
                $plural = 0;
 
399
                $header = "";
 
400
 
 
401
                do {
 
402
                        $line = trim(fgets($file));
 
403
                        if ($line == "" || $line[0] == "#") {
 
404
                                continue;
 
405
                        }
 
406
                        if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
 
407
                                $type = 1;
 
408
                                $translationKey = stripcslashes($regs[1]);
 
409
                        } elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
 
410
                                $type = 2;
 
411
                                $translationKey = "";
 
412
                        } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
 
413
                                $type = 3;
 
414
                                $translationKey .= stripcslashes($regs[1]);
 
415
                        } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
 
416
                                $translations[$translationKey] = stripcslashes($regs[1]);
 
417
                                $type = 4;
 
418
                        } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
 
419
                                $type = 4;
 
420
                                $translations[$translationKey] = "";
 
421
                        } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
 
422
                                $translations[$translationKey] .= stripcslashes($regs[1]);
 
423
                        } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
 
424
                                $type = 6;
 
425
                        } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
 
426
                                $type = 6;
 
427
                        } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
 
428
                                $plural = $regs[1];
 
429
                                $translations[$translationKey][$plural] = stripcslashes($regs[2]);
 
430
                                $type = 7;
 
431
                        } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
 
432
                                $plural = $regs[1];
 
433
                                $translations[$translationKey][$plural] = "";
 
434
                                $type = 7;
 
435
                        } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
 
436
                                $translations[$translationKey][$plural] .= stripcslashes($regs[1]);
 
437
                        } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
 
438
                                $header .= stripcslashes($regs[1]);
 
439
                                $type = 5;
 
440
                        } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
 
441
                                $header = "";
 
442
                                $type = 5;
 
443
                        } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
 
444
                                $header .= stripcslashes($regs[1]);
 
445
                        } else {
 
446
                                unset($translations[$translationKey]);
 
447
                                $type = 0;
 
448
                                $translationKey = "";
 
449
                                $plural = 0;
 
450
                        }
 
451
                } while (!feof($file));
 
452
                fclose($file);
 
453
                $merge[""] = $header;
 
454
                return $this->__domains[$domain][$this->__lang][$this->category] = array_merge($merge ,$translations);
 
455
        }
 
456
 
 
457
/**
 
458
 * Parses a locale definition file following the POSIX standard
 
459
 *
 
460
 * @param resource $file file handler
 
461
 * @param string $domain Domain where locale definitions will be stored
 
462
 * @return void
 
463
 * @access private
 
464
 */
 
465
        function __loadLocaleDefinition($file, $domain = null) {
 
466
                $comment = '#';
 
467
                $escape = '\\';
 
468
                $currentToken = false;
 
469
                $value = '';
 
470
                while ($line = fgets($file)) {
 
471
                        $line = trim($line);
 
472
                        if (empty($line) || $line[0] === $comment) {
 
473
                                continue;
 
474
                        }
 
475
                        $parts = preg_split("/[[:space:]]+/",$line);
 
476
                        if ($parts[0] === 'comment_char') {
 
477
                                $comment = $parts[1];
 
478
                                continue;
 
479
                        }
 
480
                        if ($parts[0] === 'escape_char') {
 
481
                                $escape = $parts[1];
 
482
                                continue;
 
483
                        }
 
484
                        $count = count($parts);
 
485
                        if ($count == 2) {
 
486
                                $currentToken = $parts[0];
 
487
                                $value = $parts[1];
 
488
                        } elseif ($count == 1) {
 
489
                                $value .= $parts[0];
 
490
                        } else {
 
491
                                continue;
 
492
                        }
 
493
 
 
494
                        $len = strlen($value) - 1;
 
495
                        if ($value[$len] === $escape) {
 
496
                                $value = substr($value, 0, $len);
 
497
                                continue;
 
498
                        }
 
499
 
 
500
                        $mustEscape = array($escape . ',' , $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
 
501
                        $replacements = array_map('crc32', $mustEscape);
 
502
                        $value = str_replace($mustEscape, $replacements, $value);
 
503
                        $value = explode(';', $value);
 
504
                        $this->__escape = $escape;
 
505
                        foreach ($value as $i => $val) {
 
506
                                $val = trim($val, '"');
 
507
                                $val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$this, '__parseLiteralValue'), $val);
 
508
                                $val = str_replace($replacements, $mustEscape, $val);
 
509
                                $value[$i] = $val;
 
510
                        }
 
511
                        if (count($value) == 1) {
 
512
                                $this->__domains[$domain][$this->__lang][$this->category][$currentToken] = array_pop($value);
 
513
                        } else {
 
514
                                $this->__domains[$domain][$this->__lang][$this->category][$currentToken] = $value;
 
515
                        }
 
516
                }
 
517
        }
 
518
 
 
519
/**
 
520
 * Auxiliary function to parse a symbol from a locale definition file
 
521
 *
 
522
 * @param string $string Symbol to be parsed
 
523
 * @return string parsed symbol
 
524
 * @access private
 
525
 */
 
526
        function __parseLiteralValue($string) {
 
527
                $string = $string[1];
 
528
                if (substr($string, 0, 2) === $this->__escape . 'x') {
 
529
                        $delimiter = $this->__escape . 'x';
 
530
                        return join('', array_map('chr', array_map('hexdec',array_filter(explode($delimiter, $string)))));
 
531
                }
 
532
                if (substr($string, 0, 2) === $this->__escape . 'd') {
 
533
                        $delimiter = $this->__escape . 'd';
 
534
                        return join('', array_map('chr', array_filter(explode($delimiter, $string))));
 
535
                }
 
536
                if ($string[0] === $this->__escape && isset($string[1]) && is_numeric($string[1])) {
 
537
                        $delimiter = $this->__escape;
 
538
                        return join('', array_map('chr', array_filter(explode($delimiter, $string))));
 
539
                }
 
540
                if (substr($string, 0, 3) === 'U00') {
 
541
                        $delimiter = 'U00';
 
542
                        return join('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
 
543
                }
 
544
                if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
 
545
                        return Multibyte::ascii(array(hexdec($match[1])));
 
546
                }
 
547
                return $string;
 
548
        }
 
549
 
 
550
/**
 
551
 * Returns a Time format definition from corresponding domain
 
552
 *
 
553
 * @param string $format Format to be translated
 
554
 * @param string $domain Domain where format is stored
 
555
 * @return mixed translated format string if only value or array of translated strings for corresponding format.
 
556
 * @access private
 
557
 */
 
558
        function __translateTime($format, $domain) {
 
559
                if (!empty($this->__domains[$domain][$this->__lang]['LC_TIME'][$format])) {
 
560
                        if (($trans = $this->__domains[$domain][$this->__lang][$this->category][$format])) {
 
561
                                return $trans;
 
562
                        }
 
563
                }
 
564
                return $format;
 
565
        }
 
566
}