Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Feb 22, 2024
1 parent 5711afc commit f812ccb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 79 deletions.
25 changes: 19 additions & 6 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -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
Expand Down
80 changes: 7 additions & 73 deletions tests/TranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public function dataDefaultMessageFormatterWithCategory(): array
{
return [
'with formatter' => [
'formatted by category',
'formatted by category (ru)',
new CategorySource(
'withFormatter',
$this->createMessageReader(
Expand All @@ -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(
Expand All @@ -646,86 +646,20 @@ 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 . ')';
}
},
);
$translator->addCategorySources($categorySource);

$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())
);
}

Expand Down

0 comments on commit f812ccb

Please sign in to comment.