Skip to content

Commit

Permalink
Added an ICU string formatter that uses the MessageFormatter class
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 23, 2014
1 parent 094790e commit d91f479
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/I18n/Formatter/IcuFormatter.php
@@ -0,0 +1,66 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n\Formatter;

use Aura\Intl\Exception;
use Aura\Intl\FormatterInterface;
use Cake\I18n\PluralRules;
use MessageFormatter;

/**
* A formatter that will interpolate variables using the MessageFormatter class
*/
class IcuFormatter implements FormatterInterface {

/**
* Returns a string with all passed variables interpolated into the original
* message. Variables are interpolated using the MessageFormatter class.
*
* If an array is passed in `$message`, it will trigger the plural selection
* routine. Plural forms are selected depending on the locale and the `_count`
* key passed in `$vars`.
*
* @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
*/
public function format($locale, $message, array $vars) {
if (!is_string($message)) {
$count = isset($vars['_count']) ? $vars['_count'] : 0;
$form = PluralRules::calculate($locale, $vars['_count']);
$message = $message[$form];
}

$formatter = new MessageFormatter($locale, $message);

if (!$formatter) {
throw new Exception\CannotInstantiateFormatter(
intl_get_error_message(),
intl_get_error_code()
);
}

$result = $formatter->format($vars);
if ($result === false) {
throw new Exception\CannotFormat(
$formatter->getErrorMessage(),
$formatter->getErrorCode()
);
}

return $result;
}

}
102 changes: 102 additions & 0 deletions tests/TestCase/I18n/Formatter/IcuFormatterTest.php
@@ -0,0 +1,102 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\I18n;

use Cake\I18n\Formatter\IcuFormatter;
use Cake\TestSuite\TestCase;

/**
* IcuFormatter tests
*
*/
class IcuFormatterTest extends TestCase {

/**
* Tests that variables are interpolated correctly
*
* @return void
*/
public function testFormatSimple() {
$formatter = new IcuFormatter();
$this->assertEquals('Hello José', $formatter->format('en_US', 'Hello {0}', ['José']));
$result = $formatter->format(
'1 Orange',
'{count, number} {fruit}',
['count' => 1.0, 'fruit' => 'Orange']
);
$this->assertEquals('1 Orange', $result);
}

/**
* Tests that plural forms can be selected using the PO file format plural forms
*
* @return void
*/
public function testFormatPlural() {
$formatter = new IcuFormatter();
$messages = [
'{0} is 0',
'{0} is 1',
'{0} is 2',
'{0} is 3',
'{0} > 11'
];
$this->assertEquals('1 is 1', $formatter->format('ar', $messages, ['_count' => 1, 1]));
$this->assertEquals('2 is 2', $formatter->format('ar', $messages, ['_count' => 2, 2]));
$this->assertEquals('20 > 11', $formatter->format('ar', $messages, ['_count' => 20, 20]));
}

/**
* Tests that plurals can instead be selected using ICU's native selector
*
* @return void
*/
public function testNativePluralSelection() {
$formatter = new IcuFormatter();
$locale = 'en_US';
$string = '{fruits,plural,'
. '=0{No fruits.}'
. '=1{We have one fruit}'
. 'other{We have {count} fruits}'
. '}';

$params = ['fruits' => 0];
$expect = 'No fruits.';
$actual = $formatter->format($locale, $string, $params);
$this->assertSame($expect, $actual);

$params = ['fruits' => 1];
$expect = 'We have one fruit';
$actual = $formatter->format($locale, $string, $params);
$this->assertSame($expect, $actual);

$params = ['fruits' => 10, 'count' => 10];
$expect = 'We have 10 fruits';
$actual = $formatter->format($locale, $string, $params);
$this->assertSame($expect, $actual);
}

/**
* Tests that passing a message in the wrong format will throw an exception
*
* @expectedException Aura\Intl\Exception\CannotInstantiateFormatter
* @return void
*/
public function testBadMessageFormat() {
$formatter = new IcuFormatter();
$formatter->format('en_US', '{crazy format', ['some', 'vars']);
}

}

0 comments on commit d91f479

Please sign in to comment.