src/Locale/LocaleSelector.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\Locale;
  3. use Symfony\Component\HttpFoundation\RequestStack;
  4. use Symfony\Component\Routing\RouterInterface;
  5. use Symfony\Contracts\Translation\TranslatorInterface;
  6. class LocaleSelector
  7. {
  8.     public const SHOW_LOCALE_POPIN_SESSION_KEY 'SHOW_LOCALE_POPIN_SESSION_KEY';
  9.     public const LOCALE_SELECT_URL_KEY 'setUserLocale';
  10.     private $request;
  11.     public function __construct(
  12.         private readonly string $country,
  13.         RequestStack $requestStack,
  14.         private readonly RouterInterface $router,
  15.         private readonly array $allowedLocales,
  16.         private readonly array $proposedLocales,
  17.         private readonly TranslatorInterface $translator
  18.     ) {
  19.         $this->request $requestStack->getCurrentRequest();
  20.     }
  21.     public function setMustShowLanguagePopin(bool $show): void
  22.     {
  23.         if (!$this->request) {
  24.             return;
  25.         }
  26.         if (count($this->proposedLocales) > 2) {
  27.             return;
  28.         }
  29.         $this->request->getSession()->set(self::SHOW_LOCALE_POPIN_SESSION_KEY$show);
  30.     }
  31.     public function shouldShowLanguagePopin(): bool
  32.     {
  33.         if (!$this->request || !$this->request->getSession() || count($this->proposedLocales) > 2) {
  34.             return false;
  35.         }
  36.         return (bool) $this->request->getSession()->get(self::SHOW_LOCALE_POPIN_SESSION_KEY);
  37.     }
  38.     public function pathToCurrentRouteInLocale($locale): string
  39.     {
  40.         if (!in_array($locale$this->allowedLocales)) {
  41.             throw new \Exception(sprintf('This locale is not allowed in current configuration : %s. Allowed locales are : %s.'$localeimplode(', '$this->allowedLocales)));
  42.         }
  43.         if (!$this->request) {
  44.             return '#';
  45.         }
  46.         $parameters $this->request->get('_route_params');
  47.         $parameters['_locale'] = $locale;
  48.         $parameters[self::LOCALE_SELECT_URL_KEY] = 'true';
  49.         return $this->router->generate(
  50.             $this->request->get('_route'),
  51.             $parameters
  52.         );
  53.     }
  54.     public function getProposedLocales()
  55.     {
  56.         return $this->proposedLocales;
  57.     }
  58.     public function getProposedLocalesWithTranslatedNames()
  59.     {
  60.         $namesByLocale = [];
  61.         foreach ($this->proposedLocales as $localeForKey) {
  62.             $names = [];
  63.             foreach ($this->proposedLocales as $localeForTranslation) {
  64.                 $names[] = $this->translator->trans(sprintf('frontend.locale.%s'$localeForKey), [], null$localeForTranslation);
  65.             }
  66.             $namesByLocale[$localeForKey] = implode(' / '$names);
  67.         }
  68.         return $namesByLocale;
  69.     }
  70. }