Skip to content

Commit

Permalink
Fix incorrect locale usage when message ID is not exist (#132)
Browse files Browse the repository at this point in the history
* Fix incorrect locale usage when message ID is not exist

* fix

* revert

* fix
  • Loading branch information
vjik authored Feb 25, 2024
1 parent be26af7 commit 745a810
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.0.1 under development

- Enh #131: Throw `InvalidArgumentException` when missed "one" plural key (@vjik)
- Bug #132: Fix incorrect locale usage when category source is not exist and specified fallback locale (@vjik)

## 3.0.0 February 17, 2023

Expand Down
2 changes: 1 addition & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function translate(

if (empty($this->categorySources[$category])) {
$this->dispatchMissingTranslationCategoryEvent($category);
return $this->defaultMessageFormatter->format((string) $id, $parameters, $locale);
return $this->defaultMessageFormatter->format((string) $id, $parameters, $this->fallbackLocale ?? $locale);
}

return $this->translateUsingCategorySources((string) $id, $parameters, $category, $locale);
Expand Down
37 changes: 37 additions & 0 deletions tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,43 @@ public function format(string $message, array $parameters, string $locale): stri
);
}

public static function dataDefaultMessageFormatterWithoutCategory(): array
{
return [
'another-fallback-locale' => [
'(en)',
'en',
],
'same-fallback-locale' => [
'(ru)',
'ru',
],
'without-fallback-locale' => [
'(ru)',
null,
],
];
}

/**
* @dataProvider dataDefaultMessageFormatterWithoutCategory
*/
public function testDefaultMessageFormatterWithoutCategory(string $expected, ?string $fallbackLocale)
{
$translator = new Translator(
locale: 'ru',
fallbackLocale: $fallbackLocale,
defaultMessageFormatter: new class () implements MessageFormatterInterface {
public function format(string $message, array $parameters, string $locale): string
{
return '(' . $locale . ')';
}
},
);

$this->assertSame($expected, $translator->translate('test'));
}

public function testFluentInterface(): void
{
$translator = new Translator();
Expand Down

0 comments on commit 745a810

Please sign in to comment.