New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

locale-smart

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

locale-smart

Resolve and apply best-fit locale from browser preferences and optional GEO; supports markets, URL override, and customizable tag-to-short mapping.

latest
Source
npmnpm
Version
1.0.0-dev.0
Version published
Maintainers
1
Created
Source

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.

npm GitHub package version NPM Downloads

1.5kB gzipped


Install

yarn add locale-smart

Import

import LocaleSmart from 'locale-smart';

Usage

// Basic usage: resolve a locale and update the document
const resolver = new LocaleSmart();
resolver.init();
// Adds a class like "locale-ru" to <body> and sets <html lang="ru">

Initialization with options

// Define markets with codes, tags, and GEO mapping
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, // e.g., 'BY'
  classPrefix: 'locale-',
});

resolver.init();
// When `locales` is provided, the short code defaults to the market `code`
// e.g., with GEO=BY the <body> gets class "locale-BY"

URL override

// If the URL contains ?locale=en or ?locale=en-US or ?locale=BY,
// it takes precedence and is applied immediately
new LocaleSmart().init();
// You can also read/apply it manually with a different parameter name:
const r = new LocaleSmart();
r.applyOverrideFromUrl('lang');

Server-side / no DOM

// Determine a short locale without touching the DOM
const r = new LocaleSmart({ documentRef: null });
const short = r.determineShortLocale(); // e.g., 'ru'

Options

OptionTypeDefaultDescription
localesReadonlyArray<{ code: string; tag: string; geos?: readonly string[] }>undefinedDefines markets. Enables code↔tag mapping and GEO resolution. When set, supported locales are derived from this list.
defaultCodestringfirst of localesDefault market code when locales is provided.
supportedreadonly string[]["ru-RU"]Supported locale tags when locales is not provided.
defaultLocalestring"ru-RU"Default locale tag when locales is not provided.
getGeo() => string | undefinedundefinedReturns GEO/market code (e.g., RU, BY) used as a hint during resolution.
getRequestedLocales() => readonly string[]navigator.languagesProvides the requested locales list; falls back to navigator.language.
documentRefDocumentglobal document or nullCustom document object; set null to disable DOM updates.
geoMapReadonly<Record<string, string>>undefinedMap GEO_CODE -> localeTag. Used when getGeo() returns a code.
classPrefixstring"locale-"Prefix for the class applied to <body>, e.g., locale-ru.
shortFromTag(tag: string) => stringmarket code from locales or tag.split('-')[0]Maps a full locale tag to a short code. Overridable.

API Methods

MethodDescription
init(): stringResolves the short locale and applies it to the document. Honors URL override. Returns the short code.
determineShortLocale(): stringResolves and stores the short locale without touching the DOM.
applyToDocument(short: string): voidApplies classPrefix + short to <body> and sets <html lang>.
applyOverrideFromUrl(paramName = 'locale'): string | undefinedReads 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

Keywords

locale

FAQs

Package last updated on 22 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts