<?phpnamespace App\EventListener;use Doctrine\Persistence\ManagerRegistry;use Symfony\Component\HttpKernel\Event\GetResponseEvent;use Symfony\Component\HttpKernel\HttpKernel;use Symfony\Component\HttpKernel\HttpKernelInterface;use Doctrine\ORM\EntityManager;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;use Symfony\Component\HttpKernel\KernelInterface;use Symfony\Contracts\Translation\TranslatorInterface;use Symfony\Component\Translation\LocaleSwitcher;use App\Entity\User;class LocaleListener{ private $container; private LocaleSwitcher $localeSwitcher; public function __construct(KernelInterface $appKernel, ManagerRegistry $doctrine, LocaleSwitcher $localeSwitcher) { $this->appKernel = $appKernel; $this->doctrine = $doctrine; $this->localeSwitcher = $localeSwitcher;// print('<br>LocaleListener __construct '); if($_SERVER['HTTP_HOST'] == 'www.easycommerc.cz' OR $_SERVER['HTTP_HOST'] == 'easycommerc.cz') { $defaultLocale = 'cs'; } elseif($_SERVER['HTTP_HOST'] == 'www.easycommerc.com' OR $_SERVER['HTTP_HOST'] == 'easycommerc.com') { $defaultLocale = 'en'; } elseif($_SERVER['HTTP_HOST'] == 'www.easycommerc.eu' OR $_SERVER['HTTP_HOST'] == 'easycommerc.eu') { $defaultLocale = 'en'; } else { $defaultLocale = 'cs'; } $this->defaultLocale = $defaultLocale; } /** * @throws \ReflectionException */ public function __invoke(RequestEvent $event): void { $request = $event->getRequest();// if (!$event->isMainRequest()) {// // don't do anything if it's not the master request// return;// } $session = $request->getSession();// print('<br>locale listener: '.$request->getSession()->get('lang')->getLangKey());// print('<br>session _locale: '.$request->getSession()->get('_locale')); //$locale = 'en'; //$request->getSession()->set('_locale', $locale); $request = $event->getRequest(); if (!$request->hasPreviousSession()) { return; } // try to see if the locale has been set as a _locale routing parameter if ($locale = $request->attributes->get('_locale')) { $request->getSession()->set('_locale', $locale); } else { // if no explicit locale has been set on this request, use one from the session $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale)); } if(!empty($request->getSession()->get('lang'))) { $locale = $request->getSession()->get('lang')->getLangKey(); } else { $locale = $this->defaultLocale; //$locale = 'cz'; } //$locale = 'en';// print('<br> QQW LocaleListener locale: '.$locale);// $request->setLocale($locale); // you can get the current application locale like this: $currentLocale = $this->localeSwitcher->getLocale();// print('<br> QQW localeSwitcher locale: '.$currentLocale); $this->localeSwitcher->setLocale($locale); $request->getSession()->set('_locale', $locale); }}