Translate error message of login form

故事扮演 提交于 2019-12-08 07:06:55

问题


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

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