Skip to content

Commit e5ea631

Browse files
committed
feature #44672 [Translation] Translatable parameters (sylfabre)
This PR was merged into the 6.1 branch. Discussion ---------- [Translation] Translatable parameters | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | TBD ✅ The TwigExtension now supports messages implementing `TranslatableInterface` (https://symfony.com/blog/new-in-symfony-5-2-translatable-objects). ✅ Thanks to #41858, `TranslatableMessage` now supports recursive `TranslatableInterface` as params. ❌ But using `TranslatableInterface` as params with regular messages is not supported yet. 💡 This PR addresses this issue and makes it possible to create dedicated `TranslatableInterface` implementations like the one from #41136 _Note: I would like also to add `TranslatableInterface` to the `trans(?string $id)` signature method like with the one from the [TwigExtension](https://github.com/symfony/symfony/blob/6.1/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php#L111) but I don't know how to do it without breaking BC. Any advice is welcome!_ Commits ------- 5beaee8 [Translation] Translatable parameters
2 parents 1dad23b + 5beaee8 commit e5ea631

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Parameters implementing `TranslatableInterface` are processed
8+
49
5.4
510
---
611

src/Symfony/Component/Translation/Tests/TranslatorTest.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Translation\Exception\RuntimeException;
1818
use Symfony\Component\Translation\Loader\ArrayLoader;
1919
use Symfony\Component\Translation\MessageCatalogue;
20+
use Symfony\Component\Translation\TranslatableMessage;
2021
use Symfony\Component\Translation\Translator;
2122

2223
class TranslatorTest extends TestCase
@@ -467,11 +468,14 @@ public function getTransFileTests()
467468
];
468469
}
469470

470-
public function getTransTests()
471+
public function getTransTests(): iterable
471472
{
473+
$param = new TranslatableMessage('Symfony is %what%!', ['%what%' => 'awesome'], '');
474+
472475
return [
473476
['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''],
474477
['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''],
478+
['Symfony est Symfony est awesome ! !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => $param], 'fr', ''],
475479
['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''],
476480
['', null, '', [], 'fr', ''],
477481
];

src/Symfony/Component/Translation/Translator.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;
2323
use Symfony\Component\Translation\Loader\LoaderInterface;
2424
use Symfony\Contracts\Translation\LocaleAwareInterface;
25+
use Symfony\Contracts\Translation\TranslatableInterface;
2526
use Symfony\Contracts\Translation\TranslatorInterface;
2627

2728
// Help opcache.preload discover always-needed symbols
@@ -194,6 +195,10 @@ public function trans(?string $id, array $parameters = [], string $domain = null
194195
}
195196
}
196197

198+
$parameters = array_map(function ($parameter) use ($locale) {
199+
return $parameter instanceof TranslatableInterface ? $parameter->trans($this, $locale) : $parameter;
200+
}, $parameters);
201+
197202
$len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
198203
if ($this->hasIntlFormatter
199204
&& ($catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)

0 commit comments

Comments
 (0)