Skip to content

Commit

Permalink
bug #35430 [Translation] prefer intl domain when adding messages to c…
Browse files Browse the repository at this point in the history
…atalogue (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
  • Loading branch information
fabpot committed Feb 3, 2020
2 parents a4544c2 + b72b7d3 commit 7dc5d64
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Symfony/Component/Translation/MessageCatalogue.php
Expand Up @@ -158,9 +158,17 @@ public function replace($messages, $domain = 'messages')
public function add($messages, $domain = 'messages')
{
if (!isset($this->messages[$domain])) {
$this->messages[$domain] = $messages;
} else {
foreach ($messages as $id => $message) {
$this->messages[$domain] = [];
}
$intlDomain = $domain;
$suffixLength = \strlen(self::INTL_DOMAIN_SUFFIX);
if (\strlen($domain) > $suffixLength && false !== strpos($domain, self::INTL_DOMAIN_SUFFIX, -$suffixLength)) {
$intlDomain .= self::INTL_DOMAIN_SUFFIX;
}
foreach ($messages as $id => $message) {
if (isset($this->messages[$intlDomain]) && \array_key_exists($id, $this->messages[$intlDomain])) {

This comment has been minimized.

Copy link
@kirya-dev

kirya-dev Mar 24, 2020

Why not isset($this->messages[$intlDomain][$id] ?

$this->messages[$intlDomain][$id] = $message;
} else {
$this->messages[$domain][$id] = $message;
}
}
Expand Down
Expand Up @@ -67,6 +67,19 @@ public function testGetResultFromIntlDomain()
);
}

public function testGetResultWithMixedDomains()
{
$this->assertEquals(
new MessageCatalogue('en', [
'messages+intl-icu' => ['a' => 'old_a'],
]),
$this->createOperation(
new MessageCatalogue('en', ['messages' => ['a' => 'old_a']]),
new MessageCatalogue('en', ['messages+intl-icu' => ['a' => 'new_a']])
)->getResult()
);
}

public function testGetResultWithMetadata()
{
$leftCatalogue = new MessageCatalogue('en', ['messages' => ['a' => 'old_a', 'b' => 'old_b']]);
Expand Down

0 comments on commit 7dc5d64

Please sign in to comment.