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