Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 261c3de

Browse filesBrowse files
Add choice_translation_parameters option
1 parent ec43b0c commit 261c3de
Copy full SHA for 261c3de

File tree

2 files changed

+55
-0
lines changed
Filter options

2 files changed

+55
-0
lines changed

‎reference/forms/types/choice.rst

Copy file name to clipboardExpand all lines: reference/forms/types/choice.rst
+3Lines 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`_ |
@@ -225,6 +226,8 @@ correct types will be assigned to the model.
225226

226227
.. include:: /reference/forms/types/options/choice_name.rst.inc
227228

229+
.. include:: /reference/forms/types/options/choice_translation_parameters.rst.inc
230+
228231
.. include:: /reference/forms/types/options/choice_translation_domain.rst.inc
229232

230233
.. include:: /reference/forms/types/options/choice_value.rst.inc
+52Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.