8000 Add choice_translation_parameters option · symfony/symfony-docs@0470b00 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0470b00

Browse files
Add choice_translation_parameters option
1 parent d848edd commit 0470b00

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

reference/forms/types/choice.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op
1919
| | - `choice_loader`_ |
2020
| | - `choice_name`_ |
2121
| | - `choice_translation_domain`_ |
22+
| | - `choice_translation_parameters`_ |
2223
| | - `choice_value`_ |
2324
| | - `expanded`_ |
2425
| | - `group_by`_ |
@@ -232,6 +233,8 @@ correct types will be assigned to the model.
232233

233234
.. include:: /reference/forms/types/options/choice_translation_domain_enabled.rst.inc
234235

236+
.. include:: /reference/forms/types/options/choice_translation_parameters.rst.inc
237+
235238
.. include:: /reference/forms/types/options/choice_value.rst.inc
236239

237240
.. include:: /reference/forms/types/options/expanded.rst.inc
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
.. configuration-block::
16+
17+
.. code-block:: yaml
18+
19+
# translations/messages.en.yaml
20+
form.order.yes: 'I confirm my order to the company %company%'
21+
form.order.no: 'I cancel my order'
22+
23+
.. code-block:: xml
24+
25+
<!-- translations/messages.en.xlf -->
26+
<?xml version="1.0"?>
27+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
28+
<file source-language="en" datatype="plaintext" original="file.ext">
29+
<body>
30+
<trans-unit id="form.order.yes">
31+
<source>form.order.yes</source>
32+
<target>I confirm my order to the company %company%</target>
33+
</trans-unit>
34+
<trans-unit id="form.order.no">
35+
<source>form.order.no</source>
36+
<target>I cancel my order</target>
37+
</trans-unit>
38+
</body>
39+
</file>
40+
</xliff>
41+
42+
.. code-block:: php
43+
44+
// translations/messages.fr.php
45+
return [
46+
'form.order.yes' => "I confirm my order to the company %company%",
47+
'form.order.no' => "I cancel my order",
48+
];
49+
50+
You can specify the placeholder values as follows::
51+
52+
$builder->add('id', null, [
53+
'choice' => [
54+
'form.order.yes' => true,
55+
'form.order.no' => false,
56+
],
57+
'choice_translation_parameters' => function ($choice, $key, $value) {
58+
if (false === $choice) {
59+
return [];
60+
}
61+
62+
return ['%company%' => 'ACME Inc.']
63+
},
64+
]);
65+
66+
If an array, the keys of the ``choices`` array must be used as keys::
67+
68+
$builder->add('id', null, [
69+
'choice' => [
70+
'form.order.yes' => true,
71+
'form.order.no' => false,
72+
],
73+
'choice_translation_parameters' => [
74+
'form.order.yes' => ['%company%' => 'ACME Inc.'],
75+
'form.order.no' => [],
76+
],
77+
]);
78+
79+
The translation parameters of child fields are merged with the same option of
80+
their parents, so children can reuse and/or override any of the parent placeholders.

0 commit comments

Comments
 (0)
0