简体   繁体   English

翻译不适用于Symfony2

[英]Translations not working in Symfony2

I have a file with french translations located in FooBundle/Resources/translations/messages.fr.xlf 我有一个法语翻译文件位于FooBundle/Resources/translations/messages.fr.xlf

Example: 例:

<?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>Foo</source>
                <target>Bar</target>
            </trans-unit>
        </body>
    </file>
</xliff>

But I can't seem to make any translation work, neither in the controller: 但我似乎无法在控制器中进行任何翻译工作:

// FooBundle/Controller/DefaultController.php
/**
 * @Route("/")
 * @Template()
 */
public function indexAction(Request $request)
{
    $request->setLocale("fr");
    $translatedMessage = $this->get('translator')->trans('Foo');

    echo $translatedMessage;

    return array();
}

Or the twig template: 或者树枝模板:

// FooBundle/Resources/views/Default/index.html.twig
{{ 'Foo'|trans }} or {% trans %}Foo{% endtrans %}

It always shows Foo (the original string). 它总是显示Foo (原始字符串)。

As my default locale I use english ('en'). 作为我的默认语言环境,我使用英语('en')。 My locale config from config.yml : 我的语言环境配置来自config.yml

framework:
    translator: { fallback: "%locale%" }
    default_locale: "%locale%"
    ...

I've tried to clean up the cache but it didn't make any difference. 我试图清理缓存,但它没有任何区别。

If I try to debug the translations, it shows that they are being used: 如果我尝试调试翻译,则表明它们正在被使用:

$ php app/console debug:translation fr FooBundle

+----------+---------------+----------------------+
| State(s) | Id            | Message Preview (fr) |
+----------+---------------+----------------------+
|          | Foo           | Bar                  |
+----------+---------------+----------------------+

Any idea what is going wrong here? 知道这里出了什么问题吗?

It seems that the problem was another bundle I have installed (LuneticsLocaleBundle). 似乎问题是我安装的另一个软件包(LuneticsLocaleBundle)。

Somehow it must override the $request->setLocale("fr"); 不知何故,它必须覆盖$request->setLocale("fr"); I do in the controller so it turns out I was using en_US as locale. 我在控制器中做,所以事实证明我使用en_US作为语言环境。 That was the reason why it wasn't showing the translations. 这就是它没有显示翻译的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM