简体   繁体   中英

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

i created a xml catalog for translate for french language named messages.fr.xlf and put it into 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

I hope this can help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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