From 261c3de306e02c916d3f4fb6e3b48dbe1cd98f91 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 18 May 2020 11:53:54 +0200 Subject: [PATCH] Add choice_translation_parameters option --- reference/forms/types/choice.rst | 3 ++ .../choice_translation_parameters.rst.inc | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 reference/forms/types/options/choice_translation_parameters.rst.inc diff --git a/reference/forms/types/choice.rst b/reference/forms/types/choice.rst index fb1df969b9d..27a8ffe788d 100644 --- a/reference/forms/types/choice.rst +++ b/reference/forms/types/choice.rst @@ -18,6 +18,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op | | - `choice_label`_ | | | - `choice_loader`_ | | | - `choice_name`_ | +| | - `choice_translation_parameters`_ | | | - `choice_translation_domain`_ | | | - `choice_value`_ | | | - `expanded`_ | @@ -225,6 +226,8 @@ correct types will be assigned to the model. .. include:: /reference/forms/types/options/choice_name.rst.inc +.. include:: /reference/forms/types/options/choice_translation_parameters.rst.inc + .. include:: /reference/forms/types/options/choice_translation_domain.rst.inc .. include:: /reference/forms/types/options/choice_value.rst.inc diff --git a/reference/forms/types/options/choice_translation_parameters.rst.inc b/reference/forms/types/options/choice_translation_parameters.rst.inc new file mode 100644 index 00000000000..3a035abaaee --- /dev/null +++ b/reference/forms/types/options/choice_translation_parameters.rst.inc @@ -0,0 +1,52 @@ +choice_translation_parameters +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**type**: ``array``, ``callable``, ``string`` or :class:`Symfony\\Component\\PropertyAccess\\PropertyPath` **default**: ``[]`` + +The choice values are translated before displaying it, so it can contain +:ref:`translation placeholders `. +This option defines the values used to replace those placeholders. This can be +an associative array where the keys match the choice keys and the values +are the attributes for each choice, a callable or a property path +(just like `choice_label`_). + +Given this translation message: + +.. code-block:: yaml + + # translations/messages.en.yaml + form.order.yes: 'I confirm my order to the company %company%' + form.order.no: 'I cancel my order' + +You can specify the placeholder values as follows:: + + $builder->add('id', null, [ + 'choice' => [ + 'form.order.yes' => true, + 'form.order.no' => false, + ], + 'choice_translation_parameters' => function ($choice, $key, $value) { + if (false === $choice) { + return []; + } + + return ['%company%' => 'ACME Inc.'] + }, + ]); + +If an array, the keys of the ``choices`` array must be used as keys:: + + $builder->add('id', null, [ + 'choice' => [ + 'form.order.yes' => true, + 'form.order.no' => false, + ], + 'choice_translation_parameters' => [ + 'form.order.yes' => ['%company%' => 'ACME Inc.'], + 'form.order.no' => [], + ], + ]); + +The ``choice_translation_parameters`` option of children fields is merged with +the same option of their parents, so children can reuse and/or override any of +the parent placeholders.