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