<?php
namespace AppBundle\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LocaleSubscriber implements EventSubscriberInterface
{
/**
* @var string
*/
private $defaultLocale;
/**
* @param string|null $defaultLocale
*/
public function __construct(?string $defaultLocale = 'en')
{
$this->defaultLocale = $defaultLocale;
}
/**
* @param GetResponseEvent $event
*
* @return void
*/
public function onKernelRequest(GetResponseEvent $event): void
{
$request = $event->getRequest();
if (preg_match('/^\/admin\//', $request->getPathInfo())) {
$request->attributes->set('_locale', $this->defaultLocale);
}
}
/**
* @return array
*/
public static function getSubscribedEvents(): array
{
return [
// Must be registered before (i.e. with a higher priority than) the default Locale listener.
KernelEvents::REQUEST => [['onKernelRequest', 20]],
];
}
}