Silex and Localization (i18n/L10n): TranslationServiceProvider

Written by on Tuesday March 19, 2013 - Permalink -
15017435-flags-globeI've been using Silex* in a large variety of projects in the last year, and I always find something new to make my life easier. Last week, I was dealing with internationalization / localization (i18n / L10n) - a quite relevant topic when talking about web projects aiming a high audience. This post shows how to make an easy structure who automatically loads language files written in YAML, using the TranslationServiceProvider. Although the post is focused on Silex, I believe it's also compliant with Symfony 2. If you wanna go directly to the code, there's a GitHub repo with the whole example (a basic silex application) here: https://github.com/erikaheidi/silex-L10nex For those of you who didn't have the opportunity to try Silex before, I have a post here covering the basics, and a Pagodabox quickstart with a Silex barebone using Twitter Bootstrap for front-end. Silex is a micro framework based on Symfony 2.

Structure

Before going any further, let's talk about project structure. With Silex, you have a quite liberty to choose whatever structure works better for your project. To make things easier, let's consider the following directory structure and files:
  • src/
    • locale/
      • en/
        • app.yml
      • pt-BR/
        • app.yml
    • templates/
    • app.php
    • bootstrap.php
  • composer.json
  • composer.phar
  • www/
  • vendor/
You can see it better here: https://github.com/erikaheidi/silex-L10nex (without the vendor directory, which is installed when you hit composer install ) .

The YAML translation files

The basic usage of the TranslationServiceProvider loads the language strings via arrays, but of course this is impracticable (unless you only want to translate a really small amount of strings). We're going to use YAML translation files, making an automatic loader which reads a directory. For accomplishing that, we're going to need 2 more components provided by Symfony: Config e Yaml. This is the content inside the app.yaml translation file, inside the pt-BR folder:
locale_lang: Português
Welcome: Bem Vindo
msg_example: Esse é um exemplo de texto traduzido.

Registering the ServiceProviders

First of all, you need to update your composer.json with the new dependencies. We will be working also with Twig and Sessions: Heads Up: to use the trans filter on Twig templates, you need the TwigBridge component as well. For registering the SP, this code shall go in your bootstrap.php (or wathever place you use for the app initialization):

Loading the languages

Now we need to load the language files. You could load everything on bootstrap.php, but this would cause an unnecessary load, since we just need one language each time the page is loaded. Of course this must be done at the beginning of the application code.

Changing the current language

Now lets create a route to actually change the current language. You could also do this automatically after checking request/browser headers.

Using on twig templates

Its incredibly easy to use the translation strings on twig templates. As said before, just make sure you also include TwigBridge as a dependency in your composer.json. You just need to use the trans filter. Just like this: {{ 'Welcome'|trans }} See the template code here: https://github.com/erikaheidi/silex-L10nex/blob/master/src/templates/index.twig.html The example app is available here: https://github.com/erikaheidi/silex-L10nex If you are cloning the repo, don't forget to run
php composer.phar install
to install the dependencies (vendor directory).
comments powered by Disqus