如何翻译 symfony 表单错误信息? [英] How to translate symfony form error messages?

查看:19
本文介绍了如何翻译 symfony 表单错误信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我使用的 symfony 独立翻译组件:

for symfony standalone translation component I use:

$translator = new Translator('fr_FR');
$translator->addLoader('php', new \Symfony\Component\Translation\Loader\PhpFileLoader());
$translator->addResource('php', ROOT.'/translations/messages.fr.php', 'fr_FR');
$twig->addExtension(new TranslationExtension($translator));

翻译 symfony 表单标签工作正常.要翻译我使用的 symfony 形式约束错误:

It is working fine to translate symfony form labels. To translate symfony form constraint errors I use:

->add('firstname', TextType::class, [
        'constraints' => [new Assert\Length(['min' => 3, 
                                                                   'minMessage' => 'name.short',
                                                                 ])
                                  ]
    ])

并在 messages.fr.php

我有

return [
    .....
    ...
    'name.short' => 'Name is short',
];    

其他表格标签也在此目录中.标签翻译得很好,但对于表单错误,我得到 name.short 而不是它的值.我犯了什么错误?是不是要找别的地方找翻译目录?

other form labels are in this catalog too. labels are translated fine but for form error, I get name.short instead of its value. What mistake I did? Is it looking for another place to find the translation catalog?

对于树枝,我在没有缓存参数的情况下使用它,

for twig I use this without cache parameter,

$twig = new \Twig\Environment($loader, [
    'strict_variables' => true,
    'optimizations' => -1,
    'debug' => true,
]);    

似乎没有设置树枝缓存,对于独立的 symfony 表单和验证器,我没有设置任何缓存,当然我检查了目录,没有设置缓存.我还应该检查什么?

and it seems no twig cache is set, for standalone symfony form and validator I did not set any cache, for sure I checked directories, no cache is set. What else should I check?

编辑 2:

我也用过这个:

$translator = new Translator('fr_FR');
$translator->addLoader('php', new \Symfony\Component\Translation\Loader\PhpFileLoader());
$translator->addResource('php', ROOT.'/translations/validators.fr.php', 'fr_FR');
$translator->addResource('php', ROOT.'/translations/messages.fr.php', 'fr_FR');
$twig->addExtension(new TranslationExtension($translator));

在 translations/validators.fr.php 中,我有一个返回的数组,其中的元素 name.shor 像上面一样,但仍然不起作用.

and in translations/validators.fr.php I have a returning array with element name.shor like above, but still doesn't work.

推荐答案

验证消息的默认域不是默认的消息"

The default domain for validation message is not the default "message"

尝试添加一个新的资源validators.fr.php,并将消息放入此文件中.

Try to add a new ressource validators.fr.php, and put the messages in this file.

请参阅框架的文档,但我认为它与独立组件的过程相同 https://symfony.com/doc/current/validation/translations.html

See documentation for the framework but I think it’s the same process for standalone components https://symfony.com/doc/current/validation/translations.html

这篇关于如何翻译 symfony 表单错误信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆