Symfony2 translator

Featured Image

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

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

framework:
#...
#...
    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
messages.hr.yml for Croatian
etc…

English example:

#MyBundle\Resources\translations\messages.en.yml

missing:
   parameter:
       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!'
 
invalid:
   parameter:
      token: Token parameter is not valid!
 
items:
   result:
      empty: Couldn't get items!

Croatian example:

#MyBundle\Resources\translations\messages.hr.yml

missing:
    parameter:
        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!'
 
invalid:
   parameter:
       token: Token parametar nije ispravan!
 
items:
   result:
       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:

http://ourhost.com/ourroute/en

For Croatian

http://ourhost.com/ourroute/hr

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. 🙂


4 comments

  1. Hello! If you’ll need to convert yaml files to Gettext po or vice versa, there’s a good and free online converter http://yml2po.com/ 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>.