resolve-locale

1.0.6 • Public • Published

resolve-locale
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
})

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

Logo of Sebastian Software GmbH, Mainz, Germany

Copyright 2016-2018
Sebastian Software GmbH

Package Sidebar

Install

npm i resolve-locale

Weekly Downloads

3

Version

1.0.6

License

Apache-2.0

Unpacked Size

31.2 kB

Total Files

7

Last publish

Collaborators

  • fastner