Symfony 表单标签翻译错误

Symfony form label translation errors

问题:Symfony 工具栏显示丢失的消息,而消息是......好吧,没有丢失。 似乎 {{ form_label(...) }} 调用试图对消息进行双重翻译。例如:

我的 app/Resources/translations/messages.en.yml 中有以下内容:

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

我的 FormType (AppBundle) 中有以下内容 class:

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

并在 app/Resources/views 模板中:

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

在这种情况下,Symfony 工具栏显示 translated titletranslated description 的 2 条缺失消息。它不是在抱怨消息密钥,而是在抱怨已经翻译的消息。就好像 symfony 试图将同一条消息翻译两次。

如果我删除 form_labelform_errorsform_widget 并用单个 form_row 替换它们,那么一切正常。

此外,php bin/console debug:trans 不会将这些消息显示为丢失。

我已经在没有任何自定义的情况下使用原始 form_div_layout.html.twig 对此进行了测试,它仍然显示这些错误。

在 form_label 块下的文件中,我发现:

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

哪一点没有异常?

还尝试在类型 class 中指定 translation_domain,但没有任何区别。

目前正在使用 Symfony 3.0,虽然我认为我在 2.8 中看到了同样的错误。

有什么想法吗?

谢谢 卡罗利斯

占位符和标题属性值在 widget_attributes 块内自动翻译:

{%- 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 -%}

您所要做的就是从您的标题和占位符值中删除多余的 trans 过滤器,即:

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