From a980f7236474991d50b9d80bbdfb1158ff709ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Thu, 14 Jan 2010 09:54:45 -0430 Subject: [PATCH] Refactoring I18n::translate() to return time format string or arrays when category is LC_TIME Tests added --- cake/libs/i18n.php | 36 +++++++++++++++++++++++++++++ cake/tests/cases/libs/i18n.test.php | 8 +++++++ 2 files changed, 44 insertions(+) diff --git a/cake/libs/i18n.php b/cake/libs/i18n.php index 4ea024a83..ffdc8422a 100644 --- a/cake/libs/i18n.php +++ b/cake/libs/i18n.php @@ -164,6 +164,10 @@ class I18n extends Object { $_this->__cache = true; } + if ($_this->category == 'LC_TIME') { + return $_this->__translateTime($singular,$domain); + } + if (!isset($count)) { $plurals = 0; } elseif (!empty($_this->__domains[$_this->category][$_this->__lang][$domain]["%plural-c"]) && $_this->__noLocale === false) { @@ -448,6 +452,14 @@ class I18n extends Object { return $this->__domains[$this->category][$this->__lang][$domain] = array_merge($merge ,$translations); } +/** + * Parses a locale definition file following the POSIX standard + * + * @param string $file file to load + * @param string $domain Domain where locale definitions will be stored + * @return void + * @access private + */ function __loadLocaleDefinition($file, $domain = null) { $_this =& I18N::getInstance(); $comment = '#'; @@ -503,6 +515,13 @@ class I18n extends Object { } } +/** + * Auxiliary function to parse a symbol from a locale definition file + * + * @param string $string Symbol to be parsed + * @return string parsed symbol + * @access private + */ function __parseLiteralValue($string) { $string = $string[1]; if (substr($string,0,2) === $this->__escape . 'x') { @@ -524,6 +543,23 @@ class I18n extends Object { return $string; } +/** + * Returns a Time format definition from corresponding domain + * + * @param string $format Format to be translated + * @param string $domain Domain where format is stored + * @return mixed translated format string if only value or array of translated strings for corresponding format. + * @access private + */ + function __translateTime($format,$domain) { + if (!empty($this->__domains['LC_TIME'][$this->__lang][$domain][$format])) { + if (($trans = $this->__domains[$this->category][$this->__lang][$domain][$format])) { + return $trans; + } + } + return $format; + } + /** * Object destructor * diff --git a/cake/tests/cases/libs/i18n.test.php b/cake/tests/cases/libs/i18n.test.php index 23fab7bef..53ba2f8a0 100644 --- a/cake/tests/cases/libs/i18n.test.php +++ b/cake/tests/cases/libs/i18n.test.php @@ -2594,6 +2594,14 @@ class I18nTest extends CakeTestCase { $result = __c('d_fmt', 5, true); $expected = '%m/%d/%Y'; $this->assertEqual($result,$expected); + + $result = __c('am_pm',5,true); + $expected = array('AM','PM'); + $this->assertEqual($result,$expected); + + $result = __c('abmon',5,true); + $expected = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'); + $this->assertEqual($result,$expected); } /**