我刚开始学习Symfony。 我试图构建一个多语言的网站,但是在更改区域设置时遇到了问题。
我看了一些帖子并阅读了相关文档,但是区域设置并没有发生变化,我尝试了以下方法:
public function indexAction()
{
$this->get('session')->set('_locale', 'fr');
$request = $this->getRequest();
$locale = $request->getLocale();
return $this->render('PhoneMainBundle:Default:index.html.twig',array('locale'=>$locale));
}
但是$locale中的值总是'en'(我的默认区域设置)
我还尝试过
public function indexAction()
{
$this->get('session')->set('_locale', 'fr');
$request = $this->getRequest();
$request->setLocale('fr');
$locale = $request->getLocale();
return $this->render('PhoneMainBundle:Default:index.html.twig',array('locale'=>$locale));
}
在这种情况下,$locale 是法语,但翻译始终来自 messages.en.yml。
首先,我想使用 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 检测用户的语言环境,也许可以在每个页面刷新时使用监听器?
之后,我将创建一个路由来改变语言环境。但我想找到一种改变语言环境的方法。
感谢您的帮助。
setSession
是什么作用? - PASTAGA