I am trying to have a language switcher on my symfony 2.1 website.
I followed the official documentation, set the translation files but setting the locale with $request->setLocale('en_US'); doesn't seem to work. After some research, I found this question which provides a beginning of an answer with this listener technique.
However, I still don't manage to have it working, I'm not so sure about my listener declaration, is something wrong with it?
My controller:
public function englishAction(Request $request)
{
$this->get('session')->set('_locale', 'en_US');
return $this->redirect($request->headers->get('referer'));
}
Service declaration in config.yml:
services:
my_listener:
class: "FKMyWebsiteBundleListenerLocaleListener"
My routing:
homepage:
pattern: /{_locale}
defaults: { _controller: FKMyWebsiteBundle:Default:index, _locale: en }
requirements:
_locale: en|fr|cn
about:
pattern: /{_locale}/about
defaults: { _controller: FKMyWebsiteBundle:Default:about, _locale: en }
requirements:
_locale: en|fr|cn
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…