<?php
// src/AppBundle/EventListener/SwitchUserListener.php
namespace App\EventListener;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\SecurityEvents;
class SwitchUserListener implements EventSubscriberInterface
{
public function onSwitchUser(SwitchUserEvent $event)
{
$event->getRequest()->getSession()->set(
'_locale',
// assuming your User has some getLocale() method
$event->getTargetUser()->getLocale()
);
}
public static function getSubscribedEvents(): array
{
return array(
// constant for security.switch_user
SecurityEvents::SWITCH_USER => 'onSwitchUser',
);
}
}