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

耗尽温柔 提交于 2019-12-03 23:22:16
Carlos Granados

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')));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!