Search code examples
symfonyinternationalizationrequestlocale

Symfony2 locale of user not retrieved


I want to get the user locale when he lands in my website and then stick it to the user (also stick the new one if he wants to change the language).

Yet I don't want the locale to appear in the url.

I implemented the LocaleListener from the Symfony2 doc but I am enable to get the user default locale at the first request.

This requests are giving me nothing for a response:

 $locale = $this->getRequest()->attributes->get('_locale');
 $locale = $this->getRequest()->get('_locale');

While

$this->getRequest()

Sends effectively

GET /Twinkler1.2.3/web/app_dev.php/ HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip,deflate,sdch Accept-Language: fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 Cache-Control: max-age=0 Connection: keep-alive Cookie: __uvt=; PHPSESSID=f28e3958ecab05fe97d6fc6950eb72ec; SQLiteManager_currentLangue=2 Host: localhost:8888 Referer: http://localhost:8888/Twinkler1.2.3/web/app_dev.php/login User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36 X-Php-Ob-Level: 0

So how can I get the locale of the request (French here)?

Thanks in advance Jules


Solution

  • $language = $request->getPreferredLanguage();
    $request->setLocale($language);