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

Skip to content

Commit d146704

Browse files
committed
bug #42361 [Translation] correctly handle intl domains with TargetOperation (acran)
This PR was merged into the 4.4 branch. Discussion ---------- [Translation] correctly handle intl domains with TargetOperation | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - Executing `translations:update` with `--clean` was producing erratic result: creating duplicate non-/intl files for domains, removing used translations... **TL;DR:** judging from context this was most probably just a typo in 33e6af5 # How to reproduce ## Setup Test Project A fresh minimal project to verify this can be setup with ~~~sh composer create-project symfony/skeleton translations_test cd translations_test/ composer require translation ~~~ and adding extractable translations into a file in `src/`, e.g. ~~~php <?php // in src/translations.php use Symfony\Component\Translation\TranslatableMessage; new TranslatableMessage('First Message'); new TranslatableMessage('Second Message'); ~~~ Otherwise any existing project should work as well. ## Steps to reproduce ### Case 1: duplicate translation files ~~~sh # remove all existing translations and extract them again rm -rf translations/* php bin/console translation:update --force --clean en # verify translations/messages+intl-icu.en.xlf was created containing the messages # extract messages again php bin/console translation:update --force --clean en # messages+intl-icu.en.xlf is unchanged but messages.en.xlf with all the same messages was added ~~~ ### Case 2: lost translations ~~~sh # remove all existing translations and extract them again rm -rf translations/* php bin/console translation:update --force --clean en # verify translations/messages+intl-icu.en.xlf was created containing the messages # remove one or more messages from messages+intl-icu.en.xlf, keeping at least one # extract messages again php bin/console translation:update --force --clean en # messages+intl-icu.en.xlf will *only* contain the missing/"new" messages and all other messages got removed ~~~ # The Fix The previous code was trying to merge with the `target` messages instead of the `source`. This was probably just a typo since `$keyMetadata` is take from `source` just below it and the `foreach` block below which is basically doing the same inverse just switches `target` and `source`. Also the code is mostly identical to [`MergeOperation`](https://github.com/symfony/symfony/blob/2cee75bef9f6c1ca4e532986dc0dcae8461de4dd/src/Symfony/Component/Translation/Catalogue/MergeOperation.php#L39-L46) which also uses `source` at the respective line. The code in both files was introduced in 33e6af5 Commits ------- 84b48fe [Translation] correctly handle intl domains with TargetOperation
2 parents a9b5383 + 84b48fe commit d146704

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected function processDomain($domain)
4949
foreach ($this->source->all($domain) as $id => $message) {
5050
if ($this->target->has($id, $domain)) {
5151
$this->messages[$domain]['all'][$id] = $message;
52-
$d = $this->target->defines($id, $intlDomain) ? $intlDomain : $domain;
52+
$d = $this->source->defines($id, $intlDomain) ? $intlDomain : $domain;
5353
$this->result->add([$id => $message], $d);
5454
if (null !== $keyMetadata = $this->source->getMetadata($id, $d)) {
5555
$this->result->setMetadata($id, $keyMetadata, $d);

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,45 @@ public function testGetResultWithMixedDomains()
7171
{
7272
$this->assertEquals(
7373
new MessageCatalogue('en', [
74-
'messages+intl-icu' => ['a' => 'old_a'],
74+
'messages' => ['a' => 'old_a'],
7575
]),
7676
$this->createOperation(
7777
new MessageCatalogue('en', ['messages' => ['a' => 'old_a']]),
7878
new MessageCatalogue('en', ['messages+intl-icu' => ['a' => 'new_a']])
7979
)->getResult()
8080
);
81+
82+
$this->assertEquals(
83+
new MessageCatalogue('en', [
84+
'messages+intl-icu' => ['a' => 'old_a'],
85+
]),
86+
$this->createOperation(
87+
new MessageCatalogue('en', ['messages+intl-icu' => ['a' => 'old_a']]),
88+
new MessageCatalogue('en', ['messages' => ['a' => 'new_a']])
89+
)->getResult()
90+
);
91+
92+
$this->assertEquals(
93+
new MessageCatalogue('en', [
94+
'messages+intl-icu' => ['a' => 'old_a'],
95+
'messages' => ['b' => 'new_b'],
96+
]),
97+
$this->createOperation(
98+
new MessageCatalogue('en', ['messages+intl-icu' => ['a' => 'old_a']]),
99+
new MessageCatalogue('en', ['messages' => ['a' => 'new_a', 'b' => 'new_b']])
100+
)->getResult()
101+
);
102+
103+
$this->assertEquals(
104+
new MessageCatalogue('en', [
105+
'messages' => ['a' => 'old_a'],
106+
'messages+intl-icu' => ['b' => 'new_b'],
107+
]),
108+
$this->createOperation(
109+
new MessageCatalogue('en', ['messages' => ['a' => 'old_a']]),
110+
new MessageCatalogue('en', ['messages+intl-icu' => ['a' => 'new_a', 'b' => 'new_b']])
111+
)->getResult()
112+
);
81113
}
82114

83115
public function testGetResultWithMetadata()

0 commit comments

Comments
 (0)