Symfony2 translator

Symfony2 translator

Using built – in translator with Symfony2 project is pretty simple.

First make sure you enabled it in main config file: config.yml

    translator:    { fallback: en }

Then inside: YourBundle\Resources\translations\ folder make yaml file that will hold localized messages for each language:

messages.en.yml for English for Croatian

English example:


       first_name: Missing parameter 'first_name'
       last_name: Missing parameter 'last_name'
       email: Missing parameter 'e-mail'
       username: Missing parameter 'username'
       password: Missing parameter 'password'
       token: Missing parameter 'token'
       lat: Missing parameter 'lat'
       lon: Missing parameter 'lon'
       device_id: Missing parameter 'device_id'
       appname: Missing parameter 'appname'
       hash: Missing parameter 'hash'
       activation_key: Missing parameter 'activation key!'
       dummy: Missing parameter 'dummy!'
      token: Token parameter is not valid!
      empty: Couldn't get items!

Croatian example:


        first_name: Nedostaje parametar 'first_name'
        last_name: Nedostaje parametar 'last_name'
        email: Nedostaje parametar 'e-mail'
        username: Nedostaje parametar 'username'
        password: Nedostaje parametar 'password'
        token: Nedostaje parametar 'token'
        lat: Nedostaje parametar 'lat'
        lon: Nedostaje parametar 'lon'
        device_id: Nedostaje parametar 'device_id'
        appname: Nedostaje parametar 'appname'
        hash: Nedostaje parametar 'hash'
        activation_key: Nedostaje parametar 'activation key!'
        dummy: Missing parameter 'dummy!'
       token: Token parametar nije ispravan!
       empty: Ne mogu prona?i niti jednu stavku!

After defining translations, we can go inside any controller and use translations and messages will be translated into visitor’s language. Inside URL we may specify default _locale parameters and if none provided, fallback locale (that we defined inside main config.yml file) will be used by default. Also we can specify one inside each route that visitors call:

For example:

For English:

For Croatian

Inside controller everything is simple as possible:

$tr = $this->get('translator');
$message = $tr->trans('missing.parameter.token');
return new Response($message);

That’s it for now. Cheers. 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Creating simple Symfony2 service Darko Goles
Darko Goles, | 9

Creating simple Symfony2 service

Symfony2 Assetic Darko Goles
Darko Goles, | 6

Symfony2 Assetic

Doctrine DBAL with Symfony2 Darko Goles
Darko Goles, | 4

Doctrine DBAL with Symfony2


  1. Hello! If you’ll need to convert yaml files to Gettext po or vice versa, there’s a good and free online converter to consider using. Just upload your files with one of these extensions and the tool will make the conversion.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.