<?php
namespace Aqarmap\Bundle\ApiBundle\EventListener;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class LocaleEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
{
private ParameterBagInterface $params;
public function __construct(ParameterBagInterface $params)
{
$this->params = $params;
}
public function onKernelRequest(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
$request = $event->getRequest();
if (!preg_match('/^aqarmap_api_/i', $request->attributes->get('_route'))) {
return;
}
$request->setLocale($request->getPreferredLanguage($this->params->get('jms_i18n_routing.locales')));
}
/**
* @return array<string, mixed>
*/
public static function getSubscribedEvents(): array
{
return [\Symfony\Component\HttpKernel\KernelEvents::REQUEST => 'onKernelRequest'];
}
}