Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.6.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
for a complete list of bugs fixed.
Loading