-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Translation] SimpleIntlMessageFormatter for users with no icu extension #28492
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
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
124 changes: 124 additions & 0 deletions
124
src/Symfony/Component/Translation/Formatter/IntlSimpleMessageFormatter.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,124 @@ | ||
<?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; | ||
|
||
/** | ||
* A Polyfill for IntlMessageFormatter for users that do not have the icu extension installed. | ||
* | ||
* @author Tobias Nyholm <tobias.nyholm@gmail.com> | ||
*/ | ||
class IntlSimpleMessageFormatter implements MessageFormatterInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function format($message, $locale, array $parameters = array()) | ||
{ | ||
$message = $this->handlePlural($message, $parameters); | ||
$message = $this->handleSelect($message, $parameters); | ||
$message = $this->replaceParameters($message, $parameters); | ||
|
||
return $message; | ||
} | ||
|
||
private function handlePlural(string $message, array $parameters): string | ||
{ | ||
$lookupMap = array(0 => 'zero', 1 => 'one', 2 => 'two', 3 => 'few'); | ||
|
||
foreach ($parameters as $key => $value) { | ||
$regex = '|{ ?'.$key.', plural,(.*)|sm'; | ||
if (preg_match($regex, $message, $match)) { | ||
$blockContent = $this->findBlock($match[1]); | ||
$fullBlock = substr($match[0], 0, strpos($match[0], $blockContent) + \strlen($blockContent) + 1); | ||
|
||
$lookup = array(); | ||
if (\is_int($value)) { | ||
$lookup[] = '='.$value; | ||
} | ||
if (isset($lookupMap[$value])) { | ||
$lookup[] = $lookupMap[$value]; | ||
} elseif ($value > 3) { | ||
$lookup[] = 'many'; | ||
} | ||
$lookup[] = 'other'; | ||
|
||
foreach ($lookup as $l) { | ||
if (preg_match('|'.$l.' ?{(.*)|sm', $blockContent, $blockMatch)) { | ||
$result = $this->findBlock($blockMatch[1]); | ||
$blockReplacement = str_replace('#', $value, $result); | ||
|
||
return str_replace($fullBlock, $blockReplacement, $message); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
private function handleSelect(string $message, array $parameters): string | ||
{ | ||
$regex = '|{ ?([a-zA-Z]+), select,(.*)|sm'; | ||
if (preg_match($regex, $message, $match)) { | ||
$blockContent = $this->findBlock($match[2]); | ||
$fullBlock = substr($match[0], 0, strpos($match[0], $blockContent) + \strlen($blockContent) + 1); | ||
foreach ($parameters as $key => $value) { | ||
if ($match[1] === $key) { | ||
if (preg_match('|'.$value.' ?{(.*)|sm', $blockContent, $blockMatch)) { | ||
$result = $this->findBlock($blockMatch[1]); | ||
|
||
return str_replace($fullBlock, $result, $message); | ||
} | ||
} | ||
} | ||
|
||
// If no match | ||
if (preg_match('|other ?{(.*)|sm', $blockContent, $blockMatch)) { | ||
$result = $this->findBlock($blockMatch[1]); | ||
|
||
return str_replace($fullBlock, $result, $message); | ||
} | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
private function replaceParameters(string $message, array $parameters): string | ||
{ | ||
$updatedParameters = array(); | ||
foreach ($parameters as $key => $value) { | ||
$updatedParameters[sprintf('{%s}', $key)] = $value; | ||
} | ||
|
||
return strtr($message, $updatedParameters); | ||
} | ||
|
||
private function findBlock(string $input): string | ||
{ | ||
// How may open curly brackets ({) do we got? | ||
$open = 1; | ||
$block = ''; | ||
for ($i = 0; $i < \strlen($input); ++$i) { | ||
if ('{' === $input[$i]) { | ||
++$open; | ||
} elseif ('}' === $input[$i]) { | ||
--$open; | ||
} | ||
if (0 === $open) { | ||
$block = substr($input, 0, $i); | ||
break; | ||
} | ||
} | ||
|
||
return $block; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Translation/Tests/Formatter/IntlSimpleMessageFormatterTest.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,55 @@ | ||
<?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 PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Translation\Formatter\IntlSimpleMessageFormatter; | ||
|
||
class IntlSimpleMessageFormatterTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider getTestStrings | ||
*/ | ||
public function testFormat(string $input, string $expected, array $params = array(), string $locale = 'en') | ||
{ | ||
$formatter = new IntlSimpleMessageFormatter(); | ||
$result = $formatter->format($input, 'en', $params, $locale); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function getTestStrings() | ||
{ | ||
$apples = '{ COUNT, plural, | ||
=0 {{name}, there are no apples} | ||
=1 {{name}, there is one apple} | ||
other {{name}, there are # apples} | ||
}'; | ||
|
||
yield array('foobar', 'foobar'); | ||
yield array('foo {name} bar', 'foo test bar', array('name' => 'test')); | ||
yield array($apples, 'Foo, there are no apples', array('COUNT' => 0, 'name' => 'Foo')); | ||
yield array($apples, 'Foo, there is one apple', array('COUNT' => 1, 'name' => 'Foo')); | ||
yield array($apples, 'Foo, there are 2 apples', array('COUNT' => 2, 'name' => 'Foo')); | ||
yield array('Hello {name}. There are {COUNT, plural, zero{no apples} other{some apples}} in the basket', 'Hello Foo. There are no apples in the basket', array('COUNT' => 0, 'name' => 'Foo')); | ||
yield array('Hello {name}. There are {COUNT, plural, zero{no apples} other{some apples}} in the basket', 'Hello Foo. There are some apples in the basket', array('COUNT' => 3, 'name' => 'Foo')); | ||
|
||
// Test select | ||
$gender = 'I think { GENDER, select, | ||
male {he} | ||
female {she} | ||
other {they} | ||
} liked this.'; | ||
yield array($gender, 'I think he liked this.', array('GENDER' => 'male')); | ||
yield array($gender, 'I think she liked this.', array('GENDER' => 'female')); | ||
yield array($gender, 'I think they liked this.', array()); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Would it be possible to provide a polyfill for the
MessageFormatter
class as part of the Intl component (or the intl polyfill) instead?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.
I thought about that and I’m not sure I did the correct decision.
The reason why I did not do a polyfill is because I have no intension of being feature complete. There is massive amount of exceptions that you have to consider when supporting multiple languages. Ie number formats and to figure out what “few” means in all languages.