<?php
namespace CustomBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class LocaleListener implements EventSubscriberInterface
{
private $translator;
public function __construct($translator)
{
$this->translator = $translator;
}
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
$lang = $request->query->get('lang', null);
if ($lang) {
$request->getSession()->set('_locale', $lang);
$request->setLocale($request->getSession()->get('_locale', $lang));
}
}
public static function getSubscribedEvents()
{
// TODO: Implement getSubscribedEvents() method.
return [
KernelEvents::REQUEST => [['onKernelRequest', 20]]
];
}
}