1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 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: 26: 27: 28:
29: class I18n {
30:
31: 32: 33: 34: 35:
36: public $l10n = null;
37:
38: 39: 40: 41: 42:
43: public static $defaultDomain = 'default';
44:
45: 46: 47: 48: 49:
50: public $domain = null;
51:
52: 53: 54: 55: 56:
57: public $category = 'LC_MESSAGES';
58:
59: 60: 61: 62: 63:
64: protected $_lang = null;
65:
66: 67: 68: 69: 70:
71: protected $_domains = array();
72:
73: 74: 75: 76: 77: 78:
79: protected $_noLocale = false;
80:
81: 82: 83: 84: 85:
86: protected $_categories = array(
87: 'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
88: );
89:
90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102:
103: 104: 105: 106: 107:
108: const LC_ALL = 0;
109:
110: 111: 112: 113: 114:
115: const LC_COLLATE = 1;
116:
117: 118: 119: 120: 121:
122: const LC_CTYPE = 2;
123:
124: 125: 126: 127: 128:
129: const LC_MONETARY = 3;
130:
131: 132: 133: 134: 135:
136: const LC_NUMERIC = 4;
137:
138: 139: 140: 141: 142:
143: const LC_TIME = 5;
144:
145: 146: 147: 148: 149:
150: const LC_MESSAGES = 6;
151:
152: 153: 154: 155: 156:
157: protected $_escape = null;
158:
159: 160: 161:
162: public function __construct() {
163: $this->l10n = new L10n();
164: }
165:
166: 167: 168: 169: 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: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 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: 290: 291: 292:
293: public static function clear() {
294: $self = I18n::getInstance();
295: $self->_domains = array();
296: }
297:
298: 299: 300: 301: 302:
303: public static function domains() {
304: $self = I18n::getInstance();
305: return $self->_domains;
306: }
307:
308: 309: 310: 311: 312: 313: 314: 315: 316:
317: protected function _pluralGuess($header, $n) {
318: if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
319: return 0;
320: }
321:
322: if ($header === "nplurals=2;plural=n!=1;") {
323: return $n != 1 ? 1 : 0;
324: } elseif ($header === "nplurals=2;plural=n>1;") {
325: return $n > 1 ? 1 : 0;
326: }
327:
328: if (strpos($header, "plurals=3")) {
329: if (strpos($header, "100!=11")) {
330: if (strpos($header, "10<=4")) {
331: return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
332: } elseif (strpos($header, "100<10")) {
333: return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
334: }
335: return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
336: } elseif (strpos($header, "n==2")) {
337: return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
338: } elseif (strpos($header, "n==0")) {
339: return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
340: } elseif (strpos($header, "n>=2")) {
341: return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
342: } elseif (strpos($header, "10>=2")) {
343: return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
344: }
345: return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
346: } elseif (strpos($header, "plurals=4")) {
347: if (strpos($header, "100==2")) {
348: return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
349: } elseif (strpos($header, "n>=3")) {
350: return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
351: } elseif (strpos($header, "100>=1")) {
352: return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
353: }
354: } elseif (strpos($header, "plurals=5")) {
355: return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
356: } elseif (strpos($header, "plurals=6")) {
357: return $n == 0 ? 0 :
358: ($n == 1 ? 1 :
359: ($n == 2 ? 2 :
360: ($n % 100 >= 3 && $n % 100 <= 10 ? 3 :
361: ($n % 100 >= 11 ? 4 : 5))));
362: }
363:
364: return 0;
365: }
366:
367: 368: 369: 370: 371: 372:
373: protected function _bindTextDomain($domain) {
374: $this->_noLocale = true;
375: $core = true;
376: $merge = array();
377: $searchPaths = App::path('locales');
378: $plugins = CakePlugin::loaded();
379:
380: if (!empty($plugins)) {
381: foreach ($plugins as $plugin) {
382: $pluginDomain = Inflector::underscore($plugin);
383: if ($pluginDomain === $domain) {
384: $searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
385: $searchPaths = array_reverse($searchPaths);
386: break;
387: }
388: }
389: }
390:
391: foreach ($searchPaths as $directory) {
392: foreach ($this->l10n->languagePath as $lang) {
393: $localeDef = $directory . $lang . DS . $this->category;
394: if (is_file($localeDef)) {
395: $definitions = self::loadLocaleDefinition($localeDef);
396: if ($definitions !== false) {
397: $this->_domains[$domain][$this->_lang][$this->category] = $definitions;
398: $this->_noLocale = false;
399: return $domain;
400: }
401: }
402:
403: if ($core) {
404: $app = $directory . $lang . DS . $this->category . DS . 'core';
405: $translations = false;
406:
407: if (is_file($app . '.mo')) {
408: $translations = self::loadMo($app . '.mo');
409: }
410: if ($translations === false && is_file($app . '.po')) {
411: $translations = self::loadPo($app . '.po');
412: }
413:
414: if ($translations !== false) {
415: $this->_domains[$domain][$this->_lang][$this->category] = $translations;
416: $merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
417: $this->_noLocale = false;
418: $core = null;
419: }
420: }
421:
422: $file = $directory . $lang . DS . $this->category . DS . $domain;
423: $translations = false;
424:
425: if (is_file($file . '.mo')) {
426: $translations = self::loadMo($file . '.mo');
427: }
428: if ($translations === false && is_file($file . '.po')) {
429: $translations = self::loadPo($file . '.po');
430: }
431:
432: if ($translations !== false) {
433: $this->_domains[$domain][$this->_lang][$this->category] = $translations;
434: $this->_noLocale = false;
435: break 2;
436: }
437: }
438: }
439:
440: if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
441: $this->_domains[$domain][$this->_lang][$this->category] = array();
442: return $domain;
443: }
444:
445: if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
446: $head = $this->_domains[$domain][$this->_lang][$this->category][""];
447:
448: foreach (explode("\n", $head) as $line) {
449: $header = strtok($line, ':');
450: $line = trim(strtok("\n"));
451: $this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
452: }
453:
454: if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) {
455: $switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]);
456: $this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
457: unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
458: }
459: $this->_domains = Hash::mergeDiff($this->_domains, $merge);
460:
461: if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
462: unset($this->_domains[$domain][$this->_lang][$this->category][null]);
463: }
464: }
465:
466: return $domain;
467: }
468:
469: 470: 471: 472: 473: 474:
475: public static function loadMo($filename) {
476: $translations = false;
477:
478:
479:
480: if ($data = file_get_contents($filename)) {
481: $translations = array();
482: $header = substr($data, 0, 20);
483: $header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
484: extract($header);
485:
486: if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
487: for ($n = 0; $n < $count; $n++) {
488: $r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
489: $msgid = substr($data, $r["offs"], $r["len"]);
490: unset($msgid_plural);
491:
492: if (strpos($msgid, "\000")) {
493: list($msgid, $msgid_plural) = explode("\000", $msgid);
494: }
495: $r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
496: $msgstr = substr($data, $r["offs"], $r["len"]);
497:
498: if (strpos($msgstr, "\000")) {
499: $msgstr = explode("\000", $msgstr);
500: }
501: $translations[$msgid] = $msgstr;
502:
503: if (isset($msgid_plural)) {
504: $translations[$msgid_plural] =& $translations[$msgid];
505: }
506: }
507: }
508: }
509:
510:
511: return $translations;
512: }
513:
514: 515: 516: 517: 518: 519:
520: public static function loadPo($filename) {
521: if (!$file = fopen($filename, 'r')) {
522: return false;
523: }
524:
525: $type = 0;
526: $translations = array();
527: $translationKey = '';
528: $plural = 0;
529: $header = '';
530:
531: do {
532: $line = trim(fgets($file));
533: if ($line === '' || $line[0] === '#') {
534: continue;
535: }
536: if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
537: $type = 1;
538: $translationKey = stripcslashes($regs[1]);
539: } elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
540: $type = 2;
541: $translationKey = '';
542: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
543: $type = 3;
544: $translationKey .= stripcslashes($regs[1]);
545: } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
546: $translations[$translationKey] = stripcslashes($regs[1]);
547: $type = 4;
548: } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
549: $type = 4;
550: $translations[$translationKey] = '';
551: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
552: $translations[$translationKey] .= stripcslashes($regs[1]);
553: } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
554: $type = 6;
555: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
556: $type = 6;
557: } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
558: $plural = $regs[1];
559: $translations[$translationKey][$plural] = stripcslashes($regs[2]);
560: $type = 7;
561: } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
562: $plural = $regs[1];
563: $translations[$translationKey][$plural] = '';
564: $type = 7;
565: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
566: $translations[$translationKey][$plural] .= stripcslashes($regs[1]);
567: } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
568: $header .= stripcslashes($regs[1]);
569: $type = 5;
570: } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
571: $header = '';
572: $type = 5;
573: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
574: $header .= stripcslashes($regs[1]);
575: } else {
576: unset($translations[$translationKey]);
577: $type = 0;
578: $translationKey = '';
579: $plural = 0;
580: }
581: } while (!feof($file));
582: fclose($file);
583:
584: $merge[''] = $header;
585: return array_merge($merge, $translations);
586: }
587:
588: 589: 590: 591: 592: 593:
594: public static function loadLocaleDefinition($filename) {
595: if (!$file = fopen($filename, 'r')) {
596: return false;
597: }
598:
599: $definitions = array();
600: $comment = '#';
601: $escape = '\\';
602: $currentToken = false;
603: $value = '';
604: $_this = I18n::getInstance();
605: while ($line = fgets($file)) {
606: $line = trim($line);
607: if (empty($line) || $line[0] === $comment) {
608: continue;
609: }
610: $parts = preg_split("/[[:space:]]+/", $line);
611: if ($parts[0] === 'comment_char') {
612: $comment = $parts[1];
613: continue;
614: }
615: if ($parts[0] === 'escape_char') {
616: $escape = $parts[1];
617: continue;
618: }
619: $count = count($parts);
620: if ($count === 2) {
621: $currentToken = $parts[0];
622: $value = $parts[1];
623: } elseif ($count === 1) {
624: $value = is_array($value) ? $parts[0] : $value . $parts[0];
625: } else {
626: continue;
627: }
628:
629: $len = strlen($value) - 1;
630: if ($value[$len] === $escape) {
631: $value = substr($value, 0, $len);
632: continue;
633: }
634:
635: $mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
636: $replacements = array_map('crc32', $mustEscape);
637: $value = str_replace($mustEscape, $replacements, $value);
638: $value = explode(';', $value);
639: $_this->_escape = $escape;
640: foreach ($value as $i => $val) {
641: $val = trim($val, '"');
642: $val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
643: $val = str_replace($replacements, $mustEscape, $val);
644: $value[$i] = $val;
645: }
646: if (count($value) === 1) {
647: $definitions[$currentToken] = array_pop($value);
648: } else {
649: $definitions[$currentToken] = $value;
650: }
651: }
652:
653: return $definitions;
654: }
655:
656: 657: 658: 659: 660: 661:
662: protected function _parseLiteralValue($string) {
663: $string = $string[1];
664: if (substr($string, 0, 2) === $this->_escape . 'x') {
665: $delimiter = $this->_escape . 'x';
666: return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
667: }
668: if (substr($string, 0, 2) === $this->_escape . 'd') {
669: $delimiter = $this->_escape . 'd';
670: return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
671: }
672: if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
673: $delimiter = $this->_escape;
674: return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
675: }
676: if (substr($string, 0, 3) === 'U00') {
677: $delimiter = 'U00';
678: return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
679: }
680: if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
681: return Multibyte::ascii(array(hexdec($match[1])));
682: }
683: return $string;
684: }
685:
686: 687: 688: 689: 690: 691: 692:
693: protected function _translateTime($format, $domain) {
694: if (!empty($this->_domains[$domain][$this->_lang]['LC_TIME'][$format])) {
695: if (($trans = $this->_domains[$domain][$this->_lang][$this->category][$format])) {
696: return $trans;
697: }
698: }
699: return $format;
700: }
701:
702: }
703: