8000 [Translation] Added intl message formatter. by Nyholm · Pull Request #27399 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Added intl message formatter. #27399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 4, 2018
Prev Previous commit
Next Next commit
[Translation] Added intl message formatter.
  • Loading branch information
aitboudad authored and Nyholm committed Sep 3, 2018
commit c2b3dc0a9007d2aaf8cc5fb8479a76869a29175e
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<argument type="service" id="identity_translator" />
</service>

<service id="translator.formatter.intl" class="Symfony\Component\Translation\Formatter\IntlMessageFormatter" public="false" />

<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />

<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
<tag name="translation.loader" alias="php" />
</service>
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ CHANGELOG
-----

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

3.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Formatter;

/**
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class IntlMessageFormatter implements MessageFormatterInterface
{
/**
* {@inheritdoc}
*/
public function format($message, $locale, array $parameters = array())
{
$formatter = new \MessageFormatter($locale, $message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the parsing of this message fails you get a very non-descriptive exception message (Constructor failed). Might be good for DX to rethrow a more descriptive one.

if (null === $formatter) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will never be null here

throw new \InvalidArgumentException(sprintf('Invalid message format. Reason: %s (error #%d)', intl_get_error_message(), intl_get_error_code()));
}

$message = $formatter->format($parameters);
if ($formatter->getErrorCode() !== U_ZERO_ERROR) {
throw new \InvalidArgumentException(sprintf('Unable to format message. Reason: %s (error #%s)', $formatter->getErrorMessage(), $formatter->getErrorCode()));
}

return $message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Translation\Tests\Formatter;

use Symfony\Component\Translation\Formatter\IntlMessageFormatter;

class IntlMessageFormatterTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be protected

{
if (!extension_loaded('intl')) {
$this->markTestSkipped(
'The Intl extension is not available.'
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be on one line

}
}

/**
* @dataProvider provideDataForFormat
*/
public function testFormat($expected, $message, $arguments)
{
$this->assertEquals($expected, trim($this->getMessageFormatter()->format($message, 'en', $arguments)));
}

public function testFormatWithNamedArguments()
{
if (PHP_VERSION_ID < 50500 || version_compare(INTL_ICU_VERSION, '4.8', '<')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for php version is redundant

"php": "^7.1.3",

$this->markTestSkipped('Format with named arguments can only be run with ICU 4.8 or higher and PHP >= 5.5');
}

$chooseMessage = <<<'_MSG_'
{gender_of_host, select,
female {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to her party.}
=2 {{host} invites {guest} and one other person to her party.}
other {{host} invites {guest} as one of the # people invited to her party.}}}
male {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to his party.}
=2 {{host} invites {guest} and one other person to his party.}
other {{host} invites {guest} as one of the # people invited to his party.}}}
other {{num_guests, plural, offset:1
=0 {{host} does not give a party.}
=1 {{host} invites {guest} to their party.}
=2 {{host} invites {guest} and one other person to their party.}
other {{host} invites {guest} as one of the # people invited to their party.}}}}
_MSG_;

$formatter = $this->getMessageFormatter();
$message = $formatter->format($chooseMessage, 'en', array(
'gender_of_host' => 'male',
'num_guests' => 10,
'host' => 'Fabien',
'guest' => 'Guilherme',
));

$this->assertEquals('Fabien invites Guilherme as one of the 9 people invited to his party.', $message);
}

public function provideDataForFormat()
{
return array(
array(
'There is one apple',
'There is one apple',
array(),
),
array(
'4,560 monkeys on 123 trees make 37.073 monkeys per tree',
'{0,number,integer} monkeys on {1,number,integer} trees make {2,number} monkeys per tree',
array(4560, 123, 4560 / 123),
),
);
}

private function getMessageFormatter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be inlined instead.

{
return new IntlMessageFormatter();
}
}
0