8000 [Form][choice] added choice_translation_domain to avoid trans options. · symfony/symfony@ddfa468 · GitHub
[go: up one dir, main page]

Skip to content

Commit ddfa468

Browse files
committed
[Form][choice] added choice_translation_domain to avoid trans options.
1 parent be0c98e commit ddfa468

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function configureOptions(OptionsResolver $resolver)
3030
{
3131
$resolver->setDefaults(array(
3232
'choices' => self::getTimezones(),
33+
'choice_translation_domain' => false,
3334
));
3435
}
3536

src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ public function testCountry()
840840
'/select
841841
[@name="name"]
842842
[@class="my&class form-control"]
843-
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
843+
[./option[@value="AT"][@selected="selected"][.="Austria"]]
844844
[count(./option)>200]
845845
'
846846
);
@@ -858,7 +858,7 @@ public function testCountryWithPlaceholder()
858858
[@name="name"]
859859
[@class="my&class form-control"]
860860
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Select&Country[/trans]"]]
861-
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
861+
[./option[@value="AT"][@selected="selected"][.="Austria"]]
862862
[count(./option)>201]
863863
'
864864
);
@@ -1388,7 +1388,7 @@ public function testLanguage()
13881388
'/select
13891389
[@name="name"]
13901390
[@class="my&class form-control"]
1391-
[./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]]
1391+
[./option[@value="de"][@selected="selected"][.="German"]]
13921392
[count(./option)>200]
13931393
'
13941394
);
@@ -1402,7 +1402,7 @@ public function testLocale()
14021402
'/select
14031403
[@name="name"]
14041404
[@class="my&class form-control"]
1405-
[./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]]
1405+
[./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
14061406
[count(./option)>200]
14071407
'
14081408
);
@@ -1826,8 +1826,8 @@ public function testTimezone()
18261826
[@class="my&class form-control"]
18271827
[not(@required)]
18281828
[./optgroup
1829-
[@label="[trans]Europe[/trans]"]
1830-
[./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
1829+
[@label="Europe"]
1830+
[./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
18311831
]
18321832
[count(./optgroup)>10]
18331833
[count(.//option)>200]

src/Symfony/Component/Form/Tests/AbstractLayoutTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ public function testCountry()
10161016
$this->assertWidgetMatchesXpath($form->createView(), array(),
10171017
'/select
10181018
[@name="name"]
1019-
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
1019+
[./option[@value="AT"][@selected="selected"][.="Austria"]]
10201020
[count(./option)>200]
10211021
'
10221022
);
@@ -1033,7 +1033,7 @@ public function testCountryWithPlaceholder()
10331033
'/select
10341034
[@name="name"]
10351035
[./option[@value=""][not(@selected)][not(@disabled)][.="[trans]Select&Country[/trans]"]]
1036-
[./option[@value="AT"][@selected="selected"][.="[trans]Austria[/trans]"]]
1036+
[./option[@value="AT"][@selected="selected"][.="Austria"]]
10371037
[count(./option)>201]
10381038
'
10391039
);
@@ -1557,7 +1557,7 @@ public function testLanguage()
15571557
$this->assertWidgetMatchesXpath($form->createView(), array(),
15581558
'/select
15591559
[@name="name"]
1560-
[./option[@value="de"][@selected="selected"][.="[trans]German[/trans]"]]
1560+
[./option[@value="de"][@selected="selected"][.="German"]]
15611561
[count(./option)>200]
15621562
'
15631563
);
@@ -1570,7 +1570,7 @@ public function testLocale()
15701570
$this->assertWidgetMatchesXpath($form->createView(), array(),
15711571
'/select
15721572
[@name="name"]
1573-
[./option[@value="de_AT"][@selected="selected"][.="[trans]German (Austria)[/trans]"]]
1573+
[./option[@value="de_AT"][@selected="selected"][.="German (Austria)"]]
15741574
[count(./option)>200]
15751575
'
15761576
);
@@ -1936,8 +1936,8 @@ public function testTimezone()
19361936
[@name="name"]
19371937
[not(@required)]
19381938
[./optgroup
1939-
[@label="[trans]Europe[/trans]"]
1940-
[./option[@value="Europe/Vienna"][@selected="selected"][.="[trans]Vienna[/trans]"]]
1939+
[@label="Europe"]
1940+
[./option[@value="Europe/Vienna"][@selected="selected"][.="Vienna"]]
19411941
]
19421942
[count(./optgroup)>10]
19431943
[count(.//option)>200]

src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,51 @@ public function testPassExpandedToView()
13311331
$this->assertTrue($view->vars['expanded']);
13321332
}
13331333

1334+
public function testPassChoiceTranslationDomainToView()
1335+
{
1336+
$form = $this->factory->create('choice', null, array(
1337+
'choices' => $this->choices,
1338+
));
1339+
$view = $form->createView();
1340+
1341+
$this->assertNull($view->vars['choice_translation_domain']);
1342+
}
1343+
1344+
public function testChoiceTranslationDomainWithTrueValueToView()
1345+
{
1346+
$form = $this->factory->create('choice', null, array(
1347+
'choices' => $this->choices,
1348+
'choice_translation_domain' => true,
1349+
));
1350+
$view = $form->createView();
1351+
1352+
$this->assertNull($view->vars['choice_translation_domain']);
1353+
}
1354+
1355+
public function testDefaulChoiceTranslationDomainIsSameAsTranslationDomainToView()
1356+
{
1357+
$form = $this->factory->create('choice', null, array(
1358+
'choices' => $this->choices,
1359+
'translation_domain' => 'foo',
1360+
));
1361+
$view = $form->createView();
1362+
1363+
$this->assertEquals('foo', $view->vars['choice_translation_domain']);
1364+
}
1365+
1366+
public function testInheritChoiceTranslationDomainFromParent()
1367+
{
1368+
$view = $this->factory
1369+
->createNamedBuilder('parent', 'form', null, array(
1370+
'translation_domain' => 'domain',
1371+
))
1372+
->add('child', 'choice')
1373+
->getForm()
1374+
->createView();
1375+
1376+
$this->assertEquals('domain', $view['child']->vars['choice_translation_domain']);
1377+
}
1378+
13341379
public function testPlaceholderIsNullByDefaultIfRequired()
13351380
{
13361381
$form = $this->factory->create('choice', null, array(

0 commit comments

Comments
 (0)
0