From f65409b33ae74bad3c06f619b7b96296335836e7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 27 Feb 2023 10:21:40 +0900 Subject: [PATCH 1/3] feat: add setValidLocales() --- system/HTTP/IncomingRequest.php | 12 ++++++++++++ tests/system/HTTP/IncomingRequestTest.php | 15 +++++++++++++++ 2 files changed, 27 insertions(+) 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 */ From 3b3a5e8f5ed61bde026267bf605df7ffc8c74f76 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 2 Mar 2023 11:43:13 +0900 Subject: [PATCH 2/3] docs: add docs --- user_guide_src/source/outgoing/localization.rst | 3 +++ 1 file changed, 3 insertions(+) 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 ------------------- From 561e2497035c8a0b93a71bb720aed0cc494b9004 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 2 Mar 2023 11:43:26 +0900 Subject: [PATCH 3/3] docs: add changelog --- user_guide_src/source/changelogs/v4.4.0.rst | 1 + 1 file changed, 1 insertion(+) 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 ***************