如何翻译Symfony2异常 [英] How to translate Symfony2 Exception

查看:27
本文介绍了如何翻译Symfony2异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 yml 格式来翻译我的网络应用程序,但我遇到了一个问题.

我想做什么:

#exception.en.ymlexception.bad: '错误的凭据'

我所知道的可以做到:

#exception.en.yml'坏凭据':'坏凭据'

这是转换异常的唯一方法吗?

解决方案

只需放入翻译器,记得在 Twig 模板中的 message 错误转储上添加 trans 语句.

这是一个 xliff 示例:

 messages.en.xlf<trans-unit id="1"><source>用户帐户被禁用.</source><target>账号被禁用或等待确认</target></trans-unit><trans-unit id="2"><source>坏凭据</source><target>错误的用户名或密码</target></trans-unit>

并在模板中

{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}{% 如果错误 %}

{{ error.message|trans }}

{% 万一 %}<form action="{{ path('login_check') }}" method="post"><label for="username">用户名:</label><input type="text" id="username" name="_username" value="{{ last_username }}"/><label for="password">密码:</label><input type="password" id="password" name="_password"/><button type="submit">登录</button></表单>

检查此文档排除非活跃用户

希望能帮到你

I'm using yml format to translate my web app but I've one problem.

What I would like to do:

#exception.en.yml
exception.bad: 'Bad credentials'

What I know is possible to do:

#exception.en.yml
'Bad credentials': 'Bad credentials'

Is this the only method to translate the exception?

解决方案

simply put in the translator and remember to add the trans statement on the messagge error dump in the Twig template.

Here an xliff example:

    messages.en.xlf

        <trans-unit id="1">
            <source>User account is disabled.</source>
            <target>Account disabled or waiting for confirm</target>
        </trans-unit>
        <trans-unit id="2">
            <source>Bad credentials</source>
            <target>Wrong username or password</target>
        </trans-unit>

and in the template

{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% 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" />

    <button type="submit">login</button>
</form>

Check this doc for exclude non active users

hope this help

这篇关于如何翻译Symfony2异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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