Enabling Po/Mo translations in Symfony 2.0

Those of you we are following Symfony 2.1 development might have noticed that the Translator component has been upgraded with some new FileReaders supporting more formats than the original xliff, php and yml ones. Symfony policy is to not backport new features into previous stable versions but we might need to enable them in our Symfony 2.0 projects.

At my new job at SocialPoint we needed to integrate Po/Mo translations in our new Symfony2 projects. And this is how to do so:

1) Obtain new Files from Symfony2 master branch. In our case, we needed Po and Mo:

Mo FileLoader
Po FileLoader

2) Re-create Symfony/Component/Translation/Loader folder structure under our src Folder. This way Symfony2 Universal Class Loader will still work but files can be outside of symfony vendor.

3) Modify our config.yml or services.yml with:

services:
    translation.loader.po:
        class: Symfony\Component\Translation\Loader\PoFileLoader
        tags:
            - { name: translation.loader, alias: po }
    translation.loader.mo:
        class: Symfony\Component\Translation\Loader\MoFileLoader
        tags:
            - { name: translation.loader, alias: mo }

4) And that’s it! Now we can use messages.en.po files inside our translations folder and they will be processed as xliff and yml! Also note, that when Symfony 2.1 is out, you will need to remove this as it will be supported by default distribution. But at least, this way we can start enjoying these new features!

You may also like...

3 Responses

  1. cordoval says:

    nice trick, thought you were not working on sf2 anymore, heh, ok cool. thanks!

  2. Yuriy says:

    Thank you man. Your post save me a lot of time

  3. This specific blog post Enabling Po/Mo translations
    in Symfony 2.0 | PHPLand y otros pensamientos, contains really very good advice and I actually realized specifically
    what I had been researching for. Thanks.