Having some issue changing the locale on a symfony 2.1 website.
I can't find a way to be able to change the lang without using the _locale
on every routes. I know this is against the fundamental rule, but this content will for example not be indexed by engine as it is member only.
Typically, I would like a simple method to be able to change the locale on the request (BC from version 2.1), or on the session, but can't figure out how to do that smoothly. I also would like to avoid the use of a Listener for that.
config.yml file :
framework:
translator: { fallback: %locale% }
session:
routing.yml file :
route_change_lang:
pattern: /changelang/{newlang}
defaults: { _controller: AcmeCoreBundle:Default:switchLanguage, newlang: en }
requirements:
newlang: en|fr|de
Simple action to update the locale of the router :
public function switchLanguageAction($newlang)
{
$request = $this->getRequest();
$request->setLocale($newlang);
$referer_url = $this->get('request')->headers->get('referer');
if ($referer_url != null) {
return $this->redirect($referer_url);
} else {
return $this->redirect($this->generateUrl('route_home'));
}
}
What is the problem? I guess it is related to the default_locale set in the main config.yml file, but documentation is not really clear, any help/hint appreciated