18

i am currently trying to enable the translator in Symfony 2.0. Symfony is ignoring the Accept-Language Header variable and is using default_locale (and when that is not defined the fallback).

My request looks like:

Accept-Language de-DE,de;q=0.8,en-us;q=0.5,en;q=0.3

but $this->getRequest()->getLocale(); gets me en with that same request.

Can somebody tell me what may be wrong?

Yes, I have tried to clear the cache and deleting my cookies (omnomnom) :)

Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Senči
  • 911
  • 2
  • 10
  • 25

1 Answers1

26

This is the expected behaviour. Symfony does not by default use the Accept Language header and instead relies on the symfony configuration for locale settings. In fact, it is advised not to use the same URL for content in different locales, see this document:

Symfony 2 The Book - Translations - The Locale and the URL

But if you want to ignore this advice and use the Accept language header, you can do it with this code in your controller:

$request = $this->getRequest();
$session = $this->get('session');

$session->setLocale($request->getPreferredLanguage(array('de', 'en')));
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
Carlos Granados
  • 11,273
  • 1
  • 38
  • 44
  • 2
    is there a possibility to execute that code in every controller without actually writing it in every controller? I mean with writing it just once? – Senči Aug 27 '12 at 11:55
  • 2
    You could write a listener for the kernel.request event and do it there. – Carlos Granados Aug 27 '12 at 12:14
  • if you are still there, please see link to The locale and the url, and tell me where I am supposed to set: "contact: path: /{_locale}/contact"? Also, does it mean I need to do for each single view I may have? thks so much – mario May 14 '14 at 16:46