Skip to content

Commit

Permalink
ADding mesage context support to the ICU formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 27, 2014
1 parent 10eb331 commit 2db472c
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/I18n/Formatter/IcuFormatter.php
Expand Up @@ -37,12 +37,41 @@ class IcuFormatter implements FormatterInterface {
* @return string The formatted message
*/
public function format($locale, $message, array $vars) {
if (is_string($message)) {
return $this->_formatMessage($locale, $message, $vars);
}

if (isset($vars['_context']) && isset($message['_context'])) {
$message = $message['_context'][$vars['_context']];
unset($vars['_context']);
}

// Assume first context when no context key was passed
if (isset($message['_context'])) {
$message = current($message['_context']);
}

if (!is_string($message)) {
$count = isset($vars['_count']) ? $vars['_count'] : 0;
$form = PluralRules::calculate($locale, $vars['_count']);
$form = PluralRules::calculate($locale, $count);
$message = $message[$form];
}

return $this->_formatMessage($locale, $message, $vars);
}

/**
* Does the actual formatting using the MessageFormatter class
*
* @param string $locale The locale in which the message is presented.
* @param string|array $message The message to be translated
* @return string The formatted message
* @throws Aura\Intl\Exception\CannotInstantiateFormatter if any error occurred
* while parsing the message
* @throws Aura\Intl\Exception\CannotFormat If any error related to the passed
* variables is found
*/
protected function _formatMessage($locale, $message, $vars) {
$formatter = new MessageFormatter($locale, $message);

if (!$formatter) {
Expand Down
48 changes: 48 additions & 0 deletions tests/TestCase/I18n/Formatter/IcuFormatterTest.php
Expand Up @@ -99,4 +99,52 @@ public function testBadMessageFormat() {
$formatter->format('en_US', '{crazy format', ['some', 'vars']);
}

/**
* Tests that strings stored inside context namespaces can also be formatted
*
* @return void
*/
public function testFormatWithContext() {
$messages = [
'simple' => [
'_context' => [
'context a' => 'Text "a" {thing}',
'context b' => 'Text "b" {thing}'
]
],
'complex' => [
'_context' => [
'context b' => [
0 => 'Only one',
1 => 'there are {_count}'
]
]
]
];

$formatter = new IcuFormatter();
$this->assertEquals(
'Text "a" is good',
$formatter->format('en', $messages['simple'], ['_context' => 'context a', 'thing' => 'is good'])
);
$this->assertEquals(
'Text "b" is good',
$formatter->format('en', $messages['simple'], ['_context' => 'context b', 'thing' => 'is good'])
);
$this->assertEquals(
'Text "a" is good',
$formatter->format('en', $messages['simple'], ['thing' => 'is good'])
);

$this->assertEquals(
'Only one',
$formatter->format('en', $messages['complex'], ['_context' => 'context b', '_count' => 1])
);

$this->assertEquals(
'there are 2',
$formatter->format('en', $messages['complex'], ['_context' => 'context b', '_count' => 2])
);
}

}
5 changes: 5 additions & 0 deletions tests/TestCase/I18n/Formatter/SprintfFormatterTest.php
Expand Up @@ -47,6 +47,11 @@ public function testFormatPlural() {
$this->assertEquals('20 > 11', $formatter->format('ar', $messages, ['_count' => 20]));
}

/**
* Tests that strings stored inside context namespaces can also be formatted
*
* @return void
*/
public function testFormatWithContext() {
$messages = [
'simple' => [
Expand Down

0 comments on commit 2db472c

Please sign in to comment.