$enabledLocales */ public function __construct( #[Autowire('%kernel.enabled_locales%')] private readonly array $enabledLocales, ) { } /** Remembers the chosen language in a cookie and returns to the page it was chosen on. */ #[Route('/language/{locale}', name: 'language_switch', requirements: ['locale' => '[a-z]{2}'])] public function switch(string $locale, Request $request): RedirectResponse { if (!\in_array($locale, $this->enabledLocales, true)) { throw $this->createNotFoundException(); } // Only ever back to a path on this site — never an open redirect. $back = (string) $request->query->get('back', '/'); if (!str_starts_with($back, '/') || str_starts_with($back, '//') || str_contains($back, '\\')) { $back = '/'; } $response = $this->redirect($back); $response->headers->setCookie(Cookie::create(LocaleSubscriber::COOKIE, $locale, strtotime('+1 year'), '/', null, $request->isSecure(), false, false, Cookie::SAMESITE_LAX)); return $response; } }