所以我正在阅读Symfony2 Book的安全章节。我理解了所有内容,但是如果出现登录错误,我想自定义错误消息。
我可以在哪个文件中更改此内容?
这是模板:
{% if error %}
<div>{{ error.message }}</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" />
{#
If you want to control the URL the user is redirected to on success (more details below)
<input type="hidden" name="_target_path" value="/account" />
#}
<input type="submit" name="login" />
我认为最糟糕的做法是:
if (error.message=="Bad credentials")
echo "Los datos son erróneos :)"
if (error.message==The presented password is invalid")
echo "La combinación username/password no es correcta :)"
请你帮帮我吗?
编辑:我明白了:
如果有人需要这样做,请务必将此行添加到config.yml
#app/config/config.yml
framework:
translator: { fallback: en }
并输入文件messages.whateverisyourlanguage.yml,在我的例子中就是messages.es.yml,这样的行:
您要翻译的文字:翻译文字
#Foo\DummyBundle\Resources\translations\messages.es.yml
The presented password cannot be empty.: El campo contrasena no debe estar vacio
The presented password is invalid.: Los datos suministrados son incorrectos
Bad credentials: Los datos suministrados son incorrectos
小心要翻译的文字。如果文本末尾有一个点,则必须放置点。我没有这样做而且没有用。
footranslate.
与footranslate
问候! :)
答案 0 :(得分:16)
您可以使用translation。在您parameters.ini
set locale的语言中创建message file。然后在twig模板中使用:
{% if error %}
<div class="error">{{ error.message|trans({},'messages') }}</div>
{% endif %}
答案 1 :(得分:3)
如果您不想使用翻译,还有另一种可能性。您可以只替换消息,例如:
{{ error.message | replace({"Bad credentials." : "Invalid username or password."}) }}