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
-------

b72b7d3413 [Translation] prefer intl domain when adding messages to catalogue
This commit is contained in:
Fabien Potencier 2020-02-03 17:51:48 +01:00
commit 7dc5d64b37
2 changed files with 24 additions and 3 deletions

View File

@ -158,9 +158,17 @@ class MessageCatalogue implements MessageCatalogueInterface, MetadataAwareInterf
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->messages[$intlDomain][$id] = $message;
} else {
$this->messages[$domain][$id] = $message;
}
}

View File

@ -67,6 +67,19 @@ class TargetOperationTest extends AbstractOperationTest
);
}
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']]);