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:
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: 295: 296: 297:
298: public static function clear() {
299: $self = I18n::getInstance();
300: $self->_domains = array();
301: }
302:
303: 304: 305: 306: 307:
308: public static function domains() {
309: $self = I18n::getInstance();
310: return $self->_domains;
311: }
312:
313: 314: 315: 316: 317: 318: 319: 320: 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: 374: 375: 376: 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: 478: 479: 480: 481:
482: public static function loadMo($filename) {
483: $translations = false;
484:
485:
486:
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:
522:
523: return $translations;
524: }
525:
526: 527: 528: 529: 530: 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: 608: 609: 610: 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: 676: 677: 678: 679: 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: 697: 698: 699: 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: 727: 728: 729: 730: 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: }
742: