From 39433d42729f18abea7b8c9c54b1fe92cd9823e0 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Mon, 26 Jan 2026 17:49:20 +0100 Subject: [PATCH] fix: Fix getting preferred language for disconnected users --- src/EventSubscriber/LocaleSubscriber.php | 15 ++--- src/Service/Locales.php | 46 -------------- tests/Service/LocalesTest.php | 81 ------------------------ 3 files changed, 5 insertions(+), 137 deletions(-) delete mode 100644 tests/Service/LocalesTest.php diff --git a/src/EventSubscriber/LocaleSubscriber.php b/src/EventSubscriber/LocaleSubscriber.php index 6e9d2977..29163d1a 100644 --- a/src/EventSubscriber/LocaleSubscriber.php +++ b/src/EventSubscriber/LocaleSubscriber.php @@ -13,22 +13,17 @@ class LocaleSubscriber implements EventSubscriberInterface { - public function __construct( - private Service\Locales $locales, - ) { - } - public function onKernelRequest(RequestEvent $event): void { $request = $event->getRequest(); - $preferredLocale = $this->locales->getBest($request->getLanguages()); + $locale = $request->getPreferredLanguage(Service\Locales::getSupportedLocalesCodes()); + if ($request->hasPreviousSession()) { - $locale = $request->getSession()->get('_locale', $preferredLocale); - $request->setLocale($locale); - } else { - $request->setLocale($preferredLocale); + $locale = $request->getSession()->get('_locale', $locale); } + + $request->setLocale($locale); } public static function getSubscribedEvents(): array diff --git a/src/Service/Locales.php b/src/Service/Locales.php index 91242c61..ae5c400f 100644 --- a/src/Service/Locales.php +++ b/src/Service/Locales.php @@ -51,50 +51,4 @@ public function isAvailable(string $locale): bool { return isset(self::SUPPORTED_LOCALES[$locale]); } - - /** - * @param string[] $requestedLocales - */ - public function getBest(array $requestedLocales): string - { - // First, look for an exact match in the supported locales: - foreach ($requestedLocales as $locale) { - if (self::isAvailable($locale)) { - return $locale; - } - } - - // Then, create an array to match "super" locales (e.g. en) to - // supported country locales (e.g. en_GB): - $supersToLocales = []; - foreach (self::SUPPORTED_LOCALES as $locale => $localeName) { - $splitLocale = explode('_', $locale, 2); - if (count($splitLocale) < 2) { - continue; - } - - $superLocale = $splitLocale[0]; - if (!isset($supersToLocales[$superLocale])) { - $supersToLocales[$superLocale] = $locale; - } - } - - // And search requested locales in the lookup array: - foreach ($requestedLocales as $locale) { - $splitLocale = explode('_', $locale, 2); - if (count($splitLocale) === 1) { - $superLocale = $locale; - } else { - $superLocale = $splitLocale[0]; - } - - if (isset($supersToLocales[$superLocale])) { - return $supersToLocales[$superLocale]; - } - } - - // Bileto doesn't support the requested locales, let's return the - // default one. - return self::getDefaultLocale(); - } } diff --git a/tests/Service/LocalesTest.php b/tests/Service/LocalesTest.php deleted file mode 100644 index 1afc8984..00000000 --- a/tests/Service/LocalesTest.php +++ /dev/null @@ -1,81 +0,0 @@ -get(Locales::class); - $this->locales = $locales; - } - - /** - * @param string[] $requestedLocales - */ - #[DataProvider('englishRequestedLocale')] - public function testGetBestWithEnglish(array $requestedLocales): void - { - $locale = $this->locales->getBest($requestedLocales); - - $this->assertSame('en_GB', $locale); - } - - /** - * @param string[] $requestedLocales - */ - #[DataProvider('frenchRequestedLocale')] - public function testGetBestWithFrench(array $requestedLocales): void - { - $locale = $this->locales->getBest($requestedLocales); - - $this->assertSame('fr_FR', $locale); - } - - /** - * @return string[][][] - */ - public static function englishRequestedLocale(): array - { - return [ - [[]], - [['']], - [['àà']], - [['en_GB']], - [['en_US']], - [['en_GB', 'fr_FR']], - [['fr', 'en_GB']], - [['de_DE']], - ]; - } - - /** - * @return string[][][] - */ - public static function frenchRequestedLocale(): array - { - return [ - [['fr_FR']], - [['fr_BE']], - [['fr']], - [['fr_FR', 'en_GB']], - [['en', 'fr_FR']], - ]; - } -}