My website is running Symfony, master version. So far, I was able to use the LocalListener logic from the website, with a slight difference due to code not being compatible with my version. (I think) I only simplified the onKernelRequest method this way:
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($locale = $request->get('_locale')) {
$request->getSession()->set('_locale', $locale);
}
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
That way, I could put up a simple language selector on my page, using these paths, and the new language would apply at the first request. (it would not happen if I left the "else" condition)
Then I wanted to take account of the locale stored in user accounts, in case the user is logged in and has specified a locale in his or her profile. So I added this piece of code in the function :
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
$token = $this->container->get('security.context')->getToken();
if (is_object($token)) {
$user = $token->getUser();
if (is_object($user)) {
$userlocale = $user->getLocale();
if ($userlocale) {
$request->getSession()->set('_locale', $userlocale);
$request->setLocale($userlocale);
return;
}
}
}
if ($locale = $request->get('_locale')) {
$request->getSession()->set('_locale', $locale);
}
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
}
(EDIT: sorry for poor indentation, somehow stackoverflow does not want to indent it properly...)
Basically it checks if a user is logged in, and if there is, if he or she has set a locale, and if they have, sets the locale to the user locale instead. Now this works, but... not instantly. Whenever I log in or change my locale in my profile, the next page I get to is still in the previously set locale. Only when I load a new page does it change its translations properly, and stays that way for the next requests.
So here is my question: is there something I am supposed to add to make this change occur on those post-login and post-profile-edit requests?