Skip to content

Commit fb514b6

Browse files
committed
feature #13651 [2.7][Form][choice] added choice_translation_domain to avoid trans options. (aitboudad)
This PR was merged into the 2.7 branch. Discussion ---------- [2.7][Form][choice] added choice_translation_domain to avoid trans options. | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Fixed tickets | ~ | Tests pass? | yes | License | MIT see #12941 (comment) Commits ------- 5a33c2c [Form][choice] added choice_translation_domain to avoid trans options.
2 parents 04ad2a7 + 5a33c2c commit fb514b6

File tree

14 files changed

+228
-123
lines changed

14 files changed

+228
-123
lines changed

UPGRADE-2.7.md

Lines changed: 141 additions & 105 deletions
Large diffs are not rendered by default.

src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public function configureOptions(OptionsResolver $resolver)
292292
'choice_name' => $choiceName,
293293
'choice_value' => $choiceValue,
294294
'id_reader' => null, // internal
295+
'choice_translation_domain' => false,
295296
));
296297

297298
$resolver->setRequired(array('class'));

src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@
7474
{%- block choice_widget_options -%}
7575
{% for group_label, choice in options %}
7676
{%- if choice is iterable -%}
77-
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
77+
<optgroup label="{{ choice_translation_domain is sameas(false) ? group_label : group_label|trans({}, choice_translation_domain) }}">
7878
{% set options = choice %}
7979
{{- block('choice_widget_options') -}}
8080
</optgroup>
8181
{%- else -%}
8282
{% set attr = choice.attr %}
83-
<option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
83+
<option value="{{ choice.value }}" {{ block('attributes') }}{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice_translation_domain is sameas(false) ? choice.label : choice.label|trans({}, choice_translation_domain) }}</option>
8484
{%- endif -%}
8585
{% endfor %}
8686
{%- endblock choice_widget_options -%}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_options.html.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
$translatorHelper = $view['translator']; // outside of the loop for performance reasons! ?>
44
<?php $formHelper = $view['form']; ?>
5-
<?php foreach ($choices as $index => $choice): ?>
5+
<?php foreach ($choices as $group_label => $choice): ?>
66
<?php if (is_array($choice) || $choice instanceof ChoiceGroupView): ?>
7-
<optgroup label="<?php echo $view->escape($translatorHelper->trans($index, array(), $translation_domain)) ?>">
7+
<optgroup label="<?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($group_label, array(), $choice_translation_domain) : $group_label) ?>">
88
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?>
99
</optgroup>
1010
<?php else: ?>
11-
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option>
11+
<option value="<?php echo $view->escape($choice->value) ?>" <?php echo $view['form']->block($form, 'attributes', array('attr' => $choice->attr)) ?><?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape(false !== $choice_translation_domain ? $translatorHelper->trans($choice->label, array(), $choice_translation_domain) : $choice->label) ?></option>
1212
<?php endif ?>
1313
<?php endforeach ?>

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.7.0
55
-----
66

7+
* added option "choice_translation_domain" to ChoiceType.
78
* deprecated option "precision" in favor of "scale"
89
* deprecated the overwriting of AbstractType::setDefaultOptions() in favor of overwriting AbstractType::configureOptions().
910
* deprecated the overwriting of AbstractTypeExtension::setDefaultOptions() in favor of overwriting AbstractTypeExtension::configureOptions().
@@ -16,7 +17,7 @@ CHANGELOG
1617
* deprecated ChoiceToBooleanArrayTransformer and ChoicesToBooleanArrayTransformer
1718
* deprecated FixCheckboxInputListener and FixRadioInputListener
1819
* deprecated the "choice_list" option of ChoiceType
19-
* added new options to ChoiceType:
20+
* added new options to ChoiceType:
2021
* "choices_as_values"
2122
* "choice_loader"
2223
* "choice_label"

src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
158158
*/
159159
public function buildView(FormView $view, FormInterface $form, array $options)
160160
{
161+
$choiceTranslationDomain = $options['choice_translation_domain'];
162+
if ($view->parent && null === $choiceTranslationDomain) {
163+
$choiceTranslationDomain = $view->vars['translation_domain'];
164+
}
165+
161166
/** @var ChoiceListView $choiceListView */
162167
$choiceListView = $form->getConfig()->hasAttribute('choice_list_view')
163168
? $form->getConfig()->getAttribute('choice_list_view')
@@ -170,6 +175,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
170175
'choices' => $choiceListView->choices,
171176
'separator' => '-------------------',
172177
'placeholder' => null,
178+
'choice_translation_domain' => $choiceTranslationDomain,
173179
));
174180

175181
// The decision, whether a choice is selected, is potentially done
@@ -295,6 +301,14 @@ public function configureOptions(OptionsResolver $resolver)
295301
return $options['expanded'];
296302
};
297303

304+
$choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) {
305+
if (true === $choiceTranslationDomain) {
306+
return $options['translation_domain'];
307+
}
308+
309+
return $choiceTranslationDomain;
310+
};
311+
298312
$resolver->setDefaults(array(
299313
'multiple' => false,
300314
'expanded' => false,
@@ -317,14 +331,17 @@ public function configureOptions(OptionsResolver $resolver)
317331
// is manually set to an object.
318332
// See https://github.com/symfony/symfony/pull/5582
319333
'data_class' => null,
334+
'choice_translation_domain' => true,
320335
));
321336

322337
$resolver->setNormalizer('choice_list', $choiceListNormalizer);
323338
$resolver->setNormalizer('empty_value', $placeholderNormalizer);
324339
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
340+
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
325341

326342
$resolver->setAllowedTypes('choice_list', array('null', 'Symfony\Component\Form\ChoiceList\ChoiceListInterface'));
327343
$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
344+
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
328345
$resolver->setAllowedTypes('choices_as_values', 'bool');
329346
$resolver->setAllowedTypes('choice_loader', array('null', 'Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface'));
330347
$resolver->setAllowedTypes('choice_label', array('null', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath'));

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function configureOptions(OptionsResolver $resolver)
2424
{
2525
$resolver->setDefaults(array(
2626
'choices' => Intl::getRegionBundle()->getCountryNames(),
27+
'choice_translation_domain' => false,
2728
));
2829
}
2930

src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function configureOptions(OptionsResolver $resolver)
2424
{
2525
$resolver->setDefaults(array(
2626
'choices' => Intl::getCurrencyBundle()->getCurrencyNames(),
27+
'choice_translation_domain' => false,
2728
));
2829
}
2930

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function configureOptions(OptionsResolver $resolver)
2424
{
2525
$resolver->setDefaults(array(
2626
'choices' => Intl::getLanguageBundle()->getLanguageNames(),
27+
'choice_translation_domain' => false,
2728
));
2829
}
2930

src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function configureOptions(OptionsResolver $resolver)
2424
{
2525
$resolver->setDefaults(array(
2626
'choices' => Intl::getLocaleBundle()->getLocaleNames(),
27+
'choice_translation_domain' => false,
2728
));
2829
}
2930

0 commit comments

Comments
 (0)