Autoload locale from browser language
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
Hi. I'm looking into a solution to automatically load and set default locale based on the browsers language. I already did some searching, but could not find any existing info on the topic. Surely this is a useful feature that others have done before?
I know I can get browser language from JS `navigator.language` or `$_SERVER['HTTP_ACCEPT_LANGUAGE']` from PHP (which might be more reliable). However, the challenge is to map the browser language with dayjs supported locales naming, because they do not all match automagically. Anyone have any experience with this?
For example, my browser is norwegian and outputs `no` (norwegian), although it is named `nb` (norsk bokmål) in dayjs for the default variation. Furthermore, some languages have several variations `fr` `fr-be` `fr-ca` `fr-lu`, whereas dayjs supports only some of them (which makes sense) ... If I detect `fr-ch` (French swiss) from browser, I would first need to check if there is a unique dayjs locale, and if not, I would chop it down to `fr` and see if that exists. Seems I may need to add all locales to an array and do some mapping.
Contributor guide
Assessment
This issue has not been assessed yet.