Symfony2 - 如何翻译来自FOSUserBundle的"错误凭据"登录错误消息?

meh*_*adi 8 symfony fosuserbundle

如果存在登录错误,我想自定义错误消息("错误凭据").我这样做却无效.这是模板登录:

{% if error %}
<div>{{ error.message|trans }}</div>
{% endif %}<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />

<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
<input type="hidden" name="_target_path" value="/account" />
<input type="submit" name="login" />
Run Code Online (Sandbox Code Playgroud)

文件config.yml我有这个:

framework:
#esi:             ~
translator:      { fallback: "%locale%" }
Run Code Online (Sandbox Code Playgroud)

在parametre.yml我有这个:

locale: en
Run Code Online (Sandbox Code Playgroud)

我在MyBundle/Resources/translations/messages.en.ymy目录中添加了该文件:

security:
login:
    username: "Username:"
    password: "Password:"
    submit: Login
    forgot_username: "Forgot username"
    forgot_password: "Forgot password"
    registration: register
    # Security
    "Bad credentials": "Your user name or password are incorrect."
Run Code Online (Sandbox Code Playgroud)

但我总是有错误的凭据错误消息

小智 5

问题是错误消息在字符串的末尾有点(.).

您可以使用树枝修剪滤镜来纠正此问题:

{{ error|trim('.')|trans({}, 'FOSUserBundle') }}
Run Code Online (Sandbox Code Playgroud)


Nic*_*ich 1

您使用了错误的翻译域

FOSUserBundle 使用FOSUserBundle而不是messages. 看一下默认模板

捆绑包内的翻译文件存储在:

<bundle>/Resources/translations/<translationDomain>.<locale>.<format>
Run Code Online (Sandbox Code Playgroud)

解决方案:

将翻译文件重命名为FOSUserBundle.<locale>.<format>. 然后清除缓存即可解决此问题。