<?php
namespace App\EventListener;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class LocaleListener
* @package App\EventListener
*/
class LocaleListener
{
/**
* @var TranslatorInterface
*/
private $translator;
/**
* @var
*/
private $containerBag;
/**
* LocaleListener constructor.
* @param TranslatorInterface $translator
*/
public function __construct(TranslatorInterface $translator, ContainerBagInterface $containerBag)
{
$this->translator = $translator;
$this->containerBag = $containerBag;
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event): void
{
$languageCookieIdentifier = $this->containerBag->get('language_cookie_identifier');
if (!$cookieLangCode = $event->getRequest()->cookies->get($languageCookieIdentifier)) {
return;
}
if ($this->translator->getLocale() !== $cookieLangCode) {
$this->translator->setLocale($cookieLangCode);
}
}
}