在 symfony2 中翻译身份验证错误的推荐方法
例如,我需要以另一种语言显示“错误凭据”消息。 到目前为止,我发现它作为异常抛出\vendor\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php
验证
函数为throw new BadCredentialsException('Bad credentials', 0, $notFound);
我想知道用另一种语言显示此消息的推荐方法是什么。只是改变这个地方的字符串似乎不是最佳的...... 此外,在身份验证过程中可能还会显示其他消息。
我正在使用 JMSSecurityExtraBundle
和 FOSUserBundle
,我想可能有一些内置功能来处理这个问题......?
I need to display, for example, the "Bad credentials" message in another language.
So far I found that it is thrown as an exception in\vendor\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php
authenticate
function asthrow new BadCredentialsException('Bad credentials', 0, $notFound);
I am wondering what would be the recommended way for showing this message in another language. Just changing the string in this place seems non-optimal...
Also there are other messages that might probably be shown during authentication.
I am using the JMSSecurityExtraBundle
and FOSUserBundle
and I guess there could be some built in functionality to handle this...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
覆盖登录模板:将FOSUserBundle/Resources/views/Security/login.html.twig复制到app/Resources/views/Security/login.html.twig(请参阅doc,视频)
将 trans 过滤器添加到 login.html 中的 error 变量。树枝(参见doc):
{{ error|trans }}
在您的翻译文件中src/YourBundlePath/Resources/translations/messages.{locale}.yml 添加:
错误的凭据:您的错误文本
Override login template: copy FOSUserBundle/Resources/views/Security/login.html.twig to app/Resources/views/Security/login.html.twig (see doc, video)
Add trans filter to error variable in login.html.twig (see doc):
{{ error|trans }}
In your translate file src/YourBundlePath/Resources/translations/messages.{locale}.yml add:
Bad credentials: Your error text