|
| 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