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 user's locale as an IETF language tag, based on all available sources.
tl;dr
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();
or
import { getUserLocale } from 'get-user-locale';
getUserLocale();
Options
getUserLocale()
may be called with an optional options
argument.
options
object may contain the following properties:
Property | Description | Default value |
---|
fallbackLocale | A locale to use as a fallback. | en-US |
useFallbackLocale | Whether 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();
Options
getUserLocales()
may be called with an optional options
argument.
options
object may contain the following properties:
Property | Description | Default value |
---|
fallbackLocale | A locale to use as a fallback. | en-US |
useFallbackLocale | Whether 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