Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0e0b904

Browse filesBrowse files
Restless-ETfabpot
authored andcommitted
[Translation][Form] Do not translate form labels and placeholders when 'translation_domain' is false
1 parent fef28b2 commit 0e0b904
Copy full SHA for 0e0b904

9 files changed

+193
-13
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
{%- endif -%}
5858
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
5959
{%- if placeholder is not none -%}
60-
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? placeholder|trans({}, translation_domain) }}</option>
60+
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder != '' ? (translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain)) }}</option>
6161
{%- endif -%}
6262
{%- if preferred_choices|length > 0 -%}
6363
{% set options = preferred_choices %}
@@ -192,7 +192,7 @@
192192
{% set label = name|humanize %}
193193
{%- endif -%}
194194
{%- endif -%}
195-
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ label|trans({}, translation_domain) }}</button>
195+
<button type="{{ type|default('button') }}" {{ block('button_attributes') }}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</button>
196196
{%- endblock button_widget -%}
197197

198198
{%- block submit_widget -%}
@@ -324,7 +324,7 @@
324324
{%- for attrname, attrvalue in attr -%}
325325
{{- " " -}}
326326
{%- if attrname in ['placeholder', 'title'] -%}
327-
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
327+
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
328328
{%- elseif attrvalue is same as(true) -%}
329329
{{- attrname }}="{{ attrname }}"
330330
{%- elseif attrvalue is not same as(false) -%}
@@ -338,7 +338,7 @@
338338
{%- for attrname, attrvalue in attr -%}
339339
{{- " " -}}
340340
{%- if attrname in ['placeholder', 'title'] -%}
341-
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
341+
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
342342
{%- elseif attrvalue is same as(true) -%}
343343
{{- attrname }}="{{ attrname }}"
344344
{%- elseif attrvalue is not same as(false) -%}
@@ -352,7 +352,7 @@
352352
{%- for attrname, attrvalue in attr -%}
353353
{{- " " -}}
354354
{%- if attrname in ['placeholder', 'title'] -%}
355-
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
355+
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
356356
{%- elseif attrvalue is same as(true) -%}
357357
{{- attrname }}="{{ attrname }}"
358358
{%- elseif attrvalue is not same as(false) -%}
@@ -365,7 +365,7 @@
365365
{%- for attrname, attrvalue in attr -%}
366366
{{- " " -}}
367367
{%- if attrname in ['placeholder', 'title'] -%}
368-
{{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
368+
{{- attrname }}="{{ translation_domain is same as(false) ? attrvalue : attrvalue|trans({}, translation_domain) }}"
369369
{%- elseif attrvalue is same as(true) -%}
370370
{{- attrname }}="{{ attrname }}"
371371
{%- elseif attrvalue is not same as(false) -%}

‎src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
{%- endif -%}
151151
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple" data-customforms="disabled"{% endif %}>
152152
{% if placeholder is not none -%}
153-
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ placeholder|trans({}, translation_domain) }}</option>
153+
<option value=""{% if required and value is empty %} selected="selected"{% endif %}>{{ translation_domain is same as(false) ? placeholder : placeholder|trans({}, translation_domain) }}</option>
154154
{%- endif %}
155155
{%- if preferred_choices|length > 0 -%}
156156
{% set options = preferred_choices %}
@@ -253,7 +253,7 @@
253253
{% endif %}
254254
<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>
255255
{{ widget|raw }}
256-
{{ label|trans({}, translation_domain) }}
256+
{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}
257257
</label>
258258
{%- endblock checkbox_radio_label %}
259259

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_attributes.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/button_attributes.html.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($disabled): ?>disabled="disabled" <?php endif ?>
22
<?php foreach ($attr as $k => $v): ?>
33
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
4-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
4+
<?php printf('%s="%s" ', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
55
<?php elseif ($v === true): ?>
66
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
77
<?php elseif ($v !== false): ?>
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php if (!$label) { $label = isset($label_format)
22
? strtr($label_format, array('%name%' => $name, '%id%' => $id))
33
: $view['form']->humanize($name); } ?>
4-
<button type="<?php echo isset($type) ? $view->escape($type) : 'button' ?>" <?php echo $view['form']->block($form, 'button_attributes') ?>><?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></button>
4+
<button type="<?php echo isset($type) ? $view->escape($type) : 'button' ?>" <?php echo $view['form']->block($form, 'button_attributes') ?>><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($label, array(), $translation_domain) : $label) ?></button>

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/choice_widget_collapsed.html.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
)) ?>
88
<?php if ($multiple): ?> multiple="multiple"<?php endif ?>
99
>
10-
<?php if (null !== $placeholder): ?><option value=""<?php if ($required and empty($value) && '0' !== $value): ?> selected="selected"<?php endif?>><?php echo '' != $placeholder ? $view->escape($view['translator']->trans($placeholder, array(), $translation_domain)) : '' ?></option><?php endif; ?>
10+
<?php if (null !== $placeholder): ?><option value=""<?php if ($required and empty($value) && '0' !== $value): ?> selected="selected"<?php endif?>><?php echo '' != $placeholder ? $view->escape(false !== $translation_domain ? $view['translator']->trans($placeholder, array(), $translation_domain) : $placeholder) : '' ?></option><?php endif; ?>
1111
<?php if (count($preferred_choices) > 0): ?>
1212
<?php echo $view['form']->block($form, 'choice_widget_options', array('choices' => $preferred_choices)) ?>
1313
<?php if (count($choices) > 0 && null !== $separator): ?>

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<?php if ($required): ?> required="required"<?php endif ?>
33
<?php foreach ($attr as $k => $v): ?>
44
<?php if (in_array($k, array('placeholder', 'title'), true)): ?>
5-
<?php printf(' %s="%s"', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
5+
<?php printf(' %s="%s"', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
66
<?php elseif ($v === true): ?>
77
<?php printf(' %s="%s"', $view->escape($k), $view->escape($k)) ?>
88
<?php elseif ($v !== false): ?>

‎src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_container_attributes.html.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_container_attributes.html.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php if (!empty($id)): ?>id="<?php echo $view->escape($id) ?>" <?php endif ?>
22
<?php foreach ($attr as $k => $v): ?>
33
<?php if (in_array($v, array('placeholder', 'title'), true)): ?>
4-
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?>
4+
<?php printf('%s="%s" ', $view->escape($k), $view->escape(false !== $translation_domain ? $view['translator']->trans($v, array(), $translation_domain) : $v)) ?>
55
<?php elseif ($v === true): ?>
66
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?>
77
<?php elseif ($v !== false): ?>

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,32 @@ public function testSingleChoiceWithoutTranslation()
254254
);
255255
}
256256

257+
public function testSingleChoiceWithPlaceholderWithoutTranslation()
258+
{
259+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
260+
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
261+
'multiple' => false,
262+
'expanded' => false,
263+
'required' => false,
264+
'translation_domain' => false,
265+
'placeholder' => 'Placeholder&Not&Translated',
266+
));
267+
268+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
269+
'/select
270+
[@name="name"]
271+
[@class="my&class form-control"]
272+
[not(@required)]
273+
[
274+
./option[@value=""][not(@selected)][not(@disabled)][.="Placeholder&Not&Translated"]
275+
/following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
276+
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
277+
]
278+
[count(./option)=3]
279+
'
280+
);
281+
}
282+
257283
public function testSingleChoiceAttributes()
258284
{
259285
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
@@ -771,6 +797,52 @@ public function testSingleChoiceExpandedWithPlaceholder()
771797
);
772798
}
773799

800+
public function testSingleChoiceExpandedWithPlaceholderWithoutTranslation()
801+
{
802+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
803+
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
804+
'multiple' => false,
805+
'expanded' => true,
806+
'translation_domain' => false,
807+
'placeholder' => 'Placeholder&Not&Translated',
808+
));
809+
810+
$this->assertWidgetMatchesXpath($form->createView(), array(),
811+
'/div
812+
[
813+
./div
814+
[@class="radio"]
815+
[
816+
./label
817+
[.=" Placeholder&Not&Translated"]
818+
[
819+
./input[@type="radio"][@name="name"][@id="name_placeholder"][not(@checked)]
820+
]
821+
]
822+
/following-sibling::div
823+
[@class="radio"]
824+
[
825+
./label
826+
[.=" Choice&A"]
827+
[
828+
./input[@type="radio"][@name="name"][@id="name_0"][@checked]
829+
]
830+
]
831+
/following-sibling::div
832+
[@class="radio"]
833+
[
834+
./label
835+
[.=" Choice&B"]
836+
[
837+
./input[@type="radio"][@name="name"][@id="name_1"][not(@checked)]
838+
]
839+
]
840+
/following-sibling::input[@type="hidden"][@id="name__token"][@class="form-control"]
841+
]
842+
'
843+
);
844+
}
845+
774846
public function testSingleChoiceExpandedWithBooleanValue()
775847
{
776848
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', true, array(
@@ -2023,6 +2095,17 @@ public function testButton()
20232095
);
20242096
}
20252097

2098+
public function testButtonlabelWithoutTranslation()
2099+
{
2100+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', null, array(
2101+
'translation_domain' => false,
2102+
));
2103+
2104+
$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
2105+
'/button[@type="button"][@name="name"][.="Name"][@class="my&class btn"]'
2106+
);
2107+
}
2108+
20262109
public function testSubmit()
20272110
{
20282111
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\SubmitType');

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,25 @@ public function testLabelFormatOverriddenOption()
381381
);
382382
}
383383

384+
public function testLabelWithoutTranslationOnButton()
385+
{
386+
$form = $this->factory->createNamedBuilder('myform', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
387+
'translation_domain' => false,
388+
))
389+
->add('mybutton', 'Symfony\Component\Form\Extension\Core\Type\ButtonType')
390+
->getForm();
391+
$view = $form->get('mybutton')->createView();
392+
$html = $this->renderWidget($view);
393+
394+
$this->assertMatchesXpath($html,
395+
'/button
396+
[@type="button"]
397+
[@name="myform[mybutton]"]
398+
[.="Mybutton"]
399+
'
400+
);
401+
}
402+
384403
public function testLabelFormatOnButton()
385404
{
386405
$form = $this->factory->createNamedBuilder('myform')
@@ -549,6 +568,31 @@ public function testSingleChoiceWithoutTranslation()
549568
);
550569
}
551570

571+
public function testSingleChoiceWithPlaceholderWithoutTranslation()
572+
{
573+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
574+
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
575+
'multiple' => false,
576+
'expanded' => false,
577+
'required' => false,
578+
'translation_domain' => false,
579+
'placeholder' => 'Placeholder&Not&Translated',
580+
));
581+
582+
$this->assertWidgetMatchesXpath($form->createView(), array(),
583+
'/select
584+
[@name="name"]
585+
[not(@required)]
586+
[
587+
./option[@value=""][not(@selected)][not(@disabled)][.="Placeholder&Not&Translated"]
588+
/following-sibling::option[@value="&a"][@selected="selected"][.="Choice&A"]
589+
/following-sibling::option[@value="&b"][not(@selected)][.="Choice&B"]
590+
]
591+
[count(./option)=3]
592+
'
593+
);
594+
}
595+
552596
public function testSingleChoiceAttributes()
553597
{
554598
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
@@ -998,6 +1042,32 @@ public function testSingleChoiceExpandedWithPlaceholder()
9981042
);
9991043
}
10001044

1045+
public function testSingleChoiceExpandedWithPlaceholderWithoutTranslation()
1046+
{
1047+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array(
1048+
'choices' => array('&a' => 'Choice&A', '&b' => 'Choice&B'),
1049+
'multiple' => false,
1050+
'expanded' => true,
1051+
'translation_domain' => false,
1052+
'placeholder' => 'Placeholder&Not&Translated',
1053+
));
1054+
1055+
$this->assertWidgetMatchesXpath($form->createView(), array(),
1056+
'/div
1057+
[
1058+
./input[@type="radio"][@name="name"][@id="name_placeholder"][not(@checked)]
1059+
/following-sibling::label[@for="name_placeholder"][.="Placeholder&Not&Translated"]
1060+
/following-sibling::input[@type="radio"][@name="name"][@id="name_0"][@checked]
1061+
/following-sibling::label[@for="name_0"][.="Choice&A"]
1062+
/following-sibling::input[@type="radio"][@name="name"][@id="name_1"][not(@checked)]
1063+
/following-sibling::label[@for="name_1"][.="Choice&B"]
1064+
/following-sibling::input[@type="hidden"][@id="name__token"]
1065+
]
1066+
[count(./input)=4]
1067+
'
1068+
);
1069+
}
1070+
10011071
public function testSingleChoiceExpandedWithBooleanValue()
10021072
{
10031073
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', true, array(
@@ -2141,6 +2211,17 @@ public function testButtonLabelIsEmpty()
21412211
$this->assertSame('', $this->renderLabel($form->createView()));
21422212
}
21432213

2214+
public function testButtonlabelWithoutTranslation()
2215+
{
2216+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ButtonType', null, array(
2217+
'translation_domain' => false,
2218+
));
2219+
2220+
$this->assertWidgetMatchesXpath($form->createView(), array(),
2221+
'/button[@type="button"][@name="name"][.="Name"]'
2222+
);
2223+
}
2224+
21442225
public function testSubmit()
21452226
{
21462227
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\SubmitType');
@@ -2349,4 +2430,20 @@ public function testTranslatedAttributes()
23492430
$this->assertMatchesXpath($html, '/form//input[@title="[trans]Foo[/trans]"]');
23502431
$this->assertMatchesXpath($html, '/form//input[@placeholder="[trans]Bar[/trans]"]');
23512432
}
2433+
2434+
public function testAttributesNotTranslatedWhenTranslationDomainIsFalse()
2435+
{
2436+
$view = $this->factory->createNamedBuilder('name', 'Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
2437+
'translation_domain' => false,
2438+
))
2439+
->add('firstName', 'Symfony\Component\Form\Extension\Core\Type\TextType', array('attr' => array('title' => 'Foo')))
2440+
->add('lastName', 'Symfony\Component\Form\Extension\Core\Type\TextType', array('attr' => array('placeholder' => 'Bar')))
2441+
->getForm()
2442+
->createView();
2443+
2444+
$html = $this->renderForm($view);
2445+
2446+
$this->assertMatchesXpath($html, '/form//input[@title="Foo"]');
2447+
$this->assertMatchesXpath($html, '/form//input[@placeholder="Bar"]');
2448+
}
23522449
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.