8000 feature #37371 [Translation] Add support for calling 'trans' with ICU… · symfony/symfony@241af80 · GitHub
[go: up one dir, main page]

Skip to content

Commit 241af80

Browse files
committed
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 #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 ------- d2ec41f [Translation] Add support for calling 'trans' with ICU formatted messages
2 parents dc54cc9 + d2ec41f commit 241af80

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.2.0
55
-----
66

7+
* added support for calling `trans` with ICU formatted messages
78
* added `PseudoLocalizationTranslator`
89
* added `Translatable` objects that represent a message that can be translated
910
* added the `t()` function to easily create `Translatable` objects

src/Symfony/Component/Translation/Tests/TranslatorTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,14 @@ public function testTrans($expected, $id, $translation, $parameters, $locale, $d
368368
$this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
369369
}
370370

371+
/**
372+
* @dataProvider getTransICUTests
373+
*/
374+
public function testTransICU(...$args)
375+
{
376+
$this->testTrans(...$args);
377+
}
378+
371379
/**
372380
* @dataProvider getInvalidLocalesTests
373381
*/
@@ -444,6 +452,17 @@ public function getTransTests()
444452
];
445453
}
446454

455+
public function getTransICUTests()
456+
{
457+
$id = '{apples, plural, =0 {There are no apples} one {There is one apple} other {There are # apples}}';
458+
459+
return [
460+
['There are no apples', $id, $id, ['{apples}' => 0], 'en', 'test'.MessageCatalogue::INTL_DOMAIN_SUFFIX],
461+
['There is one apple', $id, $id, ['{apples}' => 1], 'en', 'test'.MessageCatalogue::INTL_DOMAIN_SUFFIX],
462+
['There are 3 apples', $id, $id, ['{apples}' => 3], 'en', 'test'.MessageCatalogue::INTL_DOMAIN_SUFFIX],
463+
];
464+
}
465+
447466
public function getFlattenedTransTests()
448467
{
449468
$messages = [

src/Symfony/Component/Translation/Translator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ public function trans(?string $id, array $parameters = [], string $domain = null
214214
}
215215
}
216216

217-
if ($this->hasIntlFormatter && $catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)) {
217+
$len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
218+
if ($this->hasIntlFormatter
219+
&& ($catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)
220+
|| 0 == substr_compare($domain, MessageCatalogue::INTL_DOMAIN_SUFFIX, -$len, $len))) {
218221
return $this->formatter->formatIntl($catalogue->get($id, $domain), $locale, $parameters);
219222
}
220223

0 commit comments

Comments
 (0)
0