CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.5 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.5
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • I18n
  • L10n
  • Multibyte
  1: <?php
  2: /**
  3:  * Internationalization
  4:  *
  5:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7:  *
  8:  * Licensed under The MIT License
  9:  * For full copyright and license information, please see the LICENSE.txt
 10:  * Redistributions of files must retain the above copyright notice.
 11:  *
 12:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 13:  * @link          http://cakephp.org CakePHP(tm) Project
 14:  * @package       Cake.I18n
 15:  * @since         CakePHP(tm) v 1.2.0.4116
 16:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 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:  * I18n handles translation of Text and time format strings.
 26:  *
 27:  * @package       Cake.I18n
 28:  */
 29: class I18n {
 30: 
 31: /**
 32:  * Instance of the L10n class for localization
 33:  *
 34:  * @var L10n
 35:  */
 36:     public $l10n = null;
 37: 
 38: /**
 39:  * Default domain of translation
 40:  *
 41:  * @var string
 42:  */
 43:     public static $defaultDomain = 'default';
 44: 
 45: /**
 46:  * Current domain of translation
 47:  *
 48:  * @var string
 49:  */
 50:     public $domain = null;
 51: 
 52: /**
 53:  * Current category of translation
 54:  *
 55:  * @var string
 56:  */
 57:     public $category = 'LC_MESSAGES';
 58: 
 59: /**
 60:  * Current language used for translations
 61:  *
 62:  * @var string
 63:  */
 64:     protected $_lang = null;
 65: 
 66: /**
 67:  * Translation strings for a specific domain read from the .mo or .po files
 68:  *
 69:  * @var array
 70:  */
 71:     protected $_domains = array();
 72: 
 73: /**
 74:  * Set to true when I18N::_bindTextDomain() is called for the first time.
 75:  * If a translation file is found it is set to false again
 76:  *
 77:  * @var bool
 78:  */
 79:     protected $_noLocale = false;
 80: 
 81: /**
 82:  * Translation categories
 83:  *
 84:  * @var array
 85:  */
 86:     protected $_categories = array(
 87:         'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY', 'LC_NUMERIC', 'LC_TIME', 'LC_MESSAGES'
 88:     );
 89: 
 90: /**
 91:  * Constants for the translation categories.
 92:  *
 93:  * The constants may be used in translation fetching
 94:  * instead of hardcoded integers.
 95:  * Example:
 96:  * {{{
 97:  *  I18n::translate('CakePHP is awesome.', null, null, I18n::LC_MESSAGES)
 98:  * }}}
 99:  *
100:  * To keep the code more readable, I18n constants are preferred over
101:  * hardcoded integers.
102:  */
103: /**
104:  * Constant for LC_ALL.
105:  *
106:  * @var int
107:  */
108:     const LC_ALL = 0;
109: 
110: /**
111:  * Constant for LC_COLLATE.
112:  *
113:  * @var int
114:  */
115:     const LC_COLLATE = 1;
116: 
117: /**
118:  * Constant for LC_CTYPE.
119:  *
120:  * @var int
121:  */
122:     const LC_CTYPE = 2;
123: 
124: /**
125:  * Constant for LC_MONETARY.
126:  *
127:  * @var int
128:  */
129:     const LC_MONETARY = 3;
130: 
131: /**
132:  * Constant for LC_NUMERIC.
133:  *
134:  * @var int
135:  */
136:     const LC_NUMERIC = 4;
137: 
138: /**
139:  * Constant for LC_TIME.
140:  *
141:  * @var int
142:  */
143:     const LC_TIME = 5;
144: 
145: /**
146:  * Constant for LC_MESSAGES.
147:  *
148:  * @var int
149:  */
150:     const LC_MESSAGES = 6;
151: 
152: /**
153:  * Escape string
154:  *
155:  * @var string
156:  */
157:     protected $_escape = null;
158: 
159: /**
160:  * Constructor, use I18n::getInstance() to get the i18n translation object.
161:  */
162:     public function __construct() {
163:         $this->l10n = new L10n();
164:     }
165: 
166: /**
167:  * Return a static instance of the I18n class
168:  *
169:  * @return I18n
170:  */
171:     public static function getInstance() {
172:         static $instance = array();
173:         if (!$instance) {
174:             $instance[0] = new I18n();
175:         }
176:         return $instance[0];
177:     }
178: 
179: /**
180:  * Used by the translation functions in basics.php
181:  * Returns a translated string based on current language and translation files stored in locale folder
182:  *
183:  * @param string $singular String to translate
184:  * @param string $plural Plural string (if any)
185:  * @param string $domain Domain The domain of the translation. Domains are often used by plugin translations.
186:  *    If null, the default domain will be used.
187:  * @param string $category Category The integer value of the category to use.
188:  * @param int $count Count Count is used with $plural to choose the correct plural form.
189:  * @param string $language Language to translate string to.
190:  *    If null it checks for language in session followed by Config.language configuration variable.
191:  * @return string translated string.
192:  * @throws CakeException When '' is provided as a domain.
193:  */
194:     public static function translate($singular, $plural = null, $domain = null, $category = self::LC_MESSAGES, $count = null, $language = null) {
195:         $_this = I18n::getInstance();
196: 
197:         if (strpos($singular, "\r\n") !== false) {
198:             $singular = str_replace("\r\n", "\n", $singular);
199:         }
200:         if ($plural !== null && strpos($plural, "\r\n") !== false) {
201:             $plural = str_replace("\r\n", "\n", $plural);
202:         }
203: 
204:         if (is_numeric($category)) {
205:             $_this->category = $_this->_categories[$category];
206:         }
207: 
208:         if (empty($language)) {
209:             if (CakeSession::started()) {
210:                 $language = CakeSession::read('Config.language');
211:             }
212:             if (empty($language)) {
213:                 $language = Configure::read('Config.language');
214:             }
215:         }
216: 
217:         if (($_this->_lang && $_this->_lang !== $language) || !$_this->_lang) {
218:             $lang = $_this->l10n->get($language);
219:             $_this->_lang = $lang;
220:         }
221: 
222:         if ($domain === null) {
223:             $domain = self::$defaultDomain;
224:         }
225:         if ($domain === '') {
226:             throw new CakeException(__d('cake_dev', 'You cannot use "" as a domain.'));
227:         }
228: 
229:         $_this->domain = $domain . '_' . $_this->l10n->lang;
230: 
231:         if (!isset($_this->_domains[$domain][$_this->_lang])) {
232:             $_this->_domains[$domain][$_this->_lang] = Cache::read($_this->domain, '_cake_core_');
233:         }
234: 
235:         if (!isset($_this->_domains[$domain][$_this->_lang][$_this->category])) {
236:             $_this->_bindTextDomain($domain);
237:             Cache::write($_this->domain, $_this->_domains[$domain][$_this->_lang], '_cake_core_');
238:         }
239: 
240:         if ($_this->category === 'LC_TIME') {
241:             return $_this->_translateTime($singular, $domain);
242:         }
243: 
244:         if (!isset($count)) {
245:             $plurals = 0;
246:         } elseif (!empty($_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"]) && $_this->_noLocale === false) {
247:             $header = $_this->_domains[$domain][$_this->_lang][$_this->category]["%plural-c"];
248:             $plurals = $_this->_pluralGuess($header, $count);
249:         } else {
250:             if ($count != 1) {
251:                 $plurals = 1;
252:             } else {
253:                 $plurals = 0;
254:             }
255:         }
256: 
257:         if (!empty($_this->_domains[$domain][$_this->_lang][$_this->category][$singular])) {
258:             if (($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$singular]) || ($plurals) && ($trans = $_this->_domains[$domain][$_this->_lang][$_this->category][$plural])) {
259:                 if (is_array($trans)) {
260:                     if (isset($trans[$plurals])) {
261:                         $trans = $trans[$plurals];
262:                     } else {
263:                         trigger_error(
264:                             __d('cake_dev',
265:                                 'Missing plural form translation for "%s" in "%s" domain, "%s" locale. ' .
266:                                 ' Check your po file for correct plurals and valid Plural-Forms header.',
267:                                 $singular,
268:                                 $domain,
269:                                 $_this->_lang
270:                             ),
271:                             E_USER_WARNING
272:                         );
273:                         $trans = $trans[0];
274:                     }
275:                 }
276:                 if (strlen($trans)) {
277:                     return $trans;
278:                 }
279:             }
280:         }
281: 
282:         if (!empty($plurals)) {
283:             return $plural;
284:         }
285:         return $singular;
286:     }
287: 
288: /**
289:  * Clears the domains internal data array. Useful for testing i18n.
290:  *
291:  * @return void
292:  */
293:     public static function clear() {
294:         $self = I18n::getInstance();
295:         $self->_domains = array();
296:     }
297: 
298: /**
299:  * Get the loaded domains cache.
300:  *
301:  * @return array
302:  */
303:     public static function domains() {
304:         $self = I18n::getInstance();
305:         return $self->_domains;
306:     }
307: 
308: /**
309:  * Attempts to find the plural form of a string.
310:  *
311:  * @param string $header Type
312:  * @param int $n Number
313:  * @return int plural match
314:  * @link http://localization-guide.readthedocs.org/en/latest/l10n/pluralforms.html
315:  * @link https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_and_Plurals#List_of_Plural_Rules
316:  */
317:     protected function _pluralGuess($header, $n) {
318:         if (!is_string($header) || $header === "nplurals=1;plural=0;" || !isset($header[0])) {
319:             return 0;
320:         }
321: 
322:         if ($header === "nplurals=2;plural=n!=1;") {
323:             return $n != 1 ? 1 : 0;
324:         } elseif ($header === "nplurals=2;plural=n>1;") {
325:             return $n > 1 ? 1 : 0;
326:         }
327: 
328:         if (strpos($header, "plurals=3")) {
329:             if (strpos($header, "100!=11")) {
330:                 if (strpos($header, "10<=4")) {
331:                     return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
332:                 } elseif (strpos($header, "100<10")) {
333:                     return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
334:                 }
335:                 return $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n != 0 ? 1 : 2);
336:             } elseif (strpos($header, "n==2")) {
337:                 return $n == 1 ? 0 : ($n == 2 ? 1 : 2);
338:             } elseif (strpos($header, "n==0")) {
339:                 return $n == 1 ? 0 : ($n == 0 || ($n % 100 > 0 && $n % 100 < 20) ? 1 : 2);
340:             } elseif (strpos($header, "n>=2")) {
341:                 return $n == 1 ? 0 : ($n >= 2 && $n <= 4 ? 1 : 2);
342:             } elseif (strpos($header, "10>=2")) {
343:                 return $n == 1 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2);
344:             }
345:             return $n % 10 == 1 ? 0 : ($n % 10 == 2 ? 1 : 2);
346:         } elseif (strpos($header, "plurals=4")) {
347:             if (strpos($header, "100==2")) {
348:                 return $n % 100 == 1 ? 0 : ($n % 100 == 2 ? 1 : ($n % 100 == 3 || $n % 100 == 4 ? 2 : 3));
349:             } elseif (strpos($header, "n>=3")) {
350:                 return $n == 1 ? 0 : ($n == 2 ? 1 : ($n == 0 || ($n >= 3 && $n <= 10) ? 2 : 3));
351:             } elseif (strpos($header, "100>=1")) {
352:                 return $n == 1 ? 0 : ($n == 0 || ($n % 100 >= 1 && $n % 100 <= 10) ? 1 : ($n % 100 >= 11 && $n % 100 <= 20 ? 2 : 3));
353:             }
354:         } elseif (strpos($header, "plurals=5")) {
355:             return $n == 1 ? 0 : ($n == 2 ? 1 : ($n >= 3 && $n <= 6 ? 2 : ($n >= 7 && $n <= 10 ? 3 : 4)));
356:         } elseif (strpos($header, "plurals=6")) {
357:             return $n == 0 ? 0 :
358:                 ($n == 1 ? 1 :
359:                 ($n == 2 ? 2 :
360:                 ($n % 100 >= 3 && $n % 100 <= 10 ? 3 :
361:                 ($n % 100 >= 11 ? 4 : 5))));
362:         }
363: 
364:         return 0;
365:     }
366: 
367: /**
368:  * Binds the given domain to a file in the specified directory.
369:  *
370:  * @param string $domain Domain to bind
371:  * @return string Domain binded
372:  */
373:     protected function _bindTextDomain($domain) {
374:         $this->_noLocale = true;
375:         $core = true;
376:         $merge = array();
377:         $searchPaths = App::path('locales');
378:         $plugins = CakePlugin::loaded();
379: 
380:         if (!empty($plugins)) {
381:             foreach ($plugins as $plugin) {
382:                 $pluginDomain = Inflector::underscore($plugin);
383:                 if ($pluginDomain === $domain) {
384:                     $searchPaths[] = CakePlugin::path($plugin) . 'Locale' . DS;
385:                     $searchPaths = array_reverse($searchPaths);
386:                     break;
387:                 }
388:             }
389:         }
390: 
391:         foreach ($searchPaths as $directory) {
392:             foreach ($this->l10n->languagePath as $lang) {
393:                 $localeDef = $directory . $lang . DS . $this->category;
394:                 if (is_file($localeDef)) {
395:                     $definitions = self::loadLocaleDefinition($localeDef);
396:                     if ($definitions !== false) {
397:                         $this->_domains[$domain][$this->_lang][$this->category] = $definitions;
398:                         $this->_noLocale = false;
399:                         return $domain;
400:                     }
401:                 }
402: 
403:                 if ($core) {
404:                     $app = $directory . $lang . DS . $this->category . DS . 'core';
405:                     $translations = false;
406: 
407:                     if (is_file($app . '.mo')) {
408:                         $translations = self::loadMo($app . '.mo');
409:                     }
410:                     if ($translations === false && is_file($app . '.po')) {
411:                         $translations = self::loadPo($app . '.po');
412:                     }
413: 
414:                     if ($translations !== false) {
415:                         $this->_domains[$domain][$this->_lang][$this->category] = $translations;
416:                         $merge[$domain][$this->_lang][$this->category] = $this->_domains[$domain][$this->_lang][$this->category];
417:                         $this->_noLocale = false;
418:                         $core = null;
419:                     }
420:                 }
421: 
422:                 $file = $directory . $lang . DS . $this->category . DS . $domain;
423:                 $translations = false;
424: 
425:                 if (is_file($file . '.mo')) {
426:                     $translations = self::loadMo($file . '.mo');
427:                 }
428:                 if ($translations === false && is_file($file . '.po')) {
429:                     $translations = self::loadPo($file . '.po');
430:                 }
431: 
432:                 if ($translations !== false) {
433:                     $this->_domains[$domain][$this->_lang][$this->category] = $translations;
434:                     $this->_noLocale = false;
435:                     break 2;
436:                 }
437:             }
438:         }
439: 
440:         if (empty($this->_domains[$domain][$this->_lang][$this->category])) {
441:             $this->_domains[$domain][$this->_lang][$this->category] = array();
442:             return $domain;
443:         }
444: 
445:         if (isset($this->_domains[$domain][$this->_lang][$this->category][""])) {
446:             $head = $this->_domains[$domain][$this->_lang][$this->category][""];
447: 
448:             foreach (explode("\n", $head) as $line) {
449:                 $header = strtok($line, ':');
450:                 $line = trim(strtok("\n"));
451:                 $this->_domains[$domain][$this->_lang][$this->category]["%po-header"][strtolower($header)] = $line;
452:             }
453: 
454:             if (isset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"])) {
455:                 $switch = preg_replace("/(?:[() {}\\[\\]^\\s*\\]]+)/", "", $this->_domains[$domain][$this->_lang][$this->category]["%po-header"]["plural-forms"]);
456:                 $this->_domains[$domain][$this->_lang][$this->category]["%plural-c"] = $switch;
457:                 unset($this->_domains[$domain][$this->_lang][$this->category]["%po-header"]);
458:             }
459:             $this->_domains = Hash::mergeDiff($this->_domains, $merge);
460: 
461:             if (isset($this->_domains[$domain][$this->_lang][$this->category][null])) {
462:                 unset($this->_domains[$domain][$this->_lang][$this->category][null]);
463:             }
464:         }
465: 
466:         return $domain;
467:     }
468: 
469: /**
470:  * Loads the binary .mo file and returns array of translations
471:  *
472:  * @param string $filename Binary .mo file to load
473:  * @return mixed Array of translations on success or false on failure
474:  */
475:     public static function loadMo($filename) {
476:         $translations = false;
477: 
478:         // @codingStandardsIgnoreStart
479:         // Binary files extracted makes non-standard local variables
480:         if ($data = file_get_contents($filename)) {
481:             $translations = array();
482:             $header = substr($data, 0, 20);
483:             $header = unpack('L1magic/L1version/L1count/L1o_msg/L1o_trn', $header);
484:             extract($header);
485: 
486:             if ((dechex($magic) === '950412de' || dechex($magic) === 'ffffffff950412de') && !$version) {
487:                 for ($n = 0; $n < $count; $n++) {
488:                     $r = unpack("L1len/L1offs", substr($data, $o_msg + $n * 8, 8));
489:                     $msgid = substr($data, $r["offs"], $r["len"]);
490:                     unset($msgid_plural);
491: 
492:                     if (strpos($msgid, "\000")) {
493:                         list($msgid, $msgid_plural) = explode("\000", $msgid);
494:                     }
495:                     $r = unpack("L1len/L1offs", substr($data, $o_trn + $n * 8, 8));
496:                     $msgstr = substr($data, $r["offs"], $r["len"]);
497: 
498:                     if (strpos($msgstr, "\000")) {
499:                         $msgstr = explode("\000", $msgstr);
500:                     }
501:                     $translations[$msgid] = $msgstr;
502: 
503:                     if (isset($msgid_plural)) {
504:                         $translations[$msgid_plural] =& $translations[$msgid];
505:                     }
506:                 }
507:             }
508:         }
509:         // @codingStandardsIgnoreEnd
510: 
511:         return $translations;
512:     }
513: 
514: /**
515:  * Loads the text .po file and returns array of translations
516:  *
517:  * @param string $filename Text .po file to load
518:  * @return mixed Array of translations on success or false on failure
519:  */
520:     public static function loadPo($filename) {
521:         if (!$file = fopen($filename, 'r')) {
522:             return false;
523:         }
524: 
525:         $type = 0;
526:         $translations = array();
527:         $translationKey = '';
528:         $plural = 0;
529:         $header = '';
530: 
531:         do {
532:             $line = trim(fgets($file));
533:             if ($line === '' || $line[0] === '#') {
534:                 continue;
535:             }
536:             if (preg_match("/msgid[[:space:]]+\"(.+)\"$/i", $line, $regs)) {
537:                 $type = 1;
538:                 $translationKey = stripcslashes($regs[1]);
539:             } elseif (preg_match("/msgid[[:space:]]+\"\"$/i", $line, $regs)) {
540:                 $type = 2;
541:                 $translationKey = '';
542:             } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && ($type == 1 || $type == 2 || $type == 3)) {
543:                 $type = 3;
544:                 $translationKey .= stripcslashes($regs[1]);
545:             } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
546:                 $translations[$translationKey] = stripcslashes($regs[1]);
547:                 $type = 4;
548:             } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && ($type == 1 || $type == 3) && $translationKey) {
549:                 $type = 4;
550:                 $translations[$translationKey] = '';
551:             } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 4 && $translationKey) {
552:                 $translations[$translationKey] .= stripcslashes($regs[1]);
553:             } elseif (preg_match("/msgid_plural[[:space:]]+\".*\"$/i", $line, $regs)) {
554:                 $type = 6;
555:             } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 6 && $translationKey) {
556:                 $type = 6;
557:             } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"(.+)\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
558:                 $plural = $regs[1];
559:                 $translations[$translationKey][$plural] = stripcslashes($regs[2]);
560:                 $type = 7;
561:             } elseif (preg_match("/msgstr\[(\d+)\][[:space:]]+\"\"$/i", $line, $regs) && ($type == 6 || $type == 7) && $translationKey) {
562:                 $plural = $regs[1];
563:                 $translations[$translationKey][$plural] = '';
564:                 $type = 7;
565:             } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 7 && $translationKey) {
566:                 $translations[$translationKey][$plural] .= stripcslashes($regs[1]);
567:             } elseif (preg_match("/msgstr[[:space:]]+\"(.+)\"$/i", $line, $regs) && $type == 2 && !$translationKey) {
568:                 $header .= stripcslashes($regs[1]);
569:                 $type = 5;
570:             } elseif (preg_match("/msgstr[[:space:]]+\"\"$/i", $line, $regs) && !$translationKey) {
571:                 $header = '';
572:                 $type = 5;
573:             } elseif (preg_match("/^\"(.*)\"$/i", $line, $regs) && $type == 5) {
574:                 $header .= stripcslashes($regs[1]);
575:             } else {
576:                 unset($translations[$translationKey]);
577:                 $type = 0;
578:                 $translationKey = '';
579:                 $plural = 0;
580:             }
581:         } while (!feof($file));
582:         fclose($file);
583: 
584:         $merge[''] = $header;
585:         return array_merge($merge, $translations);
586:     }
587: 
588: /**
589:  * Parses a locale definition file following the POSIX standard
590:  *
591:  * @param string $filename Locale definition filename
592:  * @return mixed Array of definitions on success or false on failure
593:  */
594:     public static function loadLocaleDefinition($filename) {
595:         if (!$file = fopen($filename, 'r')) {
596:             return false;
597:         }
598: 
599:         $definitions = array();
600:         $comment = '#';
601:         $escape = '\\';
602:         $currentToken = false;
603:         $value = '';
604:         $_this = I18n::getInstance();
605:         while ($line = fgets($file)) {
606:             $line = trim($line);
607:             if (empty($line) || $line[0] === $comment) {
608:                 continue;
609:             }
610:             $parts = preg_split("/[[:space:]]+/", $line);
611:             if ($parts[0] === 'comment_char') {
612:                 $comment = $parts[1];
613:                 continue;
614:             }
615:             if ($parts[0] === 'escape_char') {
616:                 $escape = $parts[1];
617:                 continue;
618:             }
619:             $count = count($parts);
620:             if ($count === 2) {
621:                 $currentToken = $parts[0];
622:                 $value = $parts[1];
623:             } elseif ($count === 1) {
624:                 $value = is_array($value) ? $parts[0] : $value . $parts[0];
625:             } else {
626:                 continue;
627:             }
628: 
629:             $len = strlen($value) - 1;
630:             if ($value[$len] === $escape) {
631:                 $value = substr($value, 0, $len);
632:                 continue;
633:             }
634: 
635:             $mustEscape = array($escape . ',', $escape . ';', $escape . '<', $escape . '>', $escape . $escape);
636:             $replacements = array_map('crc32', $mustEscape);
637:             $value = str_replace($mustEscape, $replacements, $value);
638:             $value = explode(';', $value);
639:             $_this->_escape = $escape;
640:             foreach ($value as $i => $val) {
641:                 $val = trim($val, '"');
642:                 $val = preg_replace_callback('/(?:<)?(.[^>]*)(?:>)?/', array(&$_this, '_parseLiteralValue'), $val);
643:                 $val = str_replace($replacements, $mustEscape, $val);
644:                 $value[$i] = $val;
645:             }
646:             if (count($value) === 1) {
647:                 $definitions[$currentToken] = array_pop($value);
648:             } else {
649:                 $definitions[$currentToken] = $value;
650:             }
651:         }
652: 
653:         return $definitions;
654:     }
655: 
656: /**
657:  * Auxiliary function to parse a symbol from a locale definition file
658:  *
659:  * @param string $string Symbol to be parsed
660:  * @return string parsed symbol
661:  */
662:     protected function _parseLiteralValue($string) {
663:         $string = $string[1];
664:         if (substr($string, 0, 2) === $this->_escape . 'x') {
665:             $delimiter = $this->_escape . 'x';
666:             return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
667:         }
668:         if (substr($string, 0, 2) === $this->_escape . 'd') {
669:             $delimiter = $this->_escape . 'd';
670:             return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
671:         }
672:         if ($string[0] === $this->_escape && isset($string[1]) && is_numeric($string[1])) {
673:             $delimiter = $this->_escape;
674:             return implode('', array_map('chr', array_filter(explode($delimiter, $string))));
675:         }
676:         if (substr($string, 0, 3) === 'U00') {
677:             $delimiter = 'U00';
678:             return implode('', array_map('chr', array_map('hexdec', array_filter(explode($delimiter, $string)))));
679:         }
680:         if (preg_match('/U([0-9a-fA-F]{4})/', $string, $match)) {
681:             return Multibyte::ascii(array(hexdec($match[1])));
682:         }
683:         return $string;
684:     }
685: 
686: /**
687:  * Returns a Time format definition from corresponding domain
688:  *
689:  * @param string $format Format to be translated
690:  * @param string $domain Domain where format is stored
691:  * @return mixed translated format string if only value or array of translated strings for corresponding format.
692:  */
693:     protected function _translateTime($format, $domain) {
694:         if (!empty($this->_domains[$domain][$this->_lang]['LC_TIME'][$format])) {
695:             if (($trans = $this->_domains[$domain][$this->_lang][$this->category][$format])) {
696:                 return $trans;
697:             }
698:         }
699:         return $format;
700:     }
701: 
702: }
703: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs