<?php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use App\Repository\UserRepository;
class RequestListener
{
private $security;
private $repo_user;
private $translator;
public function __construct(UserRepository $repo_user, Security $security, TranslatorInterface $translator)
{
$this->repo_user = $repo_user;
$this->security = $security;
$this->translator = $translator;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
// The isMainRequest() method was introduced in Symfony 5.3.
// In previous versions it was called isMasterRequest()
if ($event->isMainRequest()) {
$logged_user = $this->security->getUser();
if(!is_null($logged_user)) {
$request->setLocale($logged_user->getLangage());
$this->translator->setLocale($logged_user->getLangage());
}
}
// ...
}
}
?>