-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Translation] Added intl message formatter. #20007
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Symfony/Component/Translation/Formatter/IntlMessageFormatter.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
if (null === $formatter) { | ||
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; | ||
} | ||
} |
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/Translation/Tests/Formatter/IntlMessageFormatterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
if (!extension_loaded('intl')) { | ||
$this->markTestSkipped( | ||
'The Intl extension is not available.' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @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', '<')) { | ||
$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() | ||
{ | ||
return new IntlMessageFormatter(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point in
MessageFormatter
abstraction if at the end you are coupled with specific implementation?