Skip to content

Commit 7b44a00

Browse files
Add choice_translation_parameters option
1 parent 625b789 commit 7b44a00

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

reference/forms/types/choice.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op
1818
| | - `choice_label`_ |
1919
| | - `choice_loader`_ |
2020
| | - `choice_name`_ |
21+
| | - `choice_translation_parameters`_ |
2122
| | - `choice_translation_domain`_ |
2223
| | - `choice_value`_ |
2324
| | - `expanded`_ |
@@ -230,6 +231,8 @@ correct types will be assigned to the model.
230231

231232
.. include:: /reference/forms/types/options/choice_name.rst.inc
232233

234+
.. include:: /reference/forms/types/options/choice_translation_parameters.rst.inc
235+
233236
.. include:: /reference/forms/types/options/choice_translation_domain_enabled.rst.inc
234237

235238
.. include:: /reference/forms/types/options/choice_value.rst.inc
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
choice_translation_parameters
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
**type**: ``array``, ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``[]``
5+
6+
The choice values are translated before displaying it, so it can contain
7+
:ref:`translation placeholders <component-translation-placeholders>`.
8+
This option defines the values used to replace those placeholders. This can be
9+
an associative array where the keys match the choice keys and the values
10+
are the attributes for each choice, a callable or a property path
11+
(just like `choice_label`_).
12+
13+
Given this translation message:
14+
15+
.. code-block:: yaml
16+
17+
# translations/messages.en.yaml
18+
form.order.yes: 'I confirm my order to the company %company%'
19+
form.order.no: 'I cancel my order'
20+
21+
You can specify the placeholder values as follows::
22+
23+
$builder->add('id', null, [
24+
'choice' => [
25+
'form.order.yes' => true,
26+
'form.order.no' => false,
27+
],
28+
'choice_translation_parameters' => function ($choice, $key, $value) {
29+
if (false === $choice) {
30+
return [];
31+
}
32+
33+
return ['%company%' => 'ACME Inc.']
34+
},
35+
]);
36+
37+
If an array, the keys of the ``choices`` array must be used as keys::
38+
39+
$builder->add('id', null, [
40+
'choice' => [
41+
'form.order.yes' => true,
42+
'form.order.no' => false,
43+
],
44+
'choice_translation_parameters' => [
45+
'form.order.yes' => ['%company%' => 'ACME Inc.'],
46+
'form.order.no' => [],
47+
],
48+
]);
49+
50+
The ``choice_translation_parameters`` option of children fields is merged with
51+
the same option of their parents, so children can reuse and/or override any of
52+
the parent placeholders.

0 commit comments

Comments
 (0)