What is get-user-locale?
The get-user-locale npm package is a simple utility that helps you determine the user's locale based on their browser settings. This can be useful for internationalization (i18n) and localization (l10n) purposes in web applications.
What are get-user-locale's main functionalities?
Get User Locale
This feature allows you to retrieve the user's locale setting from their browser. The code sample demonstrates how to import the package, get the user's locale, and log it to the console.
const getUserLocale = require('get-user-locale');
const locale = getUserLocale();
console.log(locale);
Get User Locale with Fallback
This feature allows you to specify a fallback locale in case the user's locale cannot be determined. The code sample shows how to set a fallback locale of 'en-US'.
const getUserLocale = require('get-user-locale');
const locale = getUserLocale({ fallbackLocale: 'en-US' });
console.log(locale);
Other packages similar to get-user-locale
os-locale
The os-locale package retrieves the locale of the operating system. It is more focused on the system's locale rather than the browser's locale, making it useful for Node.js applications running on a server or desktop environment.
browser-locale
The browser-locale package is similar to get-user-locale in that it retrieves the user's locale from the browser. However, it provides a more straightforward API and does not support fallback locales.
intl-locales-supported
The intl-locales-supported package checks if the current environment supports a list of locales. While it does not retrieve the user's locale, it is useful for ensuring that the necessary locale data is available for internationalization purposes.
Get-User-Locale
A function that returns a IETF language tag, based on all available sources.
tl;dr
User guide
getUserLocale()
A function that returns a IETF language tag, based on all available sources.
Sample result
'de-DE'
Usage
import getUserLocale from 'get-user-locale';
or
import { getUserLocale } from 'get-user-locale';
getUserLocales()
A function that returns an array of IETF language tags, based on all available sources.
Sample result
['de-DE', 'de', 'en-US', 'en']
Usage
import { getUserLocales } from 'get-user-locale';
Technical details
There are at least five ways of determining user's locale:
window.navigator.languages
window.navigator.language
window.navigator.userLanguage
window.navigator.browserLanguage
window.navigator.systemLanguage
...languages
is an array of strings, the others are strings. 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