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?
You need not just set the new locale but also to redirect the user after you do that for changes to take effect.
Also, it's not a good practice to store the locale in the session — you should keep the locale in the URL. This way a redirect to a URL with a proper locale makes even more sense.
Well thanks to Elnur I decided to go back to my jms_i18n solution (see comments in Elnur's answer), and I found this question that helped me build my own solution. So I switched from an EventSubscriber extended class to a simple "unextended" class of my own. Here is the working code:
And here is how to register it in services :
Many thanks to Elnur for changing my mind.
I am not sure if this will work. But when user is logged in locale will be set in his/her session.
you can do something like this in your listener:
and on link where user will select locale you can create some action
I just find out that you have a problem with your listener and specially in this:
You are requesting for hasPreviousSession and it has but it doesnt has the locales I don't know what cause this but this is the major problem on the first request. If you change it in:
It will work like a charm :)