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