CodeIgniter Forums
Recognize child locale as parent locale - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Recognize child locale as parent locale (/showthread.php?tid=75360)



Recognize child locale as parent locale - SteeveDroz - 01-29-2020

Hello everyone,

Most locales have children locales. For example, "en" has "en_UK", "en_US", "en_AU", "en_CA", and so on. (FYI, "en" has 105 children locales!)

My browser is configured to look for "fr-CH,en-US;q=0.7,en;q=0.3", which means it will look for Swiss French before anything else, then US English, then other English. This is the default settings for my browser.

My website default locale is "fr", and the supported locales are "fr" and "en", all set in Config\App.

I'd like the default language to be French, but CodeIgniter4 insists on serving me English, because "en" is in my browser "Accept-Language" list and "fr" isn't!
I know for a fact that this is the reason, for if I add "fr" to the list of "Accept-Language", before "en", the site switches to French.

How can I accept the children locales (i.e. "fr_CH") as their parents (i.e. "fr")?


RE: Recognize child locale as parent locale - InsiteFX - 01-30-2020

Read: CodeIgniter User Guide - Configuring the Locale


RE: Recognize child locale as parent locale - SteeveDroz - 01-30-2020

(01-30-2020, 05:43 AM)InsiteFX Wrote: Read: CodeIgniter User Guide - Configuring the Locale

I usually don't mind minimal answers, but this one doesn't help.

I do understand that it's better to use BCP 47 codes and it's what I do. I do believe that "The system is smart enough to fall back to more generic language codes", but the fact is that the browser sends in the request:

Code:
Accept-Language: fr-CH,en-US;q=0.7,en;q=0.3

fr-CH doesn't fall back to fr, even though it is the default language, probably because there is a secondary language that exists in its generic form: en.

My guess is that CodeIgniter chooses the language this way:

fr-CH? I can't handle it, I'll loop to it later and use fr if I don't find anything better.
en-US? I can't handle it, I'll loop to it later and use en if I don't find anything better.
en? I can handle it! Let's display this site in English! Oh boy! Oh boy!

Is there a way to display it in French anyway?

And before you ask, here is my Config\App properties:

PHP Code:
// ...
public $defaultLocale 'fr';
public 
$negotiateLocale true;
public 
$supportedLocales = ['en''fr'];
// ... 



RE: Recognize child locale as parent locale - jreklund - 01-30-2020

CodeIgniter dosen't support this. It never truncates fr-CH into fr. It will only look for absolute matches.

If you want this kind of behavior, you will need to copy the code from CodeIgniter\HTTP\Negotiate. language(), getBestMatch(), parseHeader, match(), matchParameters() and matchTypes().
And create a filter or event:
https://codeigniter4.github.io/userguide/incoming/filters.html
https://codeigniter4.github.io/userguide/extending/events.html

Or you can replace those methods (parseHeader) with "extending core classes". FYI it are used by media, charset, encoding and language. So you don't break any of those.
https://codeigniter4.github.io/userguide/extending/core_classes.html

Or fork CodeIgniter 4, tweak the source code and make a Pull Request. Making this project even better!


RE: Recognize child locale as parent locale - SteeveDroz - 01-30-2020

Thanks for your answer. It seems to be quite a lot of work for something that doesn't appear to be an edge case: browser usually aren't configures to ask for generic languages first.

I'm not sure what I'll do, probably serve the website in English and save a cookie if they update their preferences.

Thanks anyway!


RE: Recognize child locale as parent locale - jreklund - 01-31-2020

I just had a quick look on Google and pretty much everyone with a decent coding style does it like this. There are some small libs with support (separating fr-CH into fr). Those aren't using namespace/composer and are just a small function and not taking the priority into consideration. So I won't link those as you shouldn't use them.