locale-matcher 
Super-fast locale matcher and normalizer in
just 2 kB gzipped.
npm install --save-prod locale-matcher
Usage
import {matchLocale, pickLocale, normalizeLocale} from 'locale-matcher';
matchLocale( 'en-US', ['en-AU', 'en-GB', 'en', 'ru']);
matchLocale(['en-GB', 'ru-RU'], ['pt', 'en', 'ru']);
pickLocale('__en-US__', ['en-AU', 'en-GB', 'en', 'ru']);
pickLocale(['es-ES', 'cz-CZ'], ['en', 'ru'], 'en_US');
pickLocale('aaa-bbb-ccc', ['aaa', 'aaa-bbb', 'aaa-ddd']);
normalizeLocale('__EN__US__');
Lookup algorithm tries to find a locale with the maximum number of matched subtags (en
, US
, etc.) and shortest
overall length.
Matching is case-insensitive and ignores non-alpha-ASCII characters
so mn-Cyrl-MN
is not distinct from MN__cYRL--mn
or ++mN__cYrL@Mn++
.
Locales are expected start with ISO 639-1,
ISO 639-2,
ISO 639-3 or
ISO 639-5 language code. These codes are interchangeable
so chi-CN
is not distinct from zh-CN
.
locale-matcher
doesn't depend on Intl
and doesn't throw exceptions if locale is malformed or subtags aren't ordered
properly.
Performance
Clone this repo and use npm ci && npm run perf
to run the performance test suite.