From af947fefc306cec6ea5a1f6160c7e305a71f2493 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 20 Jul 2022 11:29:12 +0200 Subject: [PATCH 1/2] Fix CS --- DataCollector/TranslationDataCollector.php | 2 +- Extractor/PhpExtractor.php | 2 +- PluralizationRules.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DataCollector/TranslationDataCollector.php b/DataCollector/TranslationDataCollector.php index e4f0b3a5..88894ec0 100644 --- a/DataCollector/TranslationDataCollector.php +++ b/DataCollector/TranslationDataCollector.php @@ -50,7 +50,7 @@ public function lateCollect() * * @param \Throwable|null $exception */ - public function collect(Request $request, Response $response/*, \Throwable $exception = null*/) + public function collect(Request $request, Response $response/* , \Throwable $exception = null */) { $this->data['locale'] = $this->translator->getLocale(); $this->data['fallback_locales'] = $this->translator->getFallbackLocales(); diff --git a/Extractor/PhpExtractor.php b/Extractor/PhpExtractor.php index 32389c67..e0622e6a 100644 --- a/Extractor/PhpExtractor.php +++ b/Extractor/PhpExtractor.php @@ -211,7 +211,7 @@ private function getValue(\Iterator $tokenIterator) * @param array $tokens * @param string $filename */ - protected function parseTokens($tokens, MessageCatalogue $catalog/*, string $filename*/) + protected function parseTokens($tokens, MessageCatalogue $catalog/* , string $filename */) { if (\func_num_args() < 3 && __CLASS__ !== static::class && __CLASS__ !== (new \ReflectionMethod($this, __FUNCTION__))->getDeclaringClass()->getName() && !$this instanceof \PHPUnit\Framework\MockObject\MockObject && !$this instanceof \Prophecy\Prophecy\ProphecySubjectInterface && !$this instanceof \Mockery\MockInterface) { @trigger_error(sprintf('The "%s()" method will have a new "string $filename" argument in version 5.0, not defining it is deprecated since Symfony 4.3.', __METHOD__), \E_USER_DEPRECATED); diff --git a/PluralizationRules.php b/PluralizationRules.php index e69ceabc..84513a24 100644 --- a/PluralizationRules.php +++ b/PluralizationRules.php @@ -30,7 +30,7 @@ class PluralizationRules * * @return int The plural position */ - public static function get($number, $locale/*, bool $triggerDeprecation = true*/) + public static function get($number, $locale/* , bool $triggerDeprecation = true */) { $number = abs($number); From 4e6b4c0dbeb04d6f004ed7f43eb0905ce8396def Mon Sep 17 00:00:00 2001 From: Xavier RENAUDIN Date: Wed, 27 Apr 2022 14:54:44 +0200 Subject: [PATCH 2/2] [Translator] Fix translator overlapse --- MessageCatalogue.php | 19 +++++++------------ Tests/TranslatorTest.php | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/MessageCatalogue.php b/MessageCatalogue.php index 6e6b9fe0..b43b22d6 100644 --- a/MessageCatalogue.php +++ b/MessageCatalogue.php @@ -159,19 +159,14 @@ public function replace($messages, $domain = 'messages') */ public function add($messages, $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/TranslatorTest.php b/Tests/TranslatorTest.php index 073f2255..6c9bc7a1 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\Translator; @@ -683,6 +687,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')); + } + /** * @group legacy */