I have a strange problem with Symfony2.2. I have a project using two languages: en / fr. Therefore, I create as usual (for example, Symfony2.0) two translation files "messages.en.yml" and "messages.fr.yml" in Ressources / Views / translations /. But translations in twig cannot change, even if we install the request object and the local session. Translation is always specified by the default_locale parameter (config.php).
Example: if default_locale = en, my entire website (in twig) translates to en, even if I set the _locale object to fr (request and session). Of course, if I manually change default_locale to fr, the website is naturally in fr ...
However, the llocale session works, but I don’t know if the locale request works, and, of course, the translation works in controllers too ...
There are my files:
config.yml:
framework:
Controller:
public function indexAction() { $this->get('session')->set('_locale', 'fr'); $this->getRequest()->setLocale($lang); exit($this->getRequest()->getLocale());
View:
{% block content %} <p>lang : {{ app.request.locale }}</p> {#} = "fr", OK{#} <p>{{ 'Symfony2 is great'|trans }}</p> {#} = "Symfony2 is great", WAIT WHAT?{#}
I have to step down to force the locale at the beginning of the method controller to have the requested language (stored in the session) as follows:
Controller:
if($this->get('session')->get('_locale')){ $lang = $this->get('session')->get('_locale'); $this->getRequest()->setLocale($lang); }
In other words, I had a problem registering the request object ... Since the last code works well in the controller and shows the language well on the twig page using app.request.locale, but not translations ... (sorry for my bad English and thanks for help)