<?php
namespace AppBundle\Listener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;
class LocaleListener extends AbstractSecurityDisabledListener implements EventSubscriberInterface
{
private $defaultLocale;
private $container;
public function __construct($container, $defaultLocale)
{
$this->container = $container;
$this->defaultLocale = $defaultLocale;
}
public function onKernelRequest(RequestEvent $event)
{
if (HttpKernelInterface::MAIN_REQUEST != $event->getRequestType()) {
return;
}
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
$locale = $request->getSession()->get('_locale', $request->cookies->get('_last_logout_locale') ?: $this->defaultLocale);
$request->setLocale($locale);
}
public static function getSubscribedEvents()
{
return [
// must be registered after the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 15]],
];
}
}