Skip to content

Commit a172f81

Browse files
committed
docs: Update the description for Negotiation
1 parent e36b9a9 commit a172f81

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

user_guide_src/source/changelogs/v4.6.0.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ Routing
242242
Negotiator
243243
==========
244244

245-
- Added a feature flag ``Feature::$looseLocaleNegotiation`` fix simple locale comparison.
245+
- Added a feature flag ``Feature::$strictLocaleNegotiation`` to enable strict locale comparision.
246246
Previously, response with language headers ``Accept-language: en-US,en-GB;q=0.9`` returned the first allowed language ``en`` could instead of the exact language ``en-US`` or ``en-GB``.
247-
Set the value to ``false`` to be able to get ``en-*``
247+
Set the value to ``true`` to enable comparison not only by language code ('en' - ISO 639-1) but also by regional code ('en-US' - ISO 639-1 plus ISO 3166-1 alpha).
248248

249249
Testing
250250
=======

user_guide_src/source/incoming/content_negotiation.rst

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,57 @@ and German you would do something like:
102102
In this example, 'en' would be returned as the current language. If no match is found, it will return the first element
103103
in the ``$supported`` array, so that should always be the preferred language.
104104

105+
Strict Locale Negotiation
106+
-------------------------
107+
105108
.. versionadded:: 4.6.0
106109

107-
Disabling the ``Config\Feature::$looseLocaleNegotiation`` value allows you to strictly search for the requested language from the specified territory (``en-*``).
108-
In the case of a non-strict search, the language may be limited only by the country ``en``. Don't forget to create files for the ``en-*`` locale if you need a translation.
110+
By default, locale is determined on a lossy comparison basis. So only the first part of the locale string is taken
111+
into account (language). This is usually sufficient. But sometimes we want to be able to distinguish between regional versions such as
112+
``en-US`` and ``en-GB`` to serve different content.
113+
114+
For such cases, we have introduced a new setting that can be enabled via ``Config\Feature::$strictLocaleNegotiation``. This will ensure
115+
that the strict comparison will be made in the first place.
116+
117+
.. note::
118+
119+
CodeIgniter comes with translations only for primary language tags ('en', 'fr', etc.). So if you enable this feature and your
120+
settings in ``Config\App::$supportedLocales`` include regional language tags ('en-US', 'fr-FR', etc.), then keep in mind that
121+
if you have your own translation files, you **must also change** the folder names for CodeIgniter's translation files to match
122+
what you put in the ``$supportedLocales`` array.
123+
124+
Now let's consider the below example. The browser's preferred language will be set as this::
125+
126+
GET /foo HTTP/1.1
127+
Accept-Language: fr; q=1.0, en-GB; q=0.5
128+
129+
In this example, the browser would prefer French, with a second choice of English (United Kingdom). Your website on another hand will
130+
supports German and English (United States):
131+
132+
.. code-block:: php
133+
134+
$supported = [
135+
'de',
136+
'en-US',
137+
];
138+
139+
$lang = $request->negotiate('language', $supported);
140+
// or
141+
$lang = $negotiate->language($supported);
142+
143+
In this example, 'en-US' would be returned as the current language. If no match is found, it will return the first element
144+
in the ``$supported`` array. Here is how exactly the locale selection process works.
145+
146+
Even though the 'fr' is preferred by the browser it is not in our ``$supported`` array. The same problem occurs with 'en-GB', but here
147+
we will be able to search for variants. First, we will fallback to the most general locale (in this case 'en') which again is not in our
148+
array. Then we will search for the regional locale 'en-'. And that's when our value from the ``$supported`` array will be matched.
149+
We will return 'en-US'.
150+
151+
So the process of selecting a locale is as follows:
152+
153+
#. strict match ('en-GB') - ISO 639-1 plus ISO 3166-1 alpha-2
154+
#. general locale match ('en') - ISO 639-1
155+
#. regional locale match ('en-') - ISO 639-1 plus "wildcard" for ISO 3166-1 alpha-2
109156

110157
Encoding
111158
========

user_guide_src/source/installation/upgrade_460.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ Config
211211

212212
- app/Config/Feature.php
213213
- ``Config\Feature::$autoRoutesImproved`` has been changed to ``true``.
214-
- ``Config\Feature::$looseLocaleNegotiation`` has been added.
214+
- ``Config\Feature::$strictLocaleNegotiation`` has been added.
215215
- app/Config/Routing.php
216216
- ``Config\Routing::$translateUriToCamelCase`` has been changed to ``true``.
217+
217218
All Changes
218219
===========
219220

220221
This is a list of all files in the **project space** that received changes;
221222
many will be simple comments or formatting that have no effect on the runtime:
222223

223-
- app/Config/Feature.php
224-
- @TODO
224+
- app/Config/Feature.php

0 commit comments

Comments
 (0)