8000 [Translation] Translatable parameters · symfony/symfony@7a0b2fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a0b2fc

Browse files
committed
[Translation] Translatable parameters
1 parent b2e7fcd commit 7a0b2fc

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
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

Lines changed: 12 additions & 7 deletions
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,14 +468,18 @@ public function getTransFileTests()
467468
];
468469
}
469470

470-
public function getTransTests()
471+
public function getTransTests(): iterable
471472
{
472-
return [
473-
['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''],
474-
['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''],
475-
['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''],
476-
['', null, '', [], 'fr', ''],
477-
];
473+
yield ['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''];
474+
475+
yield ['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''];
476+
477+
$param = new TranslatableMessage('Symfony is %what%!', ['%what%' => 'awesome'], '');
478+
yield ['Symfony est Symfony est awesome ! !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => $param], 'fr', ''];
479+
480+
yield ['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''];
481+
482+
yield ['', null, '', [], 'fr', ''];
478483
}
479484

480485
public function getTransICUTests()

src/Symfony/Component/Translation/Translator.php

Lines changed: 9 additions & 0 deletions
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,14 @@ public function trans(?string $id, array $parameters = [], string $domain = null
194195
}
195196
}
196197

198+
$parameters = array_map(function ($parameter) use ($locale) {
199+
if ($parameter instanceof TranslatableInterface) {
200+
return $parameter->trans($this, $locale);
201+
}
202+
203+
return $parameter;
204+
}, $parameters);
205+
197206
$len = \strlen(MessageCatalogue::INTL_DOMAIN_SUFFIX);
198207
if ($this->hasIntlFormatter
199208
&& ($catalogue->defines($id, $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX)

0 commit comments

Comments
 (0)
0