diff --git a/config/services.yaml b/config/services.yaml index a2c36599a..f3c93f4a4 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -31,6 +31,7 @@ services: App\EventSubscriber\LocaleSubscriber: arguments: $defaultLocale: "%kernel.default_locale%" + $locales: "%app.locales%" App\Service\RegionsProvider: arguments: $projectDirectory: "%kernel.project_dir%" diff --git a/src/EventSubscriber/LocaleSubscriber.php b/src/EventSubscriber/LocaleSubscriber.php index c50759294..b1bbed276 100644 --- a/src/EventSubscriber/LocaleSubscriber.php +++ b/src/EventSubscriber/LocaleSubscriber.php @@ -8,11 +8,18 @@ class LocaleSubscriber implements EventSubscriberInterface { - public function __construct(private readonly string $defaultLocale) - { + /** + * @param string[] $locales + * + * @return void + */ + public function __construct( + private readonly string $defaultLocale, + private readonly array $locales + ) { } - public function onKernelRequest(RequestEvent $event) + public function onKernelRequest(RequestEvent $event): void { $request = $event->getRequest(); @@ -36,7 +43,12 @@ public function onKernelRequest(RequestEvent $event) } elseif (\count($request->getLanguages()) > 0) { // if we still don't have a locale defined, use the browser languages $languages = $request->getLanguages(); - $request->setLocale($languages[0]); + foreach ($languages as $lang) { + if (\in_array($lang, $this->locales, true)) { + $request->setLocale($lang); + break; + } + } } else { // or use the default locale $request->setLocale($this->defaultLocale);