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: protected $_escape = null;
96:
97: 98: 99:
100: public function __construct() {
101: $this->l10n = new L10n();
102: }
103:
104: 105: 106: 107: 108:
109: public static function getInstance() {
110: static $instance = array();
111: if (!$instance) {
112: $instance[0] = new I18n();
113: }
114: return $instance[0];
115: }
116:
117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131:
132: public static function translate($singular, $plural = null, $domain = null, $category = 6, $count = null, $language = null) {
133: $_this = I18n::getInstance();
134:
135: if (strpos($singular, "\r\n") !== false) {
136: $singular = str_replace("\r\n", "\n", $singular);
137: }
138: if ($plural !== null && strpos($plural, "\r\n") !== false) {
139: $plural = str_replace("\r\n", "\n", $plural);
140: }
141:
142: if (is_numeric($category)) {
143: $_this->category = $_this->_categories[$category];
144: }
145:
146: if (empty($language)) {
147: if (CakeSession::started()) {
148: $language = CakeSession::read('Config.language');
149: }
150: if (empty($language)) {
151: $language = Configure::read('Config.language');
152: }
153: }
154:
155: if (($_this->_lang && $_this->_lang !== $language) || !$_this->_lang) {
156: $lang = $_this->l10n->get($language);
157: $_this->_lang = $lang;
158: }
159:
160: if ($domain === null) {
161: $domain = self::$defaultDomain;
162: }
163: if ($domain === '') {
164: throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
165: }
166:
167: $_this->domain = $domain . '_' . $_this->l10n->lang;
168:
169: if (!isset($_this->_domains[$domain][$_this->_lang])) {
170: $_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
171: }
172:
173: if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
174: $_this->_bindTextDomain($domain);
175: Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
176: }
177:
178: if ($_this->category === 'LC_TIME') {
179: return $_this->_translateTime($singular, $domain);
180: }
181:
182: if (!isset($count)) {
183: $plurals = 0;
184: } elseif (!empty($_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"]) && $_this->_noLocale === false) {
185: $header = $_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"];
186: $plurals = $_this->_pluralGuess($header, $count);
187: } else {
188: if ($count != 1) {
189: $plurals = 1;
190: } else {
191: $plurals = 0;
192: }
193: }
194:
195: if (!empty($_this->_domains[$domain][$_this->_lang][$_this->category][$singular])) {
196: if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular]) || ($plurals) && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural])) {
197: if (is_array($trans)) {
198: if (isset($trans[$plurals])) {
199: $trans = $trans[$plurals];
200: } else {
201: trigger_error(
202: __d('cake_dev',
203: 'Missing plural form translation for "%s" in "%s" domain, "%s" locale. ' .
204: ' Check your po file for correct plurals and valid Plural-Forms header.',
205: $singular,
206: $domain,
207: $_this->_lang
208: ),
209: E_USER_WARNING
210: );
211: $trans = $trans[0];
212: }
213: }
214: if (strlen($trans)) {
215: return $trans;
216: }
217: }
218: }
219:
220: if (!empty($plurals)) {
221: return $plural;
222: }
223: return $singular;
224: }
225:
226: 227: 228: 229: 230:
231: public static function clear() {
232: $self = I18n::getInstance();
233: $self->_domains = array();
234: }
235:
236: 237: 238: 239: 240:
241: public static function domains() {
242: $self = I18n::getInstance();
243: return $self->_domains;
244: }
245:
246: 247: 248: 249: 250: 251: 252:
253: protected function _pluralGuess($header, $n) {
254: if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
255: return 0;
256: }
257:
258: if ($header === "nplurals=2;plural=n!=1;") {
259: return $n != 1 ? 1 : 0;
260: } elseif ($header === "nplurals=2;plural=n>1;") {
261: return $n > 1 ? 1 : 0;
262: }
263:
264: if (strpos($header, "plurals=3")) {
265: if (strpos($header, "100!=11")) {
266: if (strpos($header, "10<=4")) {
267: return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
268: } elseif (strpos($header, "100<10")) {
269: return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
270: }
271: return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
272: } elseif (strpos($header, "n==2")) {
273: return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
274: } elseif (strpos($header, "n==0")) {
275: return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
276: } elseif (strpos($header, "n>=2")) {
277: return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
278: } elseif (strpos($header, "10>=2")) {
279: return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
280: }
281: return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
282: } elseif (strpos($header, "plurals=4")) {
283: if (strpos($header, "100==2")) {
284: return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
285: } elseif (strpos($header, "n>=3")) {
286: return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
287: } elseif (strpos($header, "100>=1")) {
288: return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
289: }
290: } elseif (strpos($header, "plurals=5")) {
291: return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
292: }
293: }
294:
295: 296: 297: 298: 299: 300:
301: protected function _bindTextDomain($domain) {
302: $this->_noLocale = true;
303: $core = true;
304: $merge = array();
305: $searchPaths = App::path('locales');
306: $plugins = CakePlugin::loaded();
307:
308: if (!empty($plugins)) {
309: foreach ($plugins as $plugin) {
310: $pluginDomain = Inflector::underscore($plugin);
311: if ($pluginDomain === $domain) {
312: $searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
313: $searchPaths = array_reverse($searchPaths);
314: break;
315: }
316: }
317: }
318:
319: foreach ($searchPaths as $directory) {
320: foreach ($this->l10n->languagePath as $lang) {
321: $localeDef = $directory . $lang . DS . $this->category;
322: if (is_file($localeDef)) {
323: $definitions = self::loadLocaleDefinition($localeDef);
324: if ($definitions !== false) {
325: $this->_domains[$domain][$this->_lang][$this->category] = $definitions;
326: $this->_noLocale = false;
327: return $domain;
328: }
329: }
330:
331: if ($core) {
332: $app = $directory . $lang . DS . $this->category . DS . 'core';
333: $translations = false;
334:
335: if (is_file($app . '.mo')) {
336: $translations = self::loadMo($app . '.mo');
337: }
338: if ($translations === false && is_file($app . '.po')) {
339: $translations = self::loadPo($app . '.po');
340: }
341:
342: if ($translations !== false) {
343: $this->_domains[$domain][$this->_lang][$this->category] = $translations;
344: $merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
345: $this->_noLocale = false;
346: $core = null;
347: }
348: }
349:
350: $file = $directory . $lang . DS . $this->category . DS . $domain;
351: $translations = false;
352:
353: if (is_file($file . '.mo')) {
354: $translations = self::loadMo($file . '.mo');
355: }
356: if ($translations === false && is_file($file . '.po')) {
357: $translations = self::loadPo($file . '.po');
358: }
359:
360: if ($translations !== false) {
361: $this->_domains[$domain][$this->_lang][$this->category] = $translations;
362: $this->_noLocale = false;
363: break 2;
364: }
365: }
366: }
367:
368: if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
369: $this->_domains[$domain][$this->_lang][$this->category] = array();
370: return $domain;
371: }
372:
373: if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
374: $head = $this->_domains[$domain][$this->_lang][$this->category][""];
375:
376: foreach (explode("\n", $head) as $line) {
377: $header = strtok($line, ':');
378: $line = trim(strtok("\n"));
379: $this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
380: }
381:
382: if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) {
383: $switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]);
384: $this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
385: unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
386: }
387: $this->_domains = Hash::mergeDiff($this->_domains, $merge);
388:
389: if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
390: unset($this->_domains[$domain][$this->_lang][$this->category][null]);
391: }
392: }
393:
394: return $domain;
395: }
396:
397: 398: 399: 400: 401: 402:
403: public static function loadMo($filename) {
404: $translations = false;
405:
406:
407:
408: if ($data = file_get_contents($filename)) {
409: $translations = array();
410: $header = substr($data, 0, 20);
411: $header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
412: extract($header);
413:
414: if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
415: for ($n = 0; $n < $count; $n++) {
416: $r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
417: $msgid = substr($data, $r["offs"], $r["len"]);
418: unset($msgid_plural);
419:
420: if (strpos($msgid, "\000")) {
421: list($msgid, $msgid_plural) = explode("\000", $msgid);
422: }
423: $r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
424: $msgstr = substr($data, $r["offs"], $r["len"]);
425:
426: if (strpos($msgstr, "\000")) {
427: $msgstr = explode("\000", $msgstr);
428: }
429: $translations[$msgid] = $msgstr;
430:
431: if (isset($msgid_plural)) {
432: $translations[$msgid_plural] =& $translations[$msgid];
433: }
434: }
435: }
436: }
437:
438:
439: return $translations;
440: }
441:
442: 443: 444: 445: 446: 447:
448: public static function loadPo($filename) {
449: if (!$file = fopen($filename, 'r')) {
450: return false;
451: }
452:
453: $type = 0;
454: $translations = array();
455: $translationKey = '';
456: $plural = 0;
457: $header = '';
458:
459: do {
460: $line = trim(fgets($file));
461: if ($line === '' || $line[0] === '#') {
462: continue;
463: }
464: if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
465: $type = 1;
466: $translationKey = stripcslashes($regs[1]);
467: } elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
468: $type = 2;
469: $translationKey = '';
470: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
471: $type = 3;
472: $translationKey .= stripcslashes($regs[1]);
473: } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
474: $translations[$translationKey] = stripcslashes($regs[1]);
475: $type = 4;
476: } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
477: $type = 4;
478: $translations[$translationKey] = '';
479: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
480: $translations[$translationKey] .= stripcslashes($regs[1]);
481: } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
482: $type = 6;
483: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
484: $type = 6;
485: } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
486: $plural = $regs[1];
487: $translations[$translationKey][$plural] = stripcslashes($regs[2]);
488: $type = 7;
489: } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
490: $plural = $regs[1];
491: $translations[$translationKey][$plural] = '';
492: $type = 7;
493: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
494: $translations[$translationKey][$plural] .= stripcslashes($regs[1]);
495: } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
496: $header .= stripcslashes($regs[1]);
497: $type = 5;
498: } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
499: $header = '';
500: $type = 5;
501: } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
502: $header .= stripcslashes($regs[1]);
503: } else {
504: unset($translations[$translationKey]);
505: $type = 0;
506: $translationKey = '';
507: $plural = 0;
508: }
509: } while (!feof($file));
510: fclose($file);
511:
512: $merge[''] = $header;
513: return array_merge($merge, $translations);
514: }
515:
516: 517: 518: 519: 520: 521:
522: public static function loadLocaleDefinition($filename) {
523: if (!$file = fopen($filename, 'r')) {
524: return false;
525: }
526:
527: $definitions = array();
528: $comment = '#';
529: $escape = '\\';
530: $currentToken = false;
531: $value = '';
532: $_this = I18n::getInstance();
533: while ($line = fgets($file)) {
534: $line = trim($line);
535: if (empty($line) || $line[0] === $comment) {
536: continue;
537: }
538: $parts = preg_split("/[[:space:]]+/", $line);
539: if ($parts[0] === 'comment_char') {
540: $comment = $parts[1];
541: continue;
542: }
543: if ($parts[0] === 'escape_char') {
544: $escape = $parts[1];
545: continue;
546: }
547: $count = count($parts);
548: if ($count === 2) {
549: $currentToken = $parts[0];
550: $value = $parts[1];
551: } elseif ($count === 1) {
552: $value = is_array($value) ? $parts[0] : $value . $parts[0];
553: } else {
554: continue;
555: }
556:
557: $len = strlen($value) - 1;
558: if ($value[$len] === $escape) {
559: $value = substr($value, 0, $len);
560: continue;
561: }
562:
563: $mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
564: $replacements = array_map('crc32', $mustEscape);
565: $value = str_replace($mustEscape, $replacements, $value);
566: $value = explode(';', $value);
567: $_this->_escape = $escape;
568: foreach ($value as $i => $val) {
569: $val = trim($val, '"');
570: $val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
571: $val = str_replace($replacements, $mustEscape, $val);
572: $value[$i] = $val;
573: }
574: if (count($value) === 1) {
575: $definitions[$currentToken] = array_pop($value);
576: } else {
577: $definitions[$currentToken] = $value;
578: }
579: }
580:
581: return $definitions;
582: }
583:
584: 585: 586: 587: 588: 589:
590: protected function _parseLiteralValue($string) {
591: $string = $string[1];
592: if (substr($string, 0, 2) === $this->_escape . 'x') {
593: $delimiter = $this->_escape . 'x';
594: return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
595: }
596: if (substr($string, 0, 2) === $this->_escape . 'd') {
597: $delimiter = $this->_escape . 'd';
598: return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
599: }
600: if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
601: $delimiter = $this->_escape;
602: return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
603: }
604: if (substr($string, 0, 3) === 'U00') {
605: $delimiter = 'U00';
606: return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
607: }
608: if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
609: return Multibyte::ascii(array(hexdec($match[1])));
610: }
611: return $string;
612: }
613:
614: 615: 616: 617: 618: 619: 620:
621: protected function _translateTime($format, $domain) {
622: if (!empty($this->_domains[$domain][$this->_lang]['LC_TIME'][$format])) {
623: if (($trans = $this->_domains[$domain][$this->_lang][$this->category][$format])) {
624: return $trans;
625: }
626: }
627: return $format;
628: }
629:
630: }
631: