问题
I use FOSUserBundle login form and i would like translate errors messages. These messages are launched here: vendor/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php
protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
{
$currentUser = $token->getUser();
if ($currentUser instanceof UserInterface) {
if ($currentUser->getPassword() !== $user->getPassword()) {
throw new BadCredentialsException('The credentials were changed from another session.');
}
} else {
if (!$presentedPassword = $token->getCredentials()) {
throw new BadCredentialsException('The presented password cannot be empty.');
}
if (!$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt())) {
throw new BadCredentialsException('The presented password is invalid.');
}
}
}
I've write app/Resources/translations/validators.fr.yml
"The presented password cannot be empty.": "Veuillez saisir un mot de passe."
I've write app/Resources/translations/messages.fr.yml
"The presented password cannot be empty.": "Veuillez saisir un mot de passe."
But it doesn't work. Other translation are working (=>fr), but i've problem with these messages.
Special procedure for these messages ?
回答1:
In file Sonata/UserBundle/Resources/views/Admin/Security/login.html.twig You have:
<div class="alert-message error">{{ error|trans({}, 'SonataUserBundle') }}</div>
so You must change SonataUserBundle to whatever translation file You are using or add src/Your/Bundle/Resources/translations/SonataUserBundle.{locale}.yml
and inside translation file:
'Bad credentials': 'Your translation'
'The presented password is invalid.': 'Your translation'
'The presented password cannot be empty.': 'Your translation'
I hope it's clear enough ;]
回答2:
In version 1.3 of FOSUserBundle "Bad Credentials" it's actually "Bad Credentials." (mind the last dot).
回答3:
I don't think so. You have to catch the error and do what you need to do...For example display a message with flash session message and redirect to your forget password page.
来源:https://stackoverflow.com/questions/8493380/translate-error-message-of-login-form