Symfony2.2: default_locale always used in twig translations

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: #esi: ~ translator: { fallback: %locale% } # = en # ... default_locale: %locale% # = en 

Controller:

 public function indexAction() { $this->get('session')->set('_locale', 'fr'); $this->getRequest()->setLocale($lang); exit($this->getRequest()->getLocale()); // = fr exit($this->get('translator')->trans('Symfony2 is great')); // = Symfony2 est génial return $this->render('TestBundle:Controller:test.html.twig'); 

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)

+4
source share
2 answers

I had the same problem due to the low priority of my event listener. The locale will be canceled by the Translator TranslatorListener application. Increasing the priority of my event listener did the trick for me:

 services: app.locale_listener: class: AppBundle\EventListener\LocaleListener tags: - { name: kernel.event_listener, priority: 11, ... } 

Source: https://github.com/symfony/symfony/issues/12878#issuecomment-68628808

+1
source

The _locale parameter in routing supports your language value.

Look here on this page.

Symfony - Book - Translation - Local and URL

From Symfony 2.1, they have this logic:

Because you can keep the user's locale in the session, you might be tempted to use the same URL to display the resource in different languages ​​based on the user locale. For example, http://www.example.com/contact may display English content for one user and French for another user. Unfortunately, this violates the fundamental rule of the Web: that a particular URL returns the same resource, regardless of the user. To further drown out the problem, which version of the content will be indexed by search engines?

The best policy is to include the language in the URL. This is fully supported by the routing system using the llocale special parameter:

Now that you want it to be local, it no longer works

 $this->get('session')->set('_locale', 'fr'); 

Now you can use the insted query of the session, but you cannot have the session logic with _local from Symfony 2.0 unless you model it using the event listener upon request from the kernel.

0
source

All Articles