Symfony2: Translations

To get the Translator you must use the dependecy injector inside the controller, basically you do this:


You must have it enabled on the config:

    translator: { fallback: en }

Now you put all your translations dictionaries on the path:

  • YourApp/YourBundle/Resources/translations
  • Or… if you want to override definitions from other bundles, you put them on app/Resources/translations
There will be one translation file per language, and it can be in PHP, YML or XLIFF format. The naming convention is: DOMAIN.LOCALE.FORMAT (example:  — Messages in spanish using xliff format )
Example (
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file source-language="en" datatype="plaintext" original="file.ext">
            <trans-unit id="1">
                <target><![CDATA[ <strong>Symfony2</strong> esta bueno. ]]></target>

And on the controller you call:

        $t = $this->get('translator')->trans('') ;
        return new Response($t);

You will get the message according to the current Locale language (in this case “es”)

Another cool feature from Symfony is the possibility to set the language in the URL of each page and automaticaly set the correct Locale on the session, to do that you use the _locale feature in the routes:

     * @extra:Route("/{_locale}/YourRoute", name="_testing", requirements={"_locale"="en|fr|es"})

In the above example, depending on the value of _locale symfony will automatically set the correct language, so you dont have to use setLocale on the Translator!

Read more from the official documentation.