iamkun / iamkun/dayjs

Autoload locale from browser language

Open
#732 3 comments 6 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.