Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: can't change and override valid locales #7309

Merged
Merged
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
12 changes: 12 additions & 0 deletions system/HTTP/IncomingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,18 @@ public function setLocale(string $locale)
return $this;
}

/**
* Set the valid locales.
*
* @return $this
*/
public function setValidLocales(array $locales)
{
$this->validLocales = $locales;

return $this;
}

/**
* Gets the current locale, with a fallback to the default
* locale if none is set.
Expand Down
15 changes: 15 additions & 0 deletions tests/system/HTTP/IncomingRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ public function testSetBadLocale()
$this->assertSame('es', $request->getLocale());
}

public function testSetValidLocales()
{
$config = new App();
$config->supportedLocales = ['en', 'es'];
$config->defaultLocale = 'es';
$config->baseURL = 'http://example.com/';

$request = new IncomingRequest($config, new URI(), null, new UserAgent());

$request->setValidLocales(['ja']);
$request->setLocale('ja');

$this->assertSame('ja', $request->getLocale());
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/2774
*/
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Others
``product/15`` where ``15`` is an arbitrary number.
See :ref:`controller-default-method-fallback` for details.
- **Filters:** Now you can use Filter Arguments with :ref:`$filters property <filters-filters-filter-arguments>`.
- **Request:** Added ``IncomingRequest::setValidLocales()`` method to set valid locales.

Message Changes
***************
Expand Down
3 changes: 3 additions & 0 deletions user_guide_src/source/outgoing/localization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ will be used to set the locale.

Should you ever need to set the locale directly you may use ``IncomingRequest::setLocale(string $locale)``.

Since v4.4.0, ``IncomingRequest::setValidLocales()`` has been added to set
(and reset) valid locales that are set from ``Config\App::$supportedLocales`` setting.

Content Negotiation
-------------------

Expand Down