简体   繁体   English

symfony3中的翻译目录

[英]translation catalog in symfony3

i want to translate english text into french and i enabled translating in config.yml and my problem is it doesn't translate hello to french 我想将英语文本翻译成法语,并且我启用了config.yml的翻译功能,但我的问题是它不会将问候语翻译成法语

i created a xml catalog for translate for french language named messages.fr.xlf and put it into app/Resources/translations: 我创建了一个用于翻译法语的XML目录,命名为messages.fr.xlf并将其放入app / Resources / translations:

<?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="hello">
                <source>hello</source>
                <target>bonjour</target>
            </trans-unit>
        </body>
    </file>
</xliff>

and this is my controller that renders template: 这是我呈现模板的控制器:

class TranslationController extends Controller
{
    /**
     * @Route("tran",name="tran")
     */
    public function indexAction(Request $request){
        $request->setLocale('fr');
        return $this->render('translate/index.html.twig');
    }
}

and this is my template: 这是我的模板:

{% extends 'base.html.twig'%}
{% block body %}
    <p>
        {{ 'hello' | trans }}
    </p>
    <p>
        {% trans %}hello{% endtrans %}
    </p>
{% endblock %}

I think that the problem is when you set the locale. 我认为问题出在您设置语言环境时。 You can see in this tutorial that the locale must be setted before any other parts of the system. 您可以在本教程中看到,必须在系统的其他任何部分之前设置语言环境。

In the other hand you can ensure what locale is setted throught the Symfony profiler, in the Translation tab 另一方面,您可以确保在“转换”选项卡中通过Symfony探查器设置了哪种语言环境。

I hope this can help you 希望对您有所帮助

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

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