8000 [Translation] Add support for calling 'trans' with ICU formatted mess… · symfony/symfony@d2ec41f · GitHub
[go: up one dir, main page]

Skip to content

Commit d2ec41f

Browse files
someonewithpcfabpot
authored andcommitted
[Translation] Add support for calling 'trans' with ICU formatted messages
1 parent 27bb282 commit d2ec41f

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

910
5.1.0

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