Translations
Symfony2 provides some basic mechanism to make translations easier. In our application, we enabled translations in the previous chapter as FOSUserBundle
was using this feature to present its interface.
As a reminder, to enable translation we need to uncomment it in the config.yml
file:
framework: #esi: ~ translator: { fallbacks: ["%locale%"] }
Using translations in a controller
Translator in Symfony2 is a service, just like almost anything else. It means you can use it in any controller, and you can inject it into your own services. You can also access it in Twig, as it was presented in the previous chapter.
The translation service exposes a method called trans
. As an example, to use it in a controller we need to call the translation service:
$this->get('translator')->trans('Hello user');
The trans
method can accept four parameters as follows:
trans(id, parameters, domain, locale)
The first parameter is the string to be translated...