diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index 008a865bcdcd..8012df3eb8e3 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -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. diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index f6994f722ce9..2b61850ec579 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -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 */ diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 5f902d226f85..1781eaaff0d8 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -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 `. +- **Request:** Added ``IncomingRequest::setValidLocales()`` method to set valid locales. Message Changes *************** diff --git a/user_guide_src/source/outgoing/localization.rst b/user_guide_src/source/outgoing/localization.rst index 2cff7700b091..86769f009f80 100644 --- a/user_guide_src/source/outgoing/localization.rst +++ b/user_guide_src/source/outgoing/localization.rst @@ -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 -------------------