src/EventSubscriber/LocaleSubscriber.php line 52

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Entity\User;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. final class LocaleSubscriber implements EventSubscriberInterface
  10. {
  11.     private const SUPPORTED_LOCALES = ['fr''en'];
  12.     public function __construct(
  13.         private readonly Security $security,
  14.         private readonly string $defaultLocale 'en'
  15.     )
  16.     {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  22.         ];
  23.     }
  24.     public function onKernelRequest(RequestEvent $event): void
  25.     {
  26.         if (!$event->isMainRequest()) {
  27.             return;
  28.         }
  29.         $request $event->getRequest();
  30.         $routeLocale $request->attributes->get('_locale');
  31.         if (is_string($routeLocale) && $this->isSupportedLocale($routeLocale)) {
  32.             $normalizedRouteLocale $this->normalizeLocale($routeLocale);
  33.             $request->setLocale($normalizedRouteLocale);
  34.             if ($request->hasSession()) {
  35.                 $request->getSession()->set('_locale'$normalizedRouteLocale);
  36.             }
  37.             return;
  38.         }
  39.         if ($request->hasSession()) {
  40.             $sessionLocale $request->getSession()->get('_locale');
  41.             if (is_string($sessionLocale) && $this->isSupportedLocale($sessionLocale)) {
  42.                 $request->setLocale($this->normalizeLocale($sessionLocale));
  43.                 return;
  44.             }
  45.         }
  46.         $authenticatedUser $this->security->getUser();
  47.         if ($authenticatedUser instanceof User) {
  48.             $userLocale $authenticatedUser->getLanguage();
  49.             if (is_string($userLocale) && $this->isSupportedLocale($userLocale)) {
  50.                 $normalizedUserLocale $this->normalizeLocale($userLocale);
  51.                 $request->setLocale($normalizedUserLocale);
  52.                 if ($request->hasSession()) {
  53.                     $request->getSession()->set('_locale'$normalizedUserLocale);
  54.                 }
  55.                 return;
  56.             }
  57.         }
  58.         $request->setLocale($this->normalizeLocale($this->defaultLocale));
  59.     }
  60.     private function isSupportedLocale(string $locale): bool
  61.     {
  62.         return in_array($this->normalizeLocale($locale), self::SUPPORTED_LOCALEStrue);
  63.     }
  64.     private function normalizeLocale(string $locale): string
  65.     {
  66.         return strtolower(substr($locale02));
  67.     }
  68. }