From fa9a975dbbca6723cda1772cec74d8e5251b2db4 Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 16 Oct 2013 04:41:03 +0200 Subject: [PATCH] intl message parser now handles too many or too less args --- framework/yii/i18n/MessageFormatter.php | 46 ++++++++++++---------- tests/unit/framework/i18n/MessageFormatterTest.php | 32 +++++++++++++++ 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/framework/yii/i18n/MessageFormatter.php b/framework/yii/i18n/MessageFormatter.php index cba0091..d83541c 100644 --- a/framework/yii/i18n/MessageFormatter.php +++ b/framework/yii/i18n/MessageFormatter.php @@ -65,7 +65,8 @@ class MessageFormatter extends \MessageFormatter { $map = array_flip(array_keys($args)); - // parsing http://icu-project.org/apiref/icu4c/classMessageFormat.html#details + // parsing pattern base on ICU grammar: + // http://icu-project.org/apiref/icu4c/classMessageFormat.html#details $parts = explode('{', $pattern); $c = count($parts); $pattern = $parts[0]; @@ -73,26 +74,29 @@ class MessageFormatter extends \MessageFormatter $stack = array(); for($i = 1; $i < $c; $i++) { if (preg_match('~^\A(\s*)([\d\w]+)(\s*)([},])(\s*)(.*)\z$~u', $parts[$i], $matches)) { - $d++; - // replace normal arg if it was set - if (isset($map[$matches[2]])) { - $q = ''; - $pattern .= '{' . $matches[1] . $map[$matches[2]] . $matches[3]; - } else { - // quote unused args - $q = '';//($matches[4] == '}' && isset($stack[$d]) && !($stack[$d] == 'plural' || $stack[$d] == 'select')) ? "'" : ""; - $pattern .= "$q{" . $matches[1] . $matches[2] . $matches[3]; - } - $pattern .= ($term = $matches[4] . $q . $matches[5] . $matches[6]); - // check type current level - $stack[$d] = ($matches[4] == ',') ? substr($matches[6], 0, 6) : 'none'; - // if it's plural or select, the next bracket is NOT begin of a message then! - if ($stack[$d] == 'plural' || $stack[$d] == 'select') { - $i++; - $d -= substr_count($term, '}'); - } else { - $d -= substr_count($term, '}'); - continue; + // if we are not inside a plural or select this is a message + if (!isset($stack[$d]) || $stack[$d] != 'plural' && $stack[$d] != 'select') { + $d++; + // replace normal arg if it is available + if (isset($map[$matches[2]])) { + $q = ''; + $pattern .= '{' . $matches[1] . $map[$matches[2]] . $matches[3]; + } else { + // quote unused args + $q = ($matches[4] == '}') ? "'" : ""; + $pattern .= "$q{" . $matches[1] . $matches[2] . $matches[3]; + } + $pattern .= ($term = $matches[4] . $q . $matches[5] . $matches[6]); + // store type of current level + $stack[$d] = ($matches[4] == ',') ? substr($matches[6], 0, 6) : 'none'; + // if it's plural or select, the next bracket is NOT begin of a message then! + if ($stack[$d] == 'plural' || $stack[$d] == 'select') { + $i++; + $d -= substr_count($term, '}'); + } else { + $d -= substr_count($term, '}'); + continue; + } } } $pattern .= '{' . $parts[$i]; diff --git a/tests/unit/framework/i18n/MessageFormatterTest.php b/tests/unit/framework/i18n/MessageFormatterTest.php index 51e512c..65f9ea4 100644 --- a/tests/unit/framework/i18n/MessageFormatterTest.php +++ b/tests/unit/framework/i18n/MessageFormatterTest.php @@ -65,6 +65,38 @@ _MSG_; 'gender' => 'male', )); $this->assertEquals('Alexander is male and he loves Yii!', $result); + + // verify pattern in select does not get replaced + $pattern = '{name} is {gender} and {gender, select, female{she} male{he} other{it}} loves Yii!'; + $result = MessageFormatter::formatMessage('en_US', $pattern, array( + 'name' => 'Alexander', + 'gender' => 'male', + // following should not be replaced + 'he' => 'wtf', + 'she' => 'wtf', + 'it' => 'wtf', + )); + $this->assertEquals('Alexander is male and he loves Yii!', $result); + + // verify pattern in select message gets replaced + $pattern = '{name} is {gender} and {gender, select, female{she} male{{he}} other{it}} loves Yii!'; + $result = MessageFormatter::formatMessage('en_US', $pattern, array( + 'name' => 'Alexander', + 'gender' => 'male', + 'he' => 'wtf', + 'she' => 'wtf', + )); + $this->assertEquals('Alexander is male and wtf loves Yii!', $result); + + // some parser specific verifications + $pattern = '{gender} and {gender, select, female{she} male{{he}} other{it}} loves {nr, number} is {gender}!'; + $result = MessageFormatter::formatMessage('en_US', $pattern, array( + 'nr' => 42, + 'gender' => 'male', + 'he' => 'wtf', + 'she' => 'wtf', + )); + $this->assertEquals('male and wtf loves 42 is male!', $result); } public function testInsufficientArguments()