Karolis
Karolis

Reputation: 2618

Symfony form label translation errors

The problem: Symfony toolbar shows missing messages, while the messages are... well, not missing. It seems as if {{ form_label(...) }} call tries to double-translate the message. For example:

I have the following in my app/Resources/translations/messages.en.yml:

...
entity:
    recipe:
        title: translated title
        description: translated description
...

and I have the following in my FormType (AppBundle) class:

$builder
    ->add('title', TextType::class, ['label' => 'entity.recipe.title'])
    ->add('description', TextareaType::class, ['label' => 'entity.recipe.description']);

and in the app/Resources/views template:

<div class="title_row">
{{ form_label(form.title) }}
{{ form_widget(form.title, { 
    'attr': {
        'class': 'supertitle',
        'placeholder': 'entity.recipe.title'|trans
    }}) 
}}
{{ form_errors(form.title) }}
</div>


<div class="description_row">
{{ form_label(form.description) }}
{{ form_errors(form.description) }}
{{ form_widget(form.description, { 
    'attr': {
        'class': 'metro',
        'placeholder': 'recipe.describe_your_recipe'|trans
    }}) 
}}
</div>

In this case, Symfony toolbar shows 2 missing messages for translated title and translated description. It is not complaining about message keys, but about already translated messages. As if symfony tries to translate the same message twice.

If I drop the form_label, form_errors and form_widget and replace them with a single form_row, then everything works fine.

Also, php bin/console debug:trans does not show these messages as missing.

I have tested this with the original form_div_layout.html.twig without any customizations, and it still shows these errors.

Inside that file under form_label block, I found:

<label{% for attrname, attrvalue in label_attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}>{{ translation_domain is same as(false) ? label : label|trans({}, translation_domain) }}</label>

which is nothing out of ordinary?

Also tried to specify translation_domain within the Type class, didn't make a difference.

Currently using Symfony 3.0, although I think I saw the same errors in 2.8.

Any ideas?

thanks Karolis

Upvotes: 0

Views: 1707

Answers (1)

Pierre
Pierre

Reputation: 884

Placeholder and title attribute values get translated automatically inside the widget_attributes block:

{%- for attrname, attrvalue in attr -%}
    {{- " " -}}
    {%- if attrname in ['placeholder', 'title'] -%}
        {{- attrname }}="{{ attrvalue|trans({}, translation_domain) }}"
    {%- elseif attrvalue is sameas(true) -%}
        {{- attrname }}="{{ attrname }}"
    {%- elseif attrvalue is not sameas(false) -%}
        {{- attrname }}="{{ attrvalue }}"
    {%- endif -%}
{%- endfor -%}

All you have to do is removing the redundant trans filter from your title and placeholder values, i.e.:

{{ form_widget(form.title, { 
    'attr': {
        'class': 'supertitle',
        'placeholder': 'entity.recipe.title'
    }}) 
}}

Upvotes: 1

Related Questions