resolve-locale
v1.0.6
Published
Detect and resolve locale for a given set of supported (country specific) locales
Downloads
31
Maintainers
Readme
resolve-locale
resolve-locale - Resolve locale settings between browser default, supported locales and preferred locale
Getting started
Install resolve-locale via
npm install resolve-locale
Then add it to your browser source
import { detectBrowserLocale, resolveLocale } from "resolve-locale"
const localesLoader = {
"de": () => import("./locale/de"),
"pt-pt": () => import("./locale/pt-pt"),
"zh-hant-cn": () => import("./locale/zh-Hant-CN")
}
const availableLoaders = Object.keys(localesLoader)
const storedLocale = localStorage.getItem("locale")
const browserLocales = detectBrowserLocale()
const locale = resolveLocale(browserLocales, availableLoaders, storedLocale)
localesLoader[locale]().then(() => {
... main app
})
API
<array<string>> detectBrowserLocale([navigator])
Detect browsers locales as array sorted by preference. Locales are expanded.
["de-de","en"]
will expand to ["de-de", "de", "en"]
. navigator
is an
optional parameter reflecting browsers navigator object.
<string> resolveLocale(browserLocales, supportedLocales, [preferredLocale])
browserLocales
should be result of detectBrowserLocale()
. supportedLocales
is an array of
supported locales by your app. preferredLocale
is optional and a locale that is somehow
assigned to the user. This can be done by save locale to localStorage.
Returns best fit locale.
License
Apache License; Version 2.0, January 2004
Copyright
Copyright 2016-2018Sebastian Software GmbH