Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 758c07f

Browse files
bug #44085 [Translation] Fix TranslationPullCommand with ICU translations (Kocal)
This PR was squashed before being merged into the 5.3 branch. Discussion ---------- [Translation] Fix TranslationPullCommand with ICU translations | Q | A | ------------- | --- | Branch? | 5.3 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Today I've faced an issue when trying to pull (ICU and non-ICU) translations from Loco: - I have a non-ICU translation `say_hello` tagged as `messages` - and a ICU translation `say_hello_icu` tagged as `messages+intl-icu` When running `bin/console translation:pull loco --domains messages --domains messages+intl-icu`, it **only** creates a single file `messages.fr.xlf` that contains the two translations, so `say_hello_icu` will never be interpreted as an ICU-translation: ```xml <?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> <file source-language="fr" target-language="fr" datatype="plaintext" original="file.ext"> <header> <tool tool-id="symfony" tool-name="Symfony"/> </header> <body> <trans-unit id="xQEX2ok" resname="say_hello_icu"> <source>say_hello_icu</source> <target>Bonjour {name} !</target> </trans-unit> <trans-unit id="1IHotcu" resname="say_hello"> <source>say_hello</source> <target>Bonjour %name% !</target> </trans-unit> </body> </file> </xliff> ``` With this fix, it creates two files as expected: - `messages.fr.xlf`: ```xml <?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> <file source-language="fr" target-language="fr" datatype="plaintext" original="file.ext"> <header> <tool tool-id="symfony" tool-name="Symfony"/> </header> <body> <trans-unit id="1IHotcu" resname="say_hello"> <source>say_hello</source> <target>Bonjour %name% !</target> </trans-unit> </body> </file> </xliff> ``` - `messages+intl-icu.fr.xlf`: ```xml <?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> <file source-language="fr" target-language="fr" datatype="plaintext" original="file.ext"> <header> <tool tool-id="symfony" tool-name="Symfony"/> </header> <body> <trans-unit id="xQEX2ok" resname="say_hello_icu"> <source>say_hello_icu</source> <target>Bonjour {name} !</target> </trans-unit> </body> </file> </xliff> ``` I've also added more tests to `LocoProvider` and `TranslationPullCommand`, ensuring it still handle ICU translations in the future. Ping `@welcoMattic` Commits ------- 1802488 [Translation] Fix TranslationPullCommand with ICU translations
2 parents 231918b + 1802488 commit 758c07f

File tree

5 files changed

+93
-9
lines changed

5 files changed

+93
-9
lines changed

src/Symfony/Component/Translation/Bridge/Loco/Tests/LocoProviderTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,16 @@ public function getResponsesForManyLocalesAndManyDomains(): \Generator
426426
$expectedTranslatorBag = new TranslatorBag();
427427
$expectedTranslatorBag->addCatalogue($arrayLoader->load([
428428
'index.hello' => 'Hello',
429-
'index.greetings' => 'Welcome, {firstname}!',
430429
], 'en'));
430+
$expectedTranslatorBag->addCatalogue($arrayLoader->load([
431+
'index.greetings' => 'Welcome, {firstname}!',
432+
], 'en', 'messages+intl-icu'));
431433
$expectedTranslatorBag->addCatalogue($arrayLoader->load([
432434
'index.hello' => 'Bonjour',
433-
'index.greetings' => 'Bienvenue, {firstname} !',
434435
], 'fr'));
436+
$expectedTranslatorBag->addCatalogue($arrayLoader->load([
437+
'index.greetings' => 'Bienvenue, {firstname} !',
438+
], 'fr', 'messages+intl-icu'));
435439
$expectedTranslatorBag->addCatalogue($arrayLoader->load([
436440
'firstname.error' => 'Firstname must contains only letters.',
437441
'lastname.error' => 'Lastname must contains only letters.',
@@ -443,7 +447,7 @@ public function getResponsesForManyLocalesAndManyDomains(): \Generator
443447

444448
yield [
445449
['en', 'fr'],
446-
['messages', 'validators'],
450+
['messages', 'messages+intl-icu', 'validators'],
447451
[
448452
'en' => [
449453
'messages' => <<<'XLIFF'
@@ -458,6 +462,19 @@ public function getResponsesForManyLocalesAndManyDomains(): \Generator
458462
<source>index.hello</source>
459463
<target state="translated">Hello</target>
460464
</trans-unit>
465+
</body>
466+
</file>
467+
</xliff>
468+
XLIFF
469+
,
470+
'messages+intl-icu' => <<<'XLIFF'
471+
<?xml version="1.0" encoding="UTF-8"?>
472+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd">
473+
<file original="https://localise.biz/user/symfony-translation-provider" source-language="en" datatype="database" tool-id="loco">
474+
<header>
475+
<tool tool-id="loco" tool-name="Loco" tool-version="1.0.25 20201211-1" tool-company="Loco"/>
476+
</header>
477+
<body>
461478
<trans-unit id="loco:5fd89b8542e5aa5cc27457e2" resname="index.greetings" datatype="plaintext" extradata="loco:format=icu">
462479
<source>index.greetings</source>
463480
<target state="translated">Welcome, {firstname}!</target>
@@ -502,6 +519,19 @@ public function getResponsesForManyLocalesAndManyDomains(): \Generator
502519
<source>index.hello</source>
503520
<target state="translated">Bonjour</target>
504521
</trans-unit>
522+
</body>
523+
</file>
524+
</xliff>
525+
XLIFF
526+
,
527+
'messages+intl-icu' => <<<'XLIFF'
528+
<?xml version="1.0" encoding="UTF-8"?>
529+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd">
530+
<file original="https://localise.biz/user/symfony-translation-provider" source-language="en" datatype="database" tool-id="loco">
531+
<header>
532+
<tool tool-id="loco" tool-name="Loco" tool-version="1.0.25 20201211-1" tool-company="Loco"/>
533+
</header>
534+
<body>
505535
<trans-unit id="loco:5fd89b8542e5aa5cc27457e2" resname="index.greetings" datatype="plaintext" extradata="loco:format=icu">
506536
<source>index.greetings</source>
507537
<target state="translated">Bienvenue, {firstname} !</target>

src/Symfony/Component/Translation/Catalogue/AbstractOperation.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,18 @@ public function __construct(MessageCatalogueInterface $source, MessageCatalogueI
8383
public function getDomains()
8484
{
8585
if (null === $this->domains) {
86-
$this->domains = array_values(array_unique(array_merge($this->source->getDomains(), $this->target->getDomains())));
86+
$domains = [];
87+
foreach ([$this->source, $this->target] as $catalogue) {
88+
foreach ($catalogue->getDomains() as $domain) {
89+
$domains[$domain] = $domain;
90+
91+
if ($catalogue->all($domainIcu = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX)) {
92+
$domains[$domainIcu] = $domainIcu;
93+
}
94+
}
95+
}
96+
97+
$this->domains = array_values($domains);
8798
}
8899

89100
return $this->domains;

src/Symfony/Component/Translation/Tests/Catalogue/MergeOperationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testGetResultFromIntlDomain()
5858
$this->assertEquals(
5959
new MessageCatalogue('en', [
6060
'messages' => ['a' => 'old_a', 'b' => 'old_b'],
61-
'messages+intl-icu' => ['d' => 'old_d', 'c' => 'new_c'],
61+
'messages+intl-icu' => ['d' => 'old_d', 'c' => 'new_c', 'a' => 'new_a'],
6262
]),
6363
$this->createOperation(
6464
new MessageCatalogue('en', ['messages' => ['a' => 'old_a', 'b' => 'old_b'], 'messages+intl-icu' => ['d' => 'old_d']]),

src/Symfony/Component/Translation/Tests/Catalogue/TargetOperationTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public function testGetResultWithMixedDomains()
7272
$this->assertEquals(
7373
new MessageCatalogue('en', [
7474
'messages' => ['a' => 'old_a'],
75+
'messages+intl-icu' => ['a' => 'new_a'],
7576
]),
7677
$this->createOperation(
7778
new MessageCatalogue('en', ['messages' => ['a' => 'old_a']]),
@@ -103,7 +104,7 @@ public function testGetResultWithMixedDomains()
103104
$this->assertEquals(
104105
new MessageCatalogue('en', [
105106
'messages' => ['a' => 'old_a'],
106-
'messages+intl-icu' => ['b' => 'new_b'],
107+
'messages+intl-icu' => ['b' => 'new_b', 'a' => 'new_a'],
107108
]),
108109
$this->createOperation(
109110
new MessageCatalogue('en', ['messages' => ['a' => 'old_a']]),

src/Symfony/Component/Translation/Tests/Command/TranslationPullCommandTest.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,27 @@ public function testPullNewXlf12Messages()
4646
{
4747
$arrayLoader = new ArrayLoader();
4848
$filenameEn = $this->createFile();
49+
$filenameEnIcu = $this->createFile(['say_hello' => 'Welcome, {firstname}!'], 'en', 'messages+intl-icu.%locale%.xlf');
4950
$filenameFr = $this->createFile(['note' => 'NOTE'], 'fr');
51+
$filenameFrIcu = $this->createFile(['say_hello' => 'Bonjour, {firstname}!'], 'fr', 'messages+intl-icu.%locale%.xlf');
5052
$locales = ['en', 'fr'];
51-
$domains = ['messages'];
53+
$domains = ['messages', 'messages+intl-icu'];
5254

5355
$providerReadTranslatorBag = new TranslatorBag();
5456
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
5557
'note' => 'NOTE',
5658
'new.foo' => 'newFoo',
5759
], 'en'));
60+
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
61+
'say_hello' => 'Welcome, {firstname}!',
62+
], 'en', 'messages+intl-icu'));
5863
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
5964
'note' => 'NOTE',
6065
'new.foo' => 'nouveauFoo',
6166
], 'fr'));
67+
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
68+
'say_hello' => 'Bonjour, {firstname}!',
69+
], 'fr', 'messages+intl-icu'));
6270

6371
$provider = $this->createMock(ProviderInterface::class);
6472
$provider->expects($this->once())
@@ -71,9 +79,9 @@ public function testPullNewXlf12Messages()
7179
->willReturn('null://default');
7280

7381
$tester = $this->createCommandTester($provider, $locales, $domains);
74-
$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages']]);
82+
$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages', 'messages+intl-icu']]);
7583

76-
$this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
84+
$this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages, messages+intl-icu"', trim($tester->getDisplay()));
7785
$this->assertXmlStringEqualsXmlString(<<<XLIFF
7886
<?xml version="1.0"?>
7987
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
@@ -97,6 +105,23 @@ public function testPullNewXlf12Messages()
97105
, file_get_contents($filenameEn));
98106
$this->assertXmlStringEqualsXmlString(<<<XLIFF
99107
<?xml version="1.0"?>
108+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
109+
<file source-language="en" target-language="en" datatype="plaintext" original="file.ext">
110+
<header>
111+
<tool tool-id="symfony" tool-name="Symfony"/>
112+
</header>
113+
<body>
114+
<trans-unit id="1IHotcu" resname="say_hello">
115+
<source>say_hello</source>
116+
<target>Welcome, {firstname}!</target>
117+
</trans-unit>
118+
</body>
119+
</file>
120+
</xliff>
121+
XLIFF
122+
, file_get_contents($filenameEnIcu));
123+
$this->assertXmlStringEqualsXmlString(<<<XLIFF
124+
<?xml version="1.0"?>
100125
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
101126
<file source-language="en" target-language="fr" datatype="plaintext" original="file.ext">
102127
<header>
@@ -116,6 +141,23 @@ public function testPullNewXlf12Messages()
116141
</xliff>
117142
XLIFF
118143
, file_get_contents($filenameFr));
144+
$this->assertXmlStringEqualsXmlString(<<<XLIFF
145+
<?xml version="1.0"?>
146+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
147+
<file source-language="en" target-language="fr" datatype="plaintext" original="file.ext">
148+
<header>
149+
<tool tool-id="symfony" tool-name="Symfony"/>
150+
</header>
151+
<body>
152+
<trans-unit id="1IHotcu" resname="say_hello">
153+
<source>say_hello</source>
154+
<target>Bonjour, {firstname}!</target>
155+
</trans-unit>
156+
</body>
157+
</file>
158+
</xliff>
159+
XLIFF
160+
, file_get_contents($filenameFrIcu));
119161
}
120162

121163
public function testPullNewXlf20Messages()

0 commit comments

Comments
 (0)