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