Detect and resolve locale for a given set of supported (country specific) locales

Usage no npm install needed!

<script type="module">
  import resolveLocale from '';


Sponsored by Version Downloads Build Status Unix Build Status Windows Code Coverage Dependencies

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


<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.


Apache License; Version 2.0, January 2004


Logo of Sebastian Software GmbH, Mainz, Germany

Copyright 2016-2018
Sebastian Software GmbH