7
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
10
* Licensed under The MIT License
11
* Redistributions of files must retain the above copyright notice.
13
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* @link http://cakephp.org CakePHP(tm) Project
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)
24
App::import('Core', array('l10n', 'Multibyte'));
27
* I18n handles translation of Text and time format strings.
30
* @subpackage cake.cake.libs
32
class I18n extends Object {
35
* Instance of the I10n class for localization
43
* Current domain of translation
51
* Current category of translation
56
var $category = 'LC_MESSAGES';
59
* Current language used for translations
67
* Translation strings for a specific domain read from the .mo or .po files
72
var $__domains = array();
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
81
var $__noLocale = false;
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
90
var $__categories = array(
91
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
95
* Return a static instance of the I18n class
100
function &getInstance() {
101
static $instance = array();
103
$instance[0] =& new I18n();
104
$instance[0]->l10n =& new L10n();
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.
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.
121
function translate($singular, $plural = null, $domain = null, $category = 6, $count = null) {
122
$_this =& I18n::getInstance();
124
if (strpos($singular, "\r\n") !== false) {
125
$singular = str_replace("\r\n", "\n", $singular);
127
if ($plural !== null && strpos($plural, "\r\n") !== false) {
128
$plural = str_replace("\r\n", "\n", $plural);
131
if (is_numeric($category)) {
132
$_this->category = $_this->__categories[$category];
134
$language = Configure::read('Config.language');
136
if (!empty($_SESSION['Config']['language'])) {
137
$language = $_SESSION['Config']['language'];
140
if (($_this->__lang && $_this->__lang !== $language) || !$_this->__lang) {
141
$lang = $_this->l10n->get($language);
142
$_this->__lang = $lang;
145
if (is_null($domain)) {
149
$_this->domain = $domain . '_' . $_this->l10n->lang;
151
if (empty($_this->__domains[$domain][$_this->__lang])) {
152
$_this->__domains[$domain][$_this->__lang] = Cache::read($_this->domain, '_cake_core_');
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_');
160
if ($_this->category == 'LC_TIME') {
161
return $_this->__translateTime($singular,$domain);
164
if (!isset($count)) {
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);
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];
184
if (strlen($trans)) {
190
if (!empty($plurals)) {
197
* Clears the domains internal data array. Useful for testing i18n.
202
$self =& I18n::getInstance();
203
$self->__domains = array();
207
* Attempts to find the plural form of a string.
209
* @param string $header Type
210
* @param integrer $n Number
211
* @return integer plural match
214
function __pluralGuess($header, $n) {
215
if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
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;
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);
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);
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));
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)));
257
* Binds the given domain to a file in the specified directory.
259
* @param string $domain Domain to bind
260
* @return string Domain binded
263
function __bindTextDomain($domain) {
264
$this->__noLocale = true;
267
$searchPaths = App::path('locales');
268
$plugins = App::objects('plugin');
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);
282
foreach ($searchPaths as $directory) {
284
foreach ($this->l10n->languagePath as $lang) {
285
$file = $directory . $lang . DS . $this->category . DS . $domain;
286
$localeDef = $directory . $lang . DS . $this->category;
289
$app = $directory . $lang . DS . $this->category . DS . 'core';
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];
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];
304
if (file_exists($fn = "$file.mo")) {
305
$this->__loadMo($fn, $domain);
306
$this->__noLocale = false;
308
} elseif (file_exists($fn = "$file.po") && ($f = fopen($fn, "r"))) {
309
$this->__loadPo($f, $domain);
310
$this->__noLocale = false;
312
} elseif (is_file($localeDef) && ($f = fopen($localeDef, "r"))) {
313
$this->__loadLocaleDefinition($f, $domain);
314
$this->__noLocale = false;
320
if (empty($this->__domains[$domain][$this->__lang][$this->category])) {
321
$this->__domains[$domain][$this->__lang][$this->category] = array();
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;
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"]);
337
$this->__domains = Set::pushDiff($this->__domains, $merge);
339
if (isset($this->__domains[$domain][$this->__lang][$this->category][null])) {
340
unset($this->__domains[$domain][$this->__lang][$this->category][null]);
347
* Loads the binary .mo file for translation and sets the values for this translation in the var I18n::__domains
349
* @param resource $file Binary .mo file to load
350
* @param string $domain Domain where to load file in
353
function __loadMo($file, $domain) {
354
$data = file_get_contents($file);
357
$header = substr($data, 0, 20);
358
$header = unpack("L1magic/L1version/L1count/L1o_msg/L1o_trn", $header);
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);
367
if (strpos($msgid, "\000")) {
368
list($msgid, $msgid_plural) = explode("\000", $msgid);
370
$r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
371
$msgstr = substr($data, $r["offs"], $r["len"]);
373
if (strpos($msgstr, "\000")) {
374
$msgstr = explode("\000", $msgstr);
376
$this->__domains[$domain][$this->__lang][$this->category][$msgid] = $msgstr;
378
if (isset($msgid_plural)) {
379
$this->__domains[$domain][$this->__lang][$this->category][$msgid_plural] =& $this->__domains[$domain][$this->__lang][$this->category][$msgid];
387
* Loads the text .po file for translation and sets the values for this translation in the var I18n::__domains
389
* @param resource $file Text .po file to load
390
* @param string $domain Domain to load file in
391
* @return array Binded domain elements
394
function __loadPo($file, $domain) {
396
$translations = array();
397
$translationKey = "";
402
$line = trim(fgets($file));
403
if ($line == "" || $line[0] == "#") {
406
if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
408
$translationKey = stripcslashes($regs[1]);
409
} elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
411
$translationKey = "";
412
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $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]);
418
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
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)) {
425
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
427
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
429
$translations[$translationKey][$plural] = stripcslashes($regs[2]);
431
} elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
433
$translations[$translationKey][$plural] = "";
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]);
440
} elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
443
} elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
444
$header .= stripcslashes($regs[1]);
446
unset($translations[$translationKey]);
448
$translationKey = "";
451
} while (!feof($file));
453
$merge[""] = $header;
454
return $this->__domains[$domain][$this->__lang][$this->category] = array_merge($merge ,$translations);
458
* Parses a locale definition file following the POSIX standard
460
* @param resource $file file handler
461
* @param string $domain Domain where locale definitions will be stored
465
function __loadLocaleDefinition($file, $domain = null) {
468
$currentToken = false;
470
while ($line = fgets($file)) {
472
if (empty($line) || $line[0] === $comment) {
475
$parts = preg_split("/[[:space:]]+/",$line);
476
if ($parts[0] === 'comment_char') {
477
$comment = $parts[1];
480
if ($parts[0] === 'escape_char') {
484
$count = count($parts);
486
$currentToken = $parts[0];
488
} elseif ($count == 1) {
494
$len = strlen($value) - 1;
495
if ($value[$len] === $escape) {
496
$value = substr($value, 0, $len);
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);
511
if (count($value) == 1) {
512
$this->__domains[$domain][$this->__lang][$this->category][$currentToken] = array_pop($value);
514
$this->__domains[$domain][$this->__lang][$this->category][$currentToken] = $value;
520
* Auxiliary function to parse a symbol from a locale definition file
522
* @param string $string Symbol to be parsed
523
* @return string parsed symbol
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)))));
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))));
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))));
540
if (substr($string, 0, 3) === 'U00') {
542
return join('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
544
if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
545
return Multibyte::ascii(array(hexdec($match[1])));
551
* Returns a Time format definition from corresponding domain
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.
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])) {