rudy
9c746638a3
Some checks failed
Cadoles/hydra-sql/pipeline/pr-develop There was a failure building this commit
42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\EventListener;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
|
|
|
class LocaleSubscriber implements EventSubscriberInterface
|
|
{
|
|
// Langue par défaut
|
|
private string $defaultLocale;
|
|
|
|
public function __construct(string $defaultLocale)
|
|
{
|
|
$this->defaultLocale = $defaultLocale;
|
|
}
|
|
|
|
public function onKernelRequest(RequestEvent $event)
|
|
{
|
|
$request = $event->getRequest();
|
|
if (!$request->hasPreviousSession()) {
|
|
return;
|
|
}
|
|
|
|
// On vérifie si la langue est passée en paramètre de l'URL
|
|
if ($locale = $request->query->get('_locale')) {
|
|
$request->setLocale($locale);
|
|
} else {
|
|
// Sinon on utilise celle de la session
|
|
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
|
|
}
|
|
}
|
|
|
|
public static function getSubscribedEvents()
|
|
{
|
|
return [
|
|
KernelEvents::REQUEST => [['onKernelRequest', 20]],
|
|
];
|
|
}
|
|
}
|