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:

        class: Symfony\Component\Translation\Loader\PoFileLoader
            - { name: translation.loader, alias: po }
        class: Symfony\Component\Translation\Loader\MoFileLoader
            - { 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...