简体   繁体   English

Twig form_errors函数未在Validators.lang.yml中加载翻译消息

[英]Twig form_errors function doesn't load translation message in validators.lang.yml

I'm using Symfony2 translations in order to display error messages in my forms. 我正在使用Symfony2转换,以便在表单中显示错误消息。 My message is defined in validators.fr.yml located in the Ressources/translations subfolder of my bundle. 我的消息是在我的软件包“资源Ressources/translations子文件夹中的validators.fr.yml中定义的。 I'm also using annotations to set the message to use. 我也在使用注释来设置要使用的消息。

The problem is that the message is not shown in my template. 问题是该消息未显示在我的模板中。 I only get the key that i've defined in my annotation. 我只得到注释中定义的密钥。

A sample of code from my entity : 来自我实体的代码示例:

/**
 * Nom du site de travail.
 *
 * @var string le nom du site de travail.
 *
 * @ORM\Column(
 *      name   = "nom",
 *      type   = "string",
 *      length = 255
 * )
 *
 * @Assert\NotBlank(message = "structure.siteTravail.nom.notnull")
 */
private $nom;

In my form class, I've implemented the getDefaultOptions method with the data_class option (don't really know if it helps) : 在我的表单类中,我已经实现了带有data_class选项的getDefaultOptions方法(不知道是否有帮助):

public function getDefaultOptions(array $options)
{
    return array(
        'data_class' => 'My\FreakingBundle\Entity\SiteTravail'
    );
}

Here is the content of validators.fr.yml : 这是validators.fr.yml的内容:

structure:
    siteTravail:
        nom:
            notnull: Le nom est obligatoire.

In my Twig template I'm using this code to render the field's errors : 在我的Twig模板中,我使用以下代码来呈现字段的错误:

{{ form_errors(form.nom) }}

Also, translation is activated in app/config/config.yml : 另外,在app/config/config.yml激活了翻译:

framework:
    translator: { fallback: fr }

All my translations are working perfectly in messages.fr.yml for this bundle and I've got another bundle using both messages.fr.yml and validators.fr.yml as well. 我所有的翻译都在该捆绑包的messages.fr.yml中正常工作,并且我还同时使用了messages.fr.ymlvalidators.fr.yml

The weirdest part is that when I put the content of validators.fr.yml in my other bundle, it works. 最奇怪的部分是,当我将validators.fr.yml的内容放入另一个捆绑包中时,它可以工作。

You can force the translation overriding the field_errors form block. 您可以强制转换覆盖field_errors表单块。

So, when you render the form in your twig template: 因此,当您在树枝模板中渲染表单时:

{% form_theme form _self %}

{% block field_errors %}
{% spaceless %}
    {% if errors|length > 0 %}
        <ul>
            {% for error in errors %}
                <li>{{ error.messageTemplate|trans(error.messageParameters, 'validators')|trans }}</li>
            {% endfor %}
        </ul>
    {% endif %}
{% endspaceless %}
{% endblock %}

{% block content %}
    {# ... other stuffs ... #}
        {{ form_widget(form) }}
    {# ... other stuffs ... #}
{% endblock %}

If you put the translations in a specific domain file (for example: "validators.lang.yml") use this syntax: 如果将翻译内容放在特定的域文件中(例如:“ validators.lang.yml”),请使用以下语法:

<li>{{ error.messageTemplate|trans(error.messageParameters, 'validators')|trans({}, 'validators') }}</li>

Here you can find all the default form blocks for custom replacements purposes: https://github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig 在这里,您可以找到用于自定义替换的所有默认表单块: https : //github.com/symfony/symfony/blob/master/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM