Skip to content

[Form] Add choice_translation_parameters option #14699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions reference/forms/types/choice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ To use this field, you must specify *either* ``choices`` or ``choice_loader`` op
| | - `choice_loader`_ |
| | - `choice_name`_ |
| | - `choice_translation_domain`_ |
| | - `choice_translation_parameters`_ |
| | - `choice_value`_ |
| | - `expanded`_ |
| | - `group_by`_ |
Expand Down Expand Up @@ -232,6 +233,8 @@ correct types will be assigned to the model.

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

.. include:: /reference/forms/types/options/choice_translation_parameters.rst.inc

.. include:: /reference/forms/types/options/choice_value.rst.inc

.. include:: /reference/forms/types/options/expanded.rst.inc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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 <component-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:

.. configuration-block::

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

.. code-block:: xml

<!-- translations/messages.en.xlf -->
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="form.order.yes">
<source>form.order.yes</source>
<target>I confirm my order to the company %company%</target>
</trans-unit>
<trans-unit id="form.order.no">
<source>form.order.no</source>
<target>I cancel my order</target>
</trans-unit>
</body>
</file>
</xliff>

.. code-block:: php

// translations/messages.fr.php
return [
'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 translation parameters of child fields are merged with the same option of
their parents, so children can reuse and/or override any of the parent placeholders.