src/EventSubscriber/LocaleSubscriber.php line 64
<?phpnamespace App\EventSubscriber;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\KernelEvents;use App\Admin\Service\LocaleService as AdminLocaleService;use App\Service\LocaleService as LocaleService;class LocaleSubscriber implements EventSubscriberInterface{private $_adminLocaleService;private $_localeService;public function __construct(AdminLocaleService $adminLocaleService,LocaleService $localeService){$this->_adminLocaleService = $adminLocaleService;$this->_localeService = $localeService;}public static function getSubscribedEvents(){return [KernelEvents::REQUEST => [['onKernelRequest', -10]]];}public function onKernelRequest(RequestEvent $event){$request = $event->getRequest();if (/*!$request->hasPreviousSession() || */$request->get('_route') == '')return;if (strpos($request->get('_route'), 'admin_') === 0){$locale = $request->getSession()->get('_backend_locale');if ($locale == ''){$clientLocale = $this->_getClientLocale();if ($clientLocale != ''){if (in_array($clientLocale, $this->_adminLocaleService->getAvailableBackendLocales()))$locale = $clientLocale;}}if ($locale == '')$locale = $this->_adminLocaleService->getDefaultLocale();$this->_adminLocaleService->setLocale($locale);$this->_adminLocaleService->setContentLocale($this->_adminLocaleService->getContentLocale());}else{$locale = $request->attributes->get('_locale');if ($locale == '')$locale = $request->getSession()->get('_locale');if ($locale == ''){$clientLocale = $this->_getClientLocale();if ($clientLocale != ''){if (in_array($clientLocale, $this->_localeService->getAvailableLocales()))$locale = $clientLocale;}}if ($locale == '')$this->_localeService->getDefaultLocale();$this->_localeService->setLocale($locale);}}private function _getClientLocale(){if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))return strtolower(str_split($_SERVER['HTTP_ACCEPT_LANGUAGE'], 2)[0]);}}