2.3.1 • Published 5 months ago

get-user-locale v2.3.1

Weekly downloads
181,669
License
MIT
Repository
github
Last release
5 months ago

npm downloads CI

Get-User-Locale

A function that returns user's locale as an IETF language tag, based on all available sources.

tl;dr

  • Install by executing npm install get-user-locale or yarn add get-user-locale.
  • Import by adding import getUserLocale from 'get-user-locale'.
  • Do stuff with it!
    const userLocale = getUserLocale();

User guide

getUserLocale()

A function that returns user's preferred locale as an IETF language tag, based on all available sources.

Sample usage

import getUserLocale from 'get-user-locale';

getUserLocale(); // 'de-DE'

or

import { getUserLocale } from 'get-user-locale';

getUserLocale(); // 'de-DE'
Options

getUserLocale() may be called with an optional options argument.

options object may contain the following properties:

PropertyDescriptionDefault value
fallbackLocaleA locale to use as a fallback.en-US
useFallbackLocaleWhether to use the fallback locale.true

getUserLocales()

A function that returns an array of user's preferred locales as an IETF language tags, based on all available sources.

Sample usage

import { getUserLocales } from 'get-user-locale';

getUserLocales(); // ['de-DE', 'de', 'en-US', 'en']
Options

getUserLocales() may be called with an optional options argument.

options object may contain the following properties:

PropertyDescriptionDefault value
fallbackLocaleA locale to use as a fallback.en-US
useFallbackLocaleWhether to use the fallback locale.true

Technical details

There are a few ways of determining user's locale:

  • window.navigator.languages
  • window.navigator.language

…languages is an array of strings, …language is a string. Some browsers return mixed-case IETF language tags (e.g. de-DE), while others return lowercase ones (e.g. de-de). Finally, non-browser environments will not return anything, so you need a fallback.

Get-User-Locale does the following:

  • Combines all of them into one sane set of locales - in that particular order,
  • Dedupes them,
  • Fixes invalid, lowercased IETF language tags (so that the part after - is always uppercased),
  • Adds a fallback to en-US, so if all else fails, you will get a result that won't crash your app.

License

The MIT License.

Author

@mirfanrafif/react-calendarreact-calendar-youwereact-calendar-youwee@infinitebrahmanuniverse/nolb-get-u@everything-registry/sub-chunk-1753hodlingsguidesly-react-time-pickeriui-react-calendarmaterial-table-sharedergonomic-reactgenerator-unity-packagenova-react-calendarnode-red-contrib-opcuanode-red-contrib-opcua-swmsnuxt-gettextreact-calender-for-jafeelreact-calendar-servereact-calendar-doublereact-date-pickerreact-date-picker-correct-localereact-date-picker-gleapreact-date-picker-portalreact-date-picker-servereact-calendarreact-calendar-blissreact-monthly-calendar-fork-default-iso-week-numberreact-clockreact-clock-correct-localreact-clock-correct-localereact-datetime-pickerreact-datetime-picker-correct-localereact-insti-calendarreact-intl-datetime-formatreact-intl-number-formatreseller-react-calendarreseller-react-date-picker@engin33r/react-date-picker@eo-locale/detect-user-language@encodeas/custom-time-picker@fuzio/web3auth@jdmswong/react-calendar@ihogers_nxdx/react-datetime-picker@irunner/react-date-picker@jozekcore/react-calendar@jupiterdev/react-calendar@jupiterapp/react-calendar@kavytska/react-calendarzion-calendar@navwire/react-calendar@njsn/ui@fall-out/react-calendarshort-react-calendarsomoffice-shellrnb-react-date-pickerrsvbee-react-datetime-pickerreact-time-picker-nyareact-time-picker2react-time-picker3react-time-pickersixbee-material-uireact-99h-time-pickerreact-styled-calendarreact-today-date-picker@open-election-compass/client@osdiab/react-date-picker@velo-dao/velo-web3auth@theinternetfolks/trackr@tt92618/custom-react-calendar@vmartynets/react-time-picker@qingleili/react-calendar@reverb-ui/react@reverb-ui/teste@reactguru/calendar@xola/ui-kit@wojtekmaj/react-taeyos-react-date-picker@ahaui/react3@arcataroger/react-clock-millisecondsbeatchain-artists-react@3dweb/360javascriptviewer@aladas-org/cryptocalcbraincloudcalendar-react-widgetbuddha-react-calendarbusinessnextui@beatchainmusic/core@beatchainmusic/ui@babogdanov/react-calendar@dagenius007/react-calendar@cugede/react-time@chaitanyapotti/react-intl-number-format@dropthebeatbro/react-datetime-pickercustom-react-calendardave-rennes-react-time-picker
2.3.1

5 months ago

2.3.0

9 months ago

2.2.1

1 year ago

2.2.0

1 year ago

2.1.2

1 year ago

2.1.1

1 year ago

2.1.3

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago