Symfony2基于浏览器设置主页中的区域设置';s配置
我的本地网站在我的网站上随处可见,但我无法告诉应用程序在应用程序根上设置正确的本地 例如: 如果我的网站是我想要的,当我输入这个地址时,应用程序猜测什么是我的本地地址,并将其设置在url的末尾 如果我的应用程序中存在本地文件。这里是Symfony2基于浏览器设置主页中的区域设置';s配置,symfony,localization,translation,Symfony,Localization,Translation,我的本地网站在我的网站上随处可见,但我无法告诉应用程序在应用程序根上设置正确的本地 例如: 如果我的网站是我想要的,当我输入这个地址时,应用程序猜测什么是我的本地地址,并将其设置在url的末尾 如果我的应用程序中存在本地文件。这里是en或fr,如果不是默认的en 现在,当我进入主页时,我总是得到英文版,但我的浏览器设置为法文版 routing.yml: 请看几天前提出的问题,并查看语言侦听器中的第一个方法。它正是你所需要的。并在config.yml中注意正确的设置,借助于许多其他与locale和
en
或fr
,如果不是默认的en
现在,当我进入主页时,我总是得到英文版,但我的浏览器设置为法文版
routing.yml:
请看几天前提出的问题,并查看语言侦听器中的第一个方法。它正是你所需要的。并在config.yml中注意正确的设置,借助于许多其他与locale和symfony2相关的问题,我终于得到了想要的!我想这不是一个完美的答案,但至少它是有效的 我的着陆动作在我的路线中定义为
\u欢迎
路线。当我转到url的根目录时,将加载此操作
/**
* @Route("/landing")
* @Template()
*/
public function landingAction() {
$localeAvailable = array('en', 'fr');
//user language
$localeUser = substr($this->getRequest()->getPreferredLanguage(), 0, 2);
//application language
$localeApp = $this->getRequest()->attributes->get('_locale');
//Redirect to the good locale page
//only if the locale user is on our manager locale and it is not the current locale of the application
if(in_array($localeUser, $localeAvailable) && $localeApp!=$localeUser){
return $this->redirect($this->generateUrl("_welcome", array('_locale' => $localeUser)));
}
return array();
}
谢谢你的帮助,我以前问过这个问题,这是答案的一部分!我投了更高的票
/**
* @Route("/landing")
* @Template()
*/
public function landingAction() {
$localeAvailable = array('en', 'fr');
//user language
$localeUser = substr($this->getRequest()->getPreferredLanguage(), 0, 2);
//application language
$localeApp = $this->getRequest()->attributes->get('_locale');
//Redirect to the good locale page
//only if the locale user is on our manager locale and it is not the current locale of the application
if(in_array($localeUser, $localeAvailable) && $localeApp!=$localeUser){
return $this->redirect($this->generateUrl("_welcome", array('_locale' => $localeUser)));
}
return array();
}