Skip to content

Commit 4acd05c

Browse files
bug #61111 [Translation] fix support of TranslatableInterface in IdentityTranslator (VincentLanglet)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [Translation] fix support of `TranslatableInterface` in `IdentityTranslator` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- if yes, also update src/**/CHANGELOG.md --> | Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md --> | Issues | Fix #... <!-- prefix each issue number with "Fix #"; no need to create an issue if none exists, explain below --> | License | MIT Commits ------- 92ae810 [Translation] fix support of `TranslatableInterface` in `IdentityTranslator`
2 parents 5d66cca + 92ae810 commit 4acd05c

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

src/Symfony/Bridge/Twig/Tests/Extension/Fixtures/StubTranslator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111

1212
namespace Symfony\Bridge\Twig\Tests\Extension\Fixtures;
1313

14+
use Symfony\Contracts\Translation\TranslatableInterface;
1415
use Symfony\Contracts\Translation\TranslatorInterface;
1516

1617
class StubTranslator implements TranslatorInterface
1718
{
1819
public function trans($id, array $parameters = [], $domain = null, $locale = null): string
1920
{
21+
foreach ($parameters as $k => $v) {
22+
if ($v instanceof TranslatableInterface) {
23+
$parameters[$k] = $v->trans($this, $locale);
24+
}
25+
}
26+
2027
return '[trans]'.strtr($id, $parameters).'[/trans]';
2128
}
2229

src/Symfony/Component/Translation/TranslatableMessage.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@ public function getDomain(): ?string
5252

5353
public function trans(TranslatorInterface $translator, ?string $locale = null): string
5454
{
55-
return $translator->trans($this->getMessage(), array_map(
56-
static fn ($parameter) => $parameter instanceof TranslatableInterface ? $parameter->trans($translator, $locale) : $parameter,
57-
$this->getParameters()
58-
), $this->getDomain(), $locale);
55+
$parameters = $this->getParameters();
56+
foreach ($parameters as $k => $v) {
57+
if ($v instanceof TranslatableInterface) {
58+
$parameters[$k] = $v->trans($translator, $locale);
59+
}
60+
}
61+
62+
return $translator->trans($this->getMessage(), $parameters, $this->getDomain(), $locale);
5963
}
6064
}

src/Symfony/Contracts/Translation/Test/TranslatorTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Contracts\Translation\Test;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Translation\TranslatableMessage;
1516
use Symfony\Contracts\Translation\TranslatorInterface;
1617
use Symfony\Contracts\Translation\TranslatorTrait;
1718

@@ -116,10 +117,12 @@ public function testGetLocaleReturnsDefaultLocaleIfNotSet()
116117

117118
public static function getTransTests()
118119
{
119-
return [
120-
['Symfony is great!', 'Symfony is great!', []],
121-
['Symfony is awesome!', 'Symfony is %what%!', ['%what%' => 'awesome']],
122-
];
120+
yield ['Symfony is great!', 'Symfony is great!', []];
121+
yield ['Symfony is awesome!', 'Symfony is %what%!', ['%what%' => 'awesome']];
122+
123+
if (class_exists(TranslatableMessage::class)) {
124+
yield ['He said "Symfony is awesome!".', 'He said "%what%".', ['%what%' => new TranslatableMessage('Symfony is %what%!', ['%what%' => 'awesome'])]];
125+
}
123126
}
124127

125128
public static function getTransChoiceTests()

src/Symfony/Contracts/Translation/TranslatorTrait.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public function trans(?string $id, array $parameters = [], ?string $domain = nul
4141
return '';
4242
}
4343

44+
foreach ($parameters as $k => $v) {
45+
if ($v instanceof TranslatableInterface) {
46+
$parameters[$k] = $v->trans($this, $locale);
47+
}
48+
}
49+
4450
if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
4551
return strtr($id, $parameters);
4652
}

0 commit comments

Comments
 (0)