diff --git a/MessageCatalogue.php b/MessageCatalogue.php index 09153d12..4b8fc58d 100644 --- a/MessageCatalogue.php +++ b/MessageCatalogue.php @@ -156,19 +156,14 @@ public function replace(array $messages, string $domain = 'messages') */ public function add(array $messages, string $domain = 'messages') { - if (!isset($this->messages[$domain])) { - $this->messages[$domain] = []; - } - $intlDomain = $domain; - if (!str_ends_with($domain, self::INTL_DOMAIN_SUFFIX)) { - $intlDomain .= self::INTL_DOMAIN_SUFFIX; - } + $altDomain = str_ends_with($domain, self::INTL_DOMAIN_SUFFIX) ? substr($domain, 0, -\strlen(self::INTL_DOMAIN_SUFFIX)) : $domain.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; - } + unset($this->messages[$altDomain][$id]); + $this->messages[$domain][$id] = $message; + } + + if ([] === ($this->messages[$altDomain] ?? null)) { + unset($this->messages[$altDomain]); } } diff --git a/Tests/Catalogue/MergeOperationTest.php b/Tests/Catalogue/MergeOperationTest.php index 3f21abac..1c9bcbc2 100644 --- a/Tests/Catalogue/MergeOperationTest.php +++ b/Tests/Catalogue/MergeOperationTest.php @@ -57,7 +57,7 @@ public function testGetResultFromIntlDomain() { $this->assertEquals( new MessageCatalogue('en', [ - 'messages' => ['a' => 'old_a', 'b' => 'old_b'], + 'messages' => ['b' => 'old_b'], 'messages+intl-icu' => ['d' => 'old_d', 'c' => 'new_c', 'a' => 'new_a'], ]), $this->createOperation( diff --git a/Tests/Catalogue/TargetOperationTest.php b/Tests/Catalogue/TargetOperationTest.php index 2b63cd41..6f4de858 100644 --- a/Tests/Catalogue/TargetOperationTest.php +++ b/Tests/Catalogue/TargetOperationTest.php @@ -71,7 +71,6 @@ public function testGetResultWithMixedDomains() { $this->assertEquals( new MessageCatalogue('en', [ - 'messages' => ['a' => 'old_a'], 'messages+intl-icu' => ['a' => 'new_a'], ]), $this->createOperation( @@ -103,7 +102,6 @@ public function testGetResultWithMixedDomains() $this->assertEquals( new MessageCatalogue('en', [ - 'messages' => ['a' => 'old_a'], 'messages+intl-icu' => ['b' => 'new_b', 'a' => 'new_a'], ]), $this->createOperation( diff --git a/Tests/TranslatorTest.php b/Tests/TranslatorTest.php index 57f5456d..b5c332df 100644 --- a/Tests/TranslatorTest.php +++ b/Tests/TranslatorTest.php @@ -15,6 +15,10 @@ use Symfony\Component\Translation\Exception\InvalidArgumentException; use Symfony\Component\Translation\Exception\NotFoundResourceException; use Symfony\Component\Translation\Exception\RuntimeException; +use Symfony\Component\Translation\Formatter\IntlFormatter; +use Symfony\Component\Translation\Formatter\IntlFormatterInterface; +use Symfony\Component\Translation\Formatter\MessageFormatter; +use Symfony\Component\Translation\Formatter\MessageFormatterInterface; use Symfony\Component\Translation\Loader\ArrayLoader; use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\TranslatableMessage; @@ -563,6 +567,26 @@ public function testIntlFormattedDomain() $this->assertSame('Hi Bob', $translator->trans('some_message', ['%name%' => 'Bob'])); } + public function testIntlDomainOverlapseWithIntlResourceBefore() + { + $intlFormatterMock = $this->createMock(IntlFormatterInterface::class); + $intlFormatterMock->expects($this->once())->method('formatIntl')->with('hello intl', 'en', [])->willReturn('hello intl'); + + $messageFormatter = new MessageFormatter(null, $intlFormatterMock); + + $translator = new Translator('en', $messageFormatter); + $translator->addLoader('array', new ArrayLoader()); + + $translator->addResource('array', ['some_message' => 'hello intl'], 'en', 'messages+intl-icu'); + $translator->addResource('array', ['some_message' => 'hello'], 'en', 'messages'); + + $this->assertSame('hello', $translator->trans('some_message', [], 'messages')); + + $translator->addResource('array', ['some_message' => 'hello intl'], 'en', 'messages+intl-icu'); + + $this->assertSame('hello intl', $translator->trans('some_message', [], 'messages')); + } + public function testMissingLoaderForResourceError() { $this->expectException(RuntimeException::class);