src/Controller/System/LocaleSwitchController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\System;
  3. use Sylius\Bundle\ShopBundle\Locale\LocaleSwitcherInterface;
  4. use Sylius\Component\Locale\Context\LocaleContextInterface;
  5. use Sylius\Component\Locale\Provider\LocaleProviderInterface;
  6. use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\Exception\HttpException;
  11. final class LocaleSwitchController
  12. {
  13.     /** @var EngineInterface */
  14.     private $templatingEngine;
  15.     /** @var LocaleContextInterface */
  16.     private $localeContext;
  17.     /** @var LocaleProviderInterface */
  18.     private $localeProvider;
  19.     /** @var LocaleSwitcherInterface */
  20.     private $localeSwitcher;
  21.     public function __construct(
  22.         EngineInterface $templatingEngine,
  23.         LocaleContextInterface $localeContext,
  24.         LocaleProviderInterface $localeProvider,
  25.         LocaleSwitcherInterface $localeSwitcher
  26.     ) {
  27.         $this->templatingEngine $templatingEngine;
  28.         $this->localeContext $localeContext;
  29.         $this->localeProvider $localeProvider;
  30.         $this->localeSwitcher $localeSwitcher;
  31.     }
  32.     public function renderAction(): Response
  33.     {
  34.         return $this->templatingEngine->renderResponse('@SyliusShop/Menu/_localeSwitch.html.twig', [
  35.             'active' => $this->localeContext->getLocaleCode(),
  36.             'locales' => $this->localeProvider->getAvailableLocalesCodes(),
  37.         ]);
  38.     }
  39.     public function switchAction(Request $request, ?string $code null): Response
  40.     {
  41.         $session $request->getSession();
  42.         if (null === $code) {
  43.             if ($session->has('localeCode')) {
  44.                 $code $session->get('localeCode');
  45.                 if ($code == 'en_US') {
  46.                     $code 'en';
  47.                 } elseif ($code == 'ro_RO') {
  48.                     $code 'ro';
  49.                 }
  50.             } else {
  51.                 $code $this->localeProvider->getDefaultLocaleCode();
  52.             }
  53.         }
  54.         if (!in_array($code$this->localeProvider->getAvailableLocalesCodes(), true)) {
  55.             $code 'ro';
  56.         }
  57.         $session->set('localeCode'$code);
  58.         return $this->localeSwitcher->handle($request$code);
  59.     }
  60. }