在Symfony2的翻译身份验证错误的方式推荐 [英] Recommended way of translating authentication errors in symfony2

查看:197
本文介绍了在Symfony2的翻译身份验证错误的方式推荐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要显示,例如,在另一种语言的坏凭据的消息。
到目前为止,我发现它抛出的结果异常
<$c$c>\\vendor\\symfony\\src\\Symfony\\Component\\Security\\Core\\Authentication\\Provider\\UserAuthenticationProvider.php

验证函数结果
抛出新BadCredentialsException(坏凭据',0,$ NOTFOUND);

我想知道什么将是展示另一种语言此消息的推荐方式。只是改变在这个地方似乎串非最佳...
也有可能大概身份验证过程中显示其他消息。

我现在用的是 JMSSecurityExtraBundle FOSUserBundle ,我想可能有一些内置的功能来处理这个问题。 ..?


解决方案

  1. 覆盖登录模板:副本 FOSUserBundle /资源/视图/安全/ login.html.twig 应用/资源/视图/安全/ login.html.twig (见文档,的视频


  2. 过滤器添加到错误变量login.html.twig(见的 DOC ):

    {{错误|反}}


  3. 在您的翻译文件的src / YourBundlePath /资源/翻译/信息{}区域.yml 补充:

    坏凭据:您的错误文本


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 as
throw 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...?

解决方案

  1. Override login template: copy FOSUserBundle/Resources/views/Security/login.html.twig to app/Resources/views/Security/login.html.twig (see doc, video)

  2. Add trans filter to error variable in login.html.twig (see doc):

    {{ error|trans }}

  3. In your translate file src/YourBundlePath/Resources/translations/messages.{locale}.yml add:

    Bad credentials: Your error text

这篇关于在Symfony2的翻译身份验证错误的方式推荐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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