feature #37371 [Translation] Add support for calling 'trans' with ICU formatted messages (someonewithpc)

This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[Translation] Add support for calling 'trans' with ICU formatted messages

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | maybe, see https://github.com/symfony/symfony/issues/37228
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #37228
| License       | MIT
| Doc PR        | symfony/symfony-docs#13875

Motivation:

```
$apples = [0 => 'No apples', 1 => '1 apple', '# apples'];
echo _m($apples, ['count' => 0]); // Outputs 'No apples'
echo _m($apples, ['count' => 2]); // Outputs '2 apples'
```

where `_m` is a wrapper my application is using, but we obviously don't want to replicate many of the effort of the translation component, so it relies on `trans`.

This wrapper itself could be integrated into Symfony, if deemed appropriate.

See #37228

Commits
-------

d2ec41f4ef [Translation] Add support for calling 'trans' with ICU formatted messages
This commit is contained in:
Fabien Potencier 2020-08-30 09:19:50 +02:00
commit 241af8057f
3 changed files with 24 additions and 1 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
5.2.0
-----
* added support for calling `trans` with ICU formatted messages
* added `PseudoLocalizationTranslator`
* added `Translatable` objects that represent a message that can be translated
* added the `t()` function to easily create `Translatable` objects

View File

@ -368,6 +368,14 @@ class TranslatorTest extends TestCase
$this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
}
/**
* @dataProvider getTransICUTests
*/
public function testTransICU(...$args)
{
$this->testTrans(...$args);
}
/**
* @dataProvider getInvalidLocalesTests
*/
@ -444,6 +452,17 @@ class TranslatorTest extends TestCase
];
}
public function getTransICUTests()
{
$id = '{apples, plural, =0 {There are no apples} one {There is one apple} other {There are # apples}}';
return [
['There are no apples', $id, $id, ['{apples}' => 0], 'en', 'test'.MessageCatalogue::INTL_DOMAIN_SUFFIX],
['There is one apple', $id, $id, ['{apples}' => 1], 'en', 'test'.MessageCatalogue::INTL_DOMAIN_SUFFIX],
['There are 3 apples', $id, $id, ['{apples}' => 3], 'en', 'test'.MessageCatalogue::INTL_DOMAIN_SUFFIX],
];
}
public function getFlattenedTransTests()
{
$messages = [

View File

@ -214,7 +214,10 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
}
}
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
$len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
if ($this->hasIntlFormatter
&& ($catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)
|| 0 == substr_compare($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX, -$len, $len))) {
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, $parameters);
}