<?php
/*
* Disclaimer: This source code is protected by copyright law and by
* international conventions.
*
* Any reproduction or partial or total distribution of the source code, by any
* means whatsoever, is strictly forbidden.
*
* Anyone not complying with these provisions will be guilty of the offense of
* infringement and the penal sanctions provided for by law.
*
* © 2022 All rights reserved.
*/
namespace App\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* LocaleSubscriber.
*
* @author John Doe <john.doe@projetx.io>
* @package App\Event
*/
class LocaleSubscriber implements EventSubscriberInterface {
/** @var string */
private $defaultLocale;
/**
* LocaleSubscriber constructor.
*
* @param string $defaultLocale
*/
public function __construct(string $defaultLocale = 'fr') {
$this->defaultLocale = $defaultLocale;
}
/**
* @return \array[][]
*/
public static function getSubscribedEvents() {
return [
// must be registered before (i.e. with a higher priority than) the default Locale listener
KernelEvents::REQUEST => [['onKernelRequest', 20]],
];
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event) {
$request = $event->getRequest();
if (!$request->hasPreviousSession()) {
return;
}
if ($request->getSession()->get("_locale") !== null) {
$request->setLocale($request->getSession()->get("_locale"));
}
}
}