问题
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...?
回答1:
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
来源:https://stackoverflow.com/questions/8865911/recommended-way-of-translating-authentication-errors-in-symfony2