Recommended way of translating authentication errors in symfony2

蓝咒 提交于 2019-12-11 17:34:26

问题


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:


  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



来源:https://stackoverflow.com/questions/8865911/recommended-way-of-translating-authentication-errors-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!