8000 bug #35430 [Translation] prefer intl domain when adding messages to c… · symfony/symfony@7dc5d64 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7dc5d64

Browse files
committed
bug #35430 [Translation] prefer intl domain when adding messages to catalogue (Guite)
This PR was squashed before being merged into the 4.4 branch (closes #35430). Discussion ---------- [Translation] prefer intl domain when adding messages to catalogue | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | This PR ensures that when adding translations to a catalogue using the `add(array $messages, string $domain = 'messages')` method internally the intl icu domain is checked first. Otherwise it could happen that existing messages in e.g. `messages+intl-icu` are not updated but the same keys are added to `messages`. This is a follow-up of #35370, now targeting the `4.4` branch. Commits ------- b72b7d3 [Translation] prefer intl domain when adding messages to catalogue
2 parents a4544c2 + b72b7d3 commit 7dc5d64

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/Symfony/Component/Translation/MessageCatalogue.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,17 @@ public function replace($messages, $domain = 'messages')
158158
public function add($messages, $domain = 'messages')
159159
{
160160
if (!isset($this->messages[$domain])) {
161-
$this->messages[$domain] = $messages;
162-
} else {
163-
foreach ($messages as $id => $message) {
161+
$this->messages[$domain] = [];
162+
}
163+
$intlDomain = $domain;
164+
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
165+
if (\strlen($domain) > $suffixLength && false !== strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
166+
$intlDomain .= self::INTL_DOMAIN_SUFFIX;
167+
}
168+
foreach ($messages as $id => $message) {
169+
if (isset($this->messages[$intlDomain]) && \array_key_exists($id, $this->messages[$intlDomain])) {
170+
$this->messages[$intlDomain][$id] = $message;
171+
} else {
164172
$this->messages[$domain][$id] = $message;
165173
}
166174
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ public function testGetResultFromIntlDomain()
6767
);
6868
}
6969

70+
public function testGetResultWithMixedDomains()
71+
{
72+
$this->assertEquals(
73+
new MessageCatalogue('en', [
74+
'messages+intl-icu' => ['a' => 'old_a'],
75+
]),
76+
$this->createOperation(
77+
new MessageCatalogue('en', ['messages' => ['a' => 'old_a']]),
78+
new MessageCatalogue('en', ['messages+intl-icu' => ['a' => 'new_a']])
79+
)->getResult()
80+
);
81+
}
82+
7083
public function testGetResultWithMetadata()
7184
{
7285
$leftCatalogue = new MessageCatalogue('en', ['messages' => ['a' => 'old_a', 'b' => 'old_b']]);

0 commit comments

Comments
 (0)
0