From f812ccb1b59a104896e226c3aa32d2350b262f27 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Thu, 22 Feb 2024 16:56:17 +0300 Subject: [PATCH] fix --- src/Translator.php | 25 ++++++++++--- tests/TranslatorTest.php | 80 ++++------------------------------------ 2 files changed, 26 insertions(+), 79 deletions(-) diff --git a/src/Translator.php b/src/Translator.php index 349b42f..4bbfd77 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -84,7 +84,7 @@ public function translate( return $this->defaultMessageFormatter->format((string) $id, $parameters, $locale); } - return $this->translateUsingCategorySources((string) $id, $parameters, $category, $locale); + return $this->translateUsingCategorySources((string) $id, $parameters, $category, $locale, $locale); } public function withDefaultCategory(string $category): static @@ -109,9 +109,11 @@ private function translateUsingCategorySources( string $id, array $parameters, string $category, - string $locale + string $locale, + string $defaultLocale, ): string { - $sourceCategory = end($this->categorySources[$category]); + $endSourceCategory = end($this->categorySources[$category]); + $sourceCategory = $endSourceCategory; do { $message = $sourceCategory->getMessage($id, $locale, $parameters); @@ -126,17 +128,28 @@ private function translateUsingCategorySources( $fallback = $localeObject->fallbackLocale(); if ($fallback->asString() !== $localeObject->asString()) { - return $this->translateUsingCategorySources($id, $parameters, $category, $fallback->asString()); + return $this->translateUsingCategorySources($id, $parameters, $category, $fallback->asString(), $locale); } if (!empty($this->fallbackLocale)) { $fallbackLocaleObject = (new Locale($this->fallbackLocale))->fallbackLocale(); if ($fallbackLocaleObject->asString() !== $localeObject->asString()) { - return $this->translateUsingCategorySources($id, $parameters, $category, $fallbackLocaleObject->asString()); + return $this->translateUsingCategorySources( + $id, + $parameters, + $category, + $fallbackLocaleObject->asString(), + $locale + ); } } - return $this->defaultMessageFormatter->format($id, $parameters, $locale); + return $endSourceCategory->format( + $id, + $parameters, + $defaultLocale, + $this->defaultMessageFormatter + ); } private function dispatchMissingTranslationCategoryEvent(string $category): void diff --git a/tests/TranslatorTest.php b/tests/TranslatorTest.php index b559ba3..70f8faf 100644 --- a/tests/TranslatorTest.php +++ b/tests/TranslatorTest.php @@ -598,7 +598,7 @@ public function dataDefaultMessageFormatterWithCategory(): array { return [ 'with formatter' => [ - 'formatted by category', + 'formatted by category (ru)', new CategorySource( 'withFormatter', $this->createMessageReader( @@ -614,13 +614,13 @@ public function dataDefaultMessageFormatterWithCategory(): array new class () implements MessageFormatterInterface { public function format(string $message, array $parameters, string $locale): string { - return 'formatted by category'; + return 'formatted by category (' . $locale . ')'; } }, ), ], 'without formatter' => [ - 'formatted by translator', + 'formatted by translator (ru)', new CategorySource( 'withoutFormatter', $this->createMessageReader( @@ -646,11 +646,12 @@ public function testDefaultMessageFormatterWithCategory( CategorySource $categorySource ): void { $translator = new Translator( - locale: 'en', + locale: 'ru', + fallbackLocale: 'en', defaultMessageFormatter: new class () implements MessageFormatterInterface { public function format(string $message, array $parameters, string $locale): string { - return 'formatted by translator'; + return 'formatted by translator (' . $locale . ')'; } }, ); @@ -658,74 +659,7 @@ public function format(string $message, array $parameters, string $locale): stri $this->assertSame( $expectedMessage, - $translator->translate('hello', [], $categorySource->getName()) - ); - } - - public function dataDefaultMessageFormatterWithoutId(): array - { - return [ - 'without category' => [null], - 'category with formatter' => [ - new CategorySource( - 'withFormatter', - $this->createMessageReader( - 'withFormatter', - [ - 'withFormatter' => [ - 'en' => [ - 'hello' => 'Hello, {name}!', - ], - ], - ] - ), - new class () implements MessageFormatterInterface { - public function format(string $message, array $parameters, string $locale): string - { - return 'formatted by category'; - } - }, - ), - ], - 'category without formatter' => [ - new CategorySource( - 'withoutFormatter', - $this->createMessageReader( - 'withoutFormatter', - [ - 'withoutFormatter' => [ - 'en' => [ - 'hello' => 'Hello, {name}!', - ], - ], - ] - ), - ), - ], - ]; - } - - /** - * @dataProvider dataDefaultMessageFormatterWithoutId - */ - public function testDefaultMessageFormatterWithoutId(?CategorySource $categorySource): void - { - $translator = new Translator( - locale: 'en', - defaultMessageFormatter: new class () implements MessageFormatterInterface { - public function format(string $message, array $parameters, string $locale): string - { - return 'formatted by translator'; - } - }, - ); - if ($categorySource !== null) { - $translator->addCategorySources($categorySource); - } - - $this->assertSame( - 'formatted by translator', - $translator->translate('non-exist-id', [], 'test-category') + $translator->translate('test', [], $categorySource->getName()) ); }