locale-smart
LocaleSmart selects the best locale from browser preferences and (optionally) GEO, maps it to a short code (e.g., ru or a market code), and applies it to the document. It supports market definitions, URL overrides, and a customizable mapping function from a full tag to a short code.

1.5kB gzipped
➠ Install
yarn add locale-smart
➠ Import
import LocaleSmart from 'locale-smart';
➠ Usage
const resolver = new LocaleSmart();
resolver.init();
Initialization with options
const resolver = new LocaleSmart({
locales: [
{ code: 'RU', tag: 'ru-RU', geos: ['RU'] },
{ code: 'BY', tag: 'ru-BY', geos: ['BY'] },
{ code: 'EN', tag: 'en-US', geos: ['US', 'GB'] },
],
defaultCode: 'RU',
getGeo: () => window.myGeoCode,
classPrefix: 'locale-',
});
resolver.init();
URL override
new LocaleSmart().init();
const r = new LocaleSmart();
r.applyOverrideFromUrl('lang');
Server-side / no DOM
const r = new LocaleSmart({ documentRef: null });
const short = r.determineShortLocale();
➠ Options
locales | ReadonlyArray<{ code: string; tag: string; geos?: readonly string[] }> | undefined | Defines markets. Enables code↔tag mapping and GEO resolution. When set, supported locales are derived from this list. |
defaultCode | string | first of locales | Default market code when locales is provided. |
supported | readonly string[] | ["ru-RU"] | Supported locale tags when locales is not provided. |
defaultLocale | string | "ru-RU" | Default locale tag when locales is not provided. |
getGeo | () => string | undefined | undefined | Returns GEO/market code (e.g., RU, BY) used as a hint during resolution. |
getRequestedLocales | () => readonly string[] | navigator.languages | Provides the requested locales list; falls back to navigator.language. |
documentRef | Document | global document or null | Custom document object; set null to disable DOM updates. |
geoMap | Readonly<Record<string, string>> | undefined | Map GEO_CODE -> localeTag. Used when getGeo() returns a code. |
classPrefix | string | "locale-" | Prefix for the class applied to <body>, e.g., locale-ru. |
shortFromTag | (tag: string) => string | market code from locales or tag.split('-')[0] | Maps a full locale tag to a short code. Overridable. |
➠ API Methods
init(): string | Resolves the short locale and applies it to the document. Honors URL override. Returns the short code. |
determineShortLocale(): string | Resolves and stores the short locale without touching the DOM. |
applyToDocument(short: string): void | Applies classPrefix + short to <body> and sets <html lang>. |
applyOverrideFromUrl(paramName = 'locale'): string | undefined | Reads the query parameter and, if a match is found (by full tag, market code, or short), applies the locale. |
currentLocale: string (getter) | Returns the currently resolved short locale. |
➠ Notes
- Resolution order: URL override, then GEO hint, then browser preferences; finally the default value.
- If
Intl.LocaleMatcher.bestFit is available, it is used; otherwise, a language-prefix strategy with a default fallback is applied.
➠ License
locale-smart is released under MIT license