Permalink
Browse files

Beginings of a parser of locale definition file

  • Loading branch information...
1 parent aa9258c commit 8bc4f0336e21fc3c45b6edf74316e7610c0d6ad1 @lorenzo lorenzo committed Nov 17, 2009
Showing with 139 additions and 0 deletions.
  1. +73 −0 cake/libs/i18n.php
  2. +6 −0 cake/tests/cases/libs/i18n.test.php
  3. +60 −0 cake/tests/test_app/locale/po/LC_TIME
View
@@ -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<literal>.[^>]*)(>)?/',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
*
@@ -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
*
@@ -0,0 +1,60 @@
+escape_char /
+comment_char %
+abday "<U0053><U0075><U006E></x6e>";"<U004D><U006F><U006E>";/
+ "<U0054><U0075><U0065>";"<U0057><U0065><U0064>";/
+ "<U0054><U0068><U0075>";"<U0046><U0072><U0069>";/
+ "<U0053><U0061><U0074>"
+day "<U0053><U0075><U006E><U0064><U0061><U0079>";/
+ "<U004D><U006F><U006E><U0064><U0061><U0079>";/
+ "<U0054><U0075><U0065><U0073><U0064><U0061><U0079>";/
+ "<U0057><U0065><U0064><U006E><U0065><U0073><U0064><U0061><U0079>";/
+ "<U0054><U0068><U0075><U0072><U0073><U0064><U0061><U0079>";/
+ "<U0046><U0072><U0069><U0064><U0061><U0079>";/
+ "<U0053><U0061><U0074><U0075><U0072><U0064><U0061><U0079>"
+
+week 7;19971130;7
+first_weekday 1
+first_workday 2
+abmon "<U004A><U0061><U006E>";"<U0046><U0065><U0062>";/
+ "<U004D><U0061><U0072>";"<U0041><U0070><U0072>";/
+ "<U004D><U0061><U0079>";"<U004A><U0075><U006E>";/
+ "<U004A><U0075><U006C>";"<U0041><U0075><U0067>";/
+ "<U0053><U0065><U0070>";"<U004F><U0063><U0074>";/
+ "<U004E><U006F><U0076>";"<U0044><U0065><U0063>"
+mon "<U004A><U0061><U006E><U0075><U0061><U0072><U0079>";/
+ "<U0046><U0065><U0062><U0072><U0075><U0061><U0072><U0079>";/
+ "<U004D><U0061><U0072><U0063><U0068>";/
+ "<U0041><U0070><U0072><U0069><U006C>";/
+ "<U004D><U0061><U0079>";/
+ "<U004A><U0075><U006E><U0065>";/
+ "<U004A><U0075><U006C><U0079>";/
+ "<U0041><U0075><U0067><U0075><U0073><U0074>";/
+ "<U0053><U0065><U0070><U0074><U0065><U006D><U0062><U0065><U0072>";/
+ "<U004F><U0063><U0074><U006F><U0062><U0065><U0072>";/
+ "<U004E><U006F><U0076><U0065><U006D><U0062><U0065><U0072>";/
+ "<U0044><U0065><U0063><U0065><U006D><U0062><U0065><U0072>"
+% Appropriate date and time representation (%c)
+% "%a %d %b %Y %r %Z"
+d_t_fmt "<U0025><U0061><U0020><U0025><U0064><U0020><U0025><U0062><U0020><U0025><U0059><U0020><U0025><U0072><U0020><U0025><U005A>"
+%
+% Appropriate date representation (%x)
+% "%m/%d/%Y"
+d_fmt "<U0025><U006D><U002F><U0025><U0064><U002F><U0025><U0059>"
+%
+% Appropriate time representation (%X)
+% "%r"
+t_fmt "<U0025><U0072>"
+%
+% Appropriate AM/PM time representation (%r)
+% "%I:%M:%S %p"
+t_fmt_ampm "<U0025><U0049><U003A><U0025><U004D><U003A><U0025><U0053><U0020>/
+<U0025><U0070>"
+%
+% Strings for AM/PM
+%
+am_pm "<U0041><U004D>";"<U0050><U004D>"
+%
+% Appropriate date representation (date(1)) "%a %b %e %H:%M:%S %Z %Y"
+date_fmt "<U0025><U0061><U0020><U0025><U0062><U0020><U0025><U0065>/
+<U0020><U0025><U0048><U003A><U0025><U004D><U003A><U0025><U0053><U0020>/
+<U0025><U005A><U0020><U0025><U0059>"

0 comments on commit 8bc4f03

Please sign in to comment.