Closed kenjis closed 1 year ago
This is because:
CodeIgniter::initialize()
, Services::exceptions()
creates a new Request instance and set the negotiated locale:
https://github.com/codeigniter4/CodeIgniter4/blob/6425b1de048b15aeb5257397b016e5146436cde9/system/CodeIgniter.php#L187Config\App::$defaultLocale
is set:
https://github.com/codeigniter4/CodeIgniter4/blob/6425b1de048b15aeb5257397b016e5146436cde9/system/CodeIgniter.php#L194-L195This bug was fixed in 4.4
branch.
Why is this an issue?
The content negotiation example is okay.
If {locale}
is used in the route, then treating it as default is logical. Nobody said that Locale::getDefault()
has to be equal to Config\App::defaultLocale
. Current behavior gives a nice option to distinguish lang used in the route and client preference.
@michalsn Do you say the current behavior is expected?
Content negotiation: Request:ja, Locale:en
{locale}
in the route:
Request:ja, Locale:ja
@kenjis I think yes.
Is there any other way (method) to find out what {locale}
is being used? When we specify a language in a URL, it seems reasonable to treat it as a default locale.
@michalsn Okay, I also think the case of {locale}
in the route is okay.
But why is the Locale
not changed when using Content Negotiation?
Content negotiation: Request:ja, Locale:en
@kenjis The framework has no basis to set jp
as the default Locale
when we use Content Negotiation.
When the locale is in the URL, then we clearly specify the application to work in that language. So to speak, we overwrite the settings from the config file.
I don't know the difference between the locale in the URL and the locale in Accept-Language header. When we use Content Negotiation, It seems to me it clearly specify the application to work in that language.
But if the current behavior is correct, we need to fix the behavior in 4.4
branch.
(4.4 *$=)$ curl -H "Accept-Language: ja,en-US;q=0.9,en;q=0.8" http://localhost:8080/
Request:ja, Locale:ja
(4.4 *$=)$ curl -H "Accept-Language: ja,en-US;q=0.9,en;q=0.8" http://localhost:8080/ja
Request:ja, Locale:ja
@kenjis My bad. I think you're right.
Locale::getDefault()
should always be the same as $this->request->getLocale()
by default.
@michalsn No problem! If you have any concern about it, feel free to say.
I think if these locale values are different, it is very confusing.
This bug was fixed in 4.4 branch.
version: develop (4.3.x)
Locale::getDefault()
is different from$this->request->getLocale()
when we use content negotiation.When we use only content negotiation:
But If
{locale}
is used in a route, both locales are set toja
.