翻译登录表单的错误信息 [英] Translate error message of login form

查看:25
本文介绍了翻译登录表单的错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 FOSUserBundle 登录表单,我想翻译错误消息.这些消息在此处启动:vendor/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php

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.');
            }
        }
    }

我写了app/Resources/translations/validators.fr.yml

"The presented password cannot be empty.":      "Veuillez saisir un mot de passe."

我写了app/Resources/translations/messages.fr.yml

"The presented password cannot be empty.":      "Veuillez saisir un mot de passe."

但它不起作用.其他翻译工作正常 (=>fr),但我对这些消息有疑问.

But it doesn't work. Other translation are working (=>fr), but i've problem with these messages.

这些消息的特殊程序?

推荐答案

在文件 Sonata/UserBundle/Resources/views/Admin/Security/login.html.twig 你有:

In file Sonata/UserBundle/Resources/views/Admin/Security/login.html.twig You have:

<div class="alert-message error">{{ error|trans({}, 'SonataUserBundle') }}</div>

所以您必须将 SonataUserBundle 更改为您正在使用或添加的任何翻译文件src/Your/Bundle/Resources/translations/SonataUserBundle.{locale}.yml

so You must change SonataUserBundle to whatever translation file You are using or add src/Your/Bundle/Resources/translations/SonataUserBundle.{locale}.yml

和内部翻译文件:

'Bad credentials': 'Your translation'
'The presented password is invalid.': 'Your translation'
'The presented password cannot be empty.': 'Your translation'

我希望它足够清楚;]

这篇关于翻译登录表单的错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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