Skip to content

Commit 9ed19d9

Browse files
7-zete-7nicolas-grekas
authored andcommitted
[TwigBridge] Fixed a parameterized choice label translation
1 parent ae0cec0 commit 9ed19d9

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\FormError;
2020
use Symfony\Component\Form\FormRenderer;
2121
use Symfony\Component\Form\FormView;
22+
use Symfony\Contracts\Translation\TranslatableInterface;
2223
use Symfony\Contracts\Translation\TranslatorInterface;
2324
use Twig\Extension\AbstractExtension;
2425
use Twig\TwigFilter;
@@ -149,23 +150,26 @@ public function getFieldChoices(FormView $view): iterable
149150
private function createFieldChoicesList(iterable $choices, string|false|null $translationDomain): iterable
150151
{
151152
foreach ($choices as $choice) {
152-
$translatableLabel = $this->createFieldTranslation($choice->label, [], $translationDomain);
153-
154153
if ($choice instanceof ChoiceGroupView) {
154+
$translatableLabel = $this->createFieldTranslation($choice->label, [], $translationDomain);
155155
yield $translatableLabel => $this->createFieldChoicesList($choice, $translationDomain);
156156

157157
continue;
158158
}
159159

160160
/* @var ChoiceView $choice */
161+
$translatableLabel = $this->createFieldTranslation($choice->label, $choice->labelTranslationParameters, $translationDomain);
161162
yield $translatableLabel => $choice->value;
162163
}
163164
}
164165

165-
private function createFieldTranslation(?string $value, array $parameters, string|false|null $domain): ?string
166+
private function createFieldTranslation(TranslatableInterface|string|null $value, array $parameters, string|false|null $domain): ?string
166167
{
167168
if (!$this->translator || !$value || false === $domain) {
168-
return $value;
169+
return null !== $value ? (string) $value : null;
170+
}
171+
if ($value instanceof TranslatableInterface) {
172+
return $value->trans($this->translator);
169173
}
170174

171175
return $this->translator->trans($value, $parameters, $domain);

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionFieldHelpersTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Form\FormError;
2020
use Symfony\Component\Form\FormView;
2121
use Symfony\Component\Form\Test\FormIntegrationTestCase;
22+
use Symfony\Component\Translation\TranslatableMessage;
2223

2324
class FormExtensionFieldHelpersTest extends FormIntegrationTestCase
2425
{
@@ -81,6 +82,28 @@ protected function setUp(): void
8182
'expanded' => true,
8283
'label' => false,
8384
])
85+
->add('parametrized_choice_label', ChoiceType::class, [
86+
'choices' => [
87+
(object) ['value' => 'yes', 'label' => 'parametrized.%yes%'],
88+
(object) ['value' => 'no', 'label' => 'parametrized.%no%'],
89+
],
90+
'choice_value' => 'value',
91+
'choice_label' => 'label',
92+
'choice_translation_domain' => 'forms',
93+
'choice_translation_parameters' => [
94+
['%yes%' => 'YES'],
95+
['%no%' => 'NO'],
96+
],
97+
])
98+
->add('translatable_choice_label', ChoiceType::class, [
99+
'choices' => [
100+
'yes',
101+
'no',
102+
],
103+
'choice_label' => static function (string $choice) {
104+
return new TranslatableMessage('parametrized.%value%', ['%value%' => $choice], 'forms');
105+
},
106+
])
84107
->getForm()
85108
;
86109

@@ -290,4 +313,40 @@ public function testFieldTranslatedChoicesMultiple()
290313
$this->assertSame('salt', $choicesArray[1]['value']);
291314
$this->assertSame('[trans]base.salt[/trans]', $choicesArray[1]['label']);
292315
}
316+
317+
public function testChoiceParametrizedLabel()
318+
{
319+
$choices = $this->translatorExtension->getFieldChoices($this->view->children['parametrized_choice_label']);
320+
321+
$choicesArray = [];
322+
foreach ($choices as $label => $value) {
323+
$choicesArray[] = ['label' => $label, 'value' => $value];
324+
}
325+
326+
$this->assertCount(2, $choicesArray);
327+
328+
$this->assertSame('yes', $choicesArray[0]['value']);
329+
$this->assertSame('[trans]parametrized.YES[/trans]', $choicesArray[0]['label']);
330+
331+
$this->assertSame('no', $choicesArray[1]['value']);
332+
$this->assertSame('[trans]parametrized.NO[/trans]', $choicesArray[1]['label']);
333+
}
334+
335+
public function testChoiceTranslatableLabel()
336+
{
337+
$choices = $this->translatorExtension->getFieldChoices($this->view->children['translatable_choice_label']);
338+
339+
$choicesArray = [];
340+
foreach ($choices as $label => $value) {
341+
$choicesArray[] = ['label' => $label, 'value' => $value];
342+
}
343+
344+
$this->assertCount(2, $choicesArray);
345+
346+
$this->assertSame('yes', $choicesArray[0]['value']);
347+
$this->assertSame('[trans]parametrized.yes[/trans]', $choicesArray[0]['label']);
348+
349+
$this->assertSame('no', $choicesArray[1]['value']);
350+
$this->assertSame('[trans]parametrized.no[/trans]', $choicesArray[1]['label']);
351+
}
293352
}

0 commit comments

Comments
 (0)