From 8bc4f0336e21fc3c45b6edf74316e7610c0d6ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Mon, 16 Nov 2009 19:30:10 -0430 Subject: [PATCH] Beginings of a parser of locale definition file --- cake/libs/i18n.php | 73 +++++++++++++++++++++++++++ cake/tests/cases/libs/i18n.test.php | 6 +++ cake/tests/test_app/locale/po/LC_TIME | 60 ++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 cake/tests/test_app/locale/po/LC_TIME diff --git a/cake/libs/i18n.php b/cake/libs/i18n.php index 9ffa9b6010b..9914df729b2 100644 --- a/cake/libs/i18n.php +++ b/cake/libs/i18n.php @@ -443,6 +443,79 @@ function __loadPo($file, $domain) { return $this->__domains[$this->category][$this->__lang][$domain] = array_merge($merge ,$translations); } + function __loadLocaleDefinition($file, $domain = null) { + $_this =& I18N::getInstance(); + $handler = fopen($file,'r'); + $comment = '#'; + $escape = '\\'; + $currentToken = false; + $value = ''; + while ($line = fgets($handler)) { + $line = trim($line); + if (empty($line) || $line[0] === $comment) { + continue; + } + $parts = preg_split("/[[:space:]]+/",$line); + if ($parts[0] === 'comment_char') { + $comment = $parts[1]; + continue; + } + if ($parts[0] === 'escape_char') { + $escape = $parts[1]; + continue; + } + $count = count($parts); + if ($count == 2) { + $currentToken = $parts[0]; + $value = $parts[1]; + } elseif ($count == 1) { + $value .= $parts[0]; + } else { + continue; + } + + $len = strlen($value) - 1; + if ($value[$len] === $escape) { + $value = substr($value,0,$len); + continue; + } + + $mustEscape = array($escape.',',$escape.';',$escape.'<',$escape.'>',$escape.$escape); + $replacements = array_map('crc32',$mustEscape); + $value = str_replace($mustEscape,$replacements,$value); + $value = explode(';',$value); + $_this->__escape = $escape; + foreach ($value as $i => $val) { + $val = trim($val,'"'); + $val = preg_replace_callback('/(<)?(?P.[^>]*)(>)?/',array(&$this,'__parseLiteralValue'),$val); + $val = str_replace($replacements,$mustEscape,$val); + $value[$i] = $val; + } + if (count($value) == 1) { + $this->__domains[$this->category][$this->__lang][$domain][$currentToken] = array_pop($value); + } else { + $this->__domains[$this->category][$this->__lang][$domain][$currentToken] = $value; + } + } + debug($this->__domains[$this->category][$this->__lang][$domain],true); + } + + function __parseLiteralValue($string) { + $string = $string['literal']; + switch (substr($string,0,2)) { + case $this->__escape . 'x': + $string = chr(hexdec(substr($string,-2))); + break; + case $this->__escape . 'd': + + break; + } + if (substr($string,0,3) === 'U00') { + $string = chr(hexdec(substr($string,-2))); + } + return $string; + } + /** * Object destructor * diff --git a/cake/tests/cases/libs/i18n.test.php b/cake/tests/cases/libs/i18n.test.php index 589c4c3fab8..6870037775c 100644 --- a/cake/tests/cases/libs/i18n.test.php +++ b/cake/tests/cases/libs/i18n.test.php @@ -2589,6 +2589,12 @@ function testCategoryThenSingular() { $this->assertEqual('Po (translated)', $singular); } + function testTimeDefinition() { + $I18n = I18n::getInstance(); + $file = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale' . DS . 'po' . DS .'LC_TIME'; + $I18n->__loadLocaleDefinition($file); + } + /** * Singular method * diff --git a/cake/tests/test_app/locale/po/LC_TIME b/cake/tests/test_app/locale/po/LC_TIME new file mode 100644 index 00000000000..8caceb8c939 --- /dev/null +++ b/cake/tests/test_app/locale/po/LC_TIME @@ -0,0 +1,60 @@ +escape_char / +comment_char % +abday "";"";/ + "";"";/ + "";"";/ + "" +day "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "" + +week 7;19971130;7 +first_weekday 1 +first_workday 2 +abmon "";"";/ + "";"";/ + "";"";/ + "";"";/ + "";"";/ + "";"" +mon "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "";/ + "" +% Appropriate date and time representation (%c) +% "%a %d %b %Y %r %Z" +d_t_fmt "" +% +% Appropriate date representation (%x) +% "%m/%d/%Y" +d_fmt "" +% +% Appropriate time representation (%X) +% "%r" +t_fmt "" +% +% Appropriate AM/PM time representation (%r) +% "%I:%M:%S %p" +t_fmt_ampm "/ +" +% +% Strings for AM/PM +% +am_pm "";"" +% +% Appropriate date representation (date(1)) "%a %b %e %H:%M:%S %Z %Y" +date_fmt "/ +/ +"