<?php
declare(strict_types=1);
namespace Hitso\Bundle\MultiSiteBundle\EventListener;
use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class GraphQlLocaleSubscriber implements EventSubscriberInterface
{
/**
* @var SiteContext
*/
protected $context;
public function __construct(SiteContext $context)
{
$this->context = $context;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
['onKernelRequest', 190],
],
];
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!in_array($request->getPathInfo(), ['/graphql']) || !$request->headers->has('locale')) {
return;
}
$locale = $request->headers->get('locale');
// $request->setLocale($locale);
// $request->setDefaultLocale($locale);
$this->context->getContentSite()->setLocale($locale);
$this->context->getRunningSite()->setLocale($locale);
}
}