diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 72b1ce0ca5df..fb7d47990ca9 100644 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -441,10 +441,14 @@ public function getPath(): string */ public function setLocale(string $locale) { - // If it's not a valid locale, set it - // to the default locale for the site. + // Check if valid locale. if (! in_array($locale, $this->validLocales, true)) { - $locale = $this->defaultLocale; + // If not valid, check if language only is valid locale. + $locale = strtok($locale, '-'); + if (! in_array($locale, $this->validLocales, true)) { + // If neither valid, use default locale for the site. + $locale = $this->defaultLocale; + } } $this->locale = $locale; diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 419e661c8fe1..4fb46890f5e5 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -223,6 +223,19 @@ public function testSetBadLocale(): void $this->assertSame('es', $request->getLocale()); } + public function testSetBroadLocale(): void + { + $config = new App(); + $config->supportedLocales = ['en', 'fr']; + $config->defaultLocale = 'en'; + $config->baseURL = 'http://example.com/'; + + $request = $this->createRequest($config); + + $request->setLocale('fr-FR'); + $this->assertSame('fr', $request->getLocale()); + } + public function testSetValidLocales(): void { $config = new App(); diff --git a/user_guide_src/source/changelogs/v4.6.5.rst b/user_guide_src/source/changelogs/v4.6.5.rst index 0ae6d47fe1be..de94ed43195e 100644 --- a/user_guide_src/source/changelogs/v4.6.5.rst +++ b/user_guide_src/source/changelogs/v4.6.5.rst @@ -30,6 +30,8 @@ Deprecations Bugs Fixed ********** +- **Localization:** Fixed a bug in ``IncomingRequest::setLocale()`` that prevented falling back to valid, supported generic language code as locale. + See the repo's `CHANGELOG.md `_ for a complete list of bugs fixed.