【问题标题】:How to translate Symfony2 Exception如何翻译 Symfony2 异常
【发布时间】:2014-12-21 20:40:24
【问题描述】:

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

我想做的事:

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

我知道可以做的事情:

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

这是翻译异常的唯一方法吗?

【问题讨论】:

    标签: php symfony twig yaml


    【解决方案1】:

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

    这里是一个 xliff 示例:

        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>
    

    在模板中

    {# 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>
    

    检查this doc 以排除非活跃用户

    希望对你有所帮助

    【讨论】:

    • 跟我想的一模一样,很有用!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-13
    • 2013-08-02
    • 1970-01-01
    • 2012-04-10
    • 2015-10-07
    相关资源
    最近更新 更多