【问题标题】:Cannot make Internationalisation works in Symfony 2无法在 Symfony 2 中进行国际化
【发布时间】:2013-12-16 01:29:51
【问题描述】:

我正在关注 Symfony 的 Translation document 中的每一步。但我仍然无法在 Symfony 中进行国际化。我收到的不是翻译的消息,而是相同的消息。我做错了什么?这是我的设置:

1) app/config/config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }

framework:
    #esi:             ~
    translator:      { fallback: fa }

2) 应用程序/parameters.yml

parameters:
    database_driver: pdo_mysql
    database_host: 127.0.0.1
    database_port: null
    database_user: root
    locale: fa

3) 翻译文件位于 src/App/AppBundle/Resources/translations/addproduct.fa.xliff

<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
        <body>
            <trans-unit id="1">
                <source>Title</source>
                <target>عنوان</target>
            </trans-unit>
        </body>
    </file>
</xliff>

4) 控制器

/**
 * @Route("{_locale}/test")
 */
public function Test()
{
    $translated = $this->get('translator')->trans('Title');

    return new Response($translated);
}

【问题讨论】:

    标签: php symfony internationalization


    【解决方案1】:
    1. 数字 3) 应该是 src/App/AppBundle/Resources/translations 目录中的文件。文件名需要遵循here 概述的domain.locale.loader 约定,例如app.fa.xliff

    2. 如果您将文件命名为 app.fa.xliff,则 'app' 是您的域。你需要在-&gt;trans方法中引用这个域:

    $translated = $this-&gt;get('translator')-&gt;trans('Title', array(), 'app');

    【讨论】:

    • 我遵循了命名约定,但我忘记在我的问题中提及它。 (问题已更新)。我的翻译文件名为 addproduct.fa.xliff。根据文档,域是“将消息组织成组的可选方式”。我选择了“添加产品”。语言环境是 fa,加载程序是 xliff。现在正确吗?
    • 调用-&gt;trans方法时还需要引用'addproduct'域。请参阅上面我的答案末尾的示例,但将“app”替换为“addproduct”。记得在每次更新翻译文件后清除缓存 (app/console cache:clear --no-warmup)。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 2017-09-16
    • 2012-04-25
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多