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