src/EventSubscriber/LocaleSubscriber.php line 64

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use App\Admin\Service\LocaleService as AdminLocaleService;
  7. use App\Service\LocaleService as LocaleService;
  8. class LocaleSubscriber implements EventSubscriberInterface
  9. {
  10.     private $_adminLocaleService;
  11.     private $_localeService;
  12.     public function __construct(
  13.             AdminLocaleService $adminLocaleService,
  14.             LocaleService $localeService)
  15.     {
  16.         $this->_adminLocaleService $adminLocaleService;
  17.         $this->_localeService $localeService;
  18.     }
  19.     public static function getSubscribedEvents()
  20.     {
  21.         return [
  22.             KernelEvents::REQUEST => [['onKernelRequest', -10]]
  23.         ];
  24.     }
  25.     public function onKernelRequest(RequestEvent $event)
  26.     {
  27.         $request $event->getRequest();
  28.         if (/*!$request->hasPreviousSession() || */$request->get('_route') == '')
  29.             return;
  30.         if (strpos($request->get('_route'), 'admin_') === 0)
  31.         {
  32.             $locale $request->getSession()->get('_backend_locale');
  33.             if ($locale == '')
  34.             {
  35.                 $clientLocale $this->_getClientLocale();
  36.                 
  37.                 if ($clientLocale != '')
  38.                 {
  39.                     if (in_array($clientLocale$this->_adminLocaleService->getAvailableBackendLocales()))
  40.                         $locale $clientLocale;
  41.                 }
  42.             }
  43.             
  44.             if ($locale == '')
  45.                 $locale $this->_adminLocaleService->getDefaultLocale();
  46.             
  47.             $this->_adminLocaleService->setLocale($locale);
  48.             $this->_adminLocaleService->setContentLocale($this->_adminLocaleService->getContentLocale());
  49.         }
  50.         else
  51.         {
  52.             $locale $request->attributes->get('_locale');
  53.             if ($locale == '')
  54.                 $locale $request->getSession()->get('_locale');
  55.             if ($locale == '')
  56.             {
  57.                 $clientLocale $this->_getClientLocale();
  58.                 
  59.                 if ($clientLocale != '')
  60.                 {
  61.                     if (in_array($clientLocale$this->_localeService->getAvailableLocales()))
  62.                         $locale $clientLocale;
  63.                 }
  64.             }
  65.             
  66.             if ($locale == '')
  67.                 $locale $this->_localeService->getDefaultLocale();
  68.             $this->_localeService->setLocale($locale);
  69.         }
  70.         
  71.     }
  72.     
  73.     private function _getClientLocale()
  74.     {
  75.         if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  76.             return strtolower(str_split($_SERVER['HTTP_ACCEPT_LANGUAGE'], 2)[0]);
  77.     }
  78. }