Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Symfony ignore the browsers locale-setting (HTTP-Request Accept-Language Header)?

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) :)

like image 273
Senči Avatar asked Aug 27 '12 10:08

Senči


People also ask

Is accept-language a standard HTTP header?

The Accept-Language request HTTP header indicates the natural language and locale that the client prefers. The server uses content negotiation to select one of the proposals and informs the client of the choice with the Content-Language response header.

What is accept-language in HTTP?

The Accept-Language header is information about the user's language preferences that is passed via HTTP when a document is requested. Mainstream browsers allow these language preferences to be modified by the user.


1 Answers

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')));
like image 192
Carlos Granados Avatar answered Sep 17 '22 12:09

Carlos Granados