8000 [Translation] Added intl message formatter. · symfony/symfony@c2b3dc0 · GitHub
[go: up one dir, main page]

Skip to content

Commit c2b3dc0

Browse files
aitboudadNyholm
authored andcommitted
[Translation] Added intl message formatter.
1 parent 19e8e69 commit c2b3dc0

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
<argument type="service" id="identity_translator" />
3434
</service>
3535

36+
<service id="translator.formatter.intl" class="Symfony\Component\Translation\Formatter\IntlMessageFormatter" public="false" />
37+
38+
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />
39+
3640
<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
3741
<tag name="translation.loader" alias="php" />
3842
</service>

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CHANGELOG
4141
-----
4242

4343
* Added support for escaping `|` in plural translations with double pipe.
44+
* Added intl message formatter.
4445

4546
3.1.0
4647
-----
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Formatter;
13+
14+
/**
15+
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
16+
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
17+
*/
18+
class IntlMessageFormatter implements MessageFormatterInterface
19+
{
20+
/**
21+
* {@inheritdoc}
22+
*/
23+
public function format($message, $locale, array $parameters = array())
24+
{
25+
$formatter = new \MessageFormatter($locale, $message);
26+
if (null === $formatter) {
27+
throw new \InvalidArgumentException(sprintf('Invalid message format. Reason: %s (error #%d)', intl_get_error_message(), intl_get_error_code()));
28+
}
29+
30+
$message = $formatter->format($parameters);
31+
if ($formatter->getErrorCode() !== U_ZERO_ERROR) {
32+
throw new \InvalidArgumentException(sprintf('Unable to format message. Reason: %s (error #%s)', $formatter->getErrorMessage(), $formatter->getErrorCode()));
33+
}
34+
35+
return $message;
36+
}
37+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Tests\Formatter;
13+
14+
use Symfony\Component\Translation\Formatter\IntlMessageFormatter;
15+
16+
class IntlMessageFormatterTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function setUp()
19+
{
20+
if (!extension_loaded('intl')) {
21+
$this->markTestSkipped(
22+
'The Intl extension is not available.'
23+
);
24+
A3D4 }
25+
}
26+
27+
/**
28+
* @dataProvider provideDataForFormat
29+
*/
30+
public function testFormat($expected, $message, $arguments)
31+
{
32+
$this->assertEquals($expected, trim($this->getMessageFormatter()->format($message, 'en', $arguments)));
33+
}
34+
35+
public function testFormatWithNamedArguments()
36+
{
37+
if (PHP_VERSION_ID < 50500 || version_compare(INTL_ICU_VERSION, '4.8', '<')) {
38+
$this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5');
39+
}
40+
41+
$chooseMessage = <<<'_MSG_'
42+
{gender_of_host, select,
43+
female {{num_guests, plural, offset:1
44+
=0 {{host} does not give a party.}
45+
=1 {{host} invites {guest} to her party.}
46+
=2 {{host} invites {guest} and one other person to her party.}
47+
other {{host} invites {guest} as one of the # people invited to her party.}}}
48+
male {{num_guests, plural, offset:1
49+
=0 {{host} does not give a party.}
50+
=1 {{host} invites {guest} to his party.}
51+
=2 {{host} invites {guest} and one other person to his party.}
52+
other {{host} invites {guest} as one of the # people invited to his party.}}}
53+
other {{num_guests, plural, offset:1
54+
=0 {{host} does not give a party.}
55+
=1 {{host} invites {guest} to their party.}
56+
=2 {{host} invites {guest} and one other person to their party.}
57+
other {{host} invites {guest} as one of the # people invited to their party.}}}}
58+
_MSG_;
59+
60+
$formatter = $this->getMessageFormatter();
61+
$message = $formatter->format($chooseMessage, 'en', array(
62+
'gender_of_host' => 'male',
63+
'num_guests' => 10,
64+
'host' => 'Fabien',
65+
'guest' => 'Guilherme',
66+
));
67+
68+
$this->assertEquals('Fabien invites Guilherme as one of the 9 people invited to his party.', $message);
69+
}
70+
71+
public function provideDataForFormat()
72+
{
73+
return array(
74+
array(
75+
'There is one apple',
76+
'There is one apple',
77+
array(),
78+
),
79+
array(
80+
'4,560 monkeys on 123 trees make 37.073 monkeys per tree',
81+
'{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree',
82+
array(4560, 123, 4560 / 123),
83+
),
84+
);
85+
}
86+
87+
private function getMessageFormatter()
88+
{
89+
return new IntlMessageFormatter();
90+
}
91+
}

0 commit comments

Comments
 (0)
0