Super-fast and tiny locale matcher.

Usage no npm install needed!

<script type="module">
  import localeMatcher from 'https://cdn.skypack.dev/locale-matcher';


locale-matcher build

Super-fast locale matcher and normalizer in just 2 kB gzipped.

npm install --save-prod locale-matcher


import {matchLocale, pickLocale, normalizeLocale} from 'locale-matcher';

matchLocale(/*requestedLocale*/ 'en-US', /*supportedLocale*/ ['en-AU', 'en-GB', 'en', 'ru']); // → 2

matchLocale(['en-GB', 'ru-RU'], ['pt', 'en', 'ru']); // → 1

pickLocale('__en-US__', ['en-AU', 'en-GB', 'en', 'ru']); // → 'en'

pickLocale(['es-ES', 'cz-CZ'], ['en', 'ru'], /*defaultLocale*/ 'en_US'); // → 'en_US'

pickLocale('aaa-bbb-ccc', ['aaa', 'aaa-bbb', 'aaa-ddd']); // → aaa-bbb

normalizeLocale('__EN__US__'); // → '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.


Clone this repo and use npm ci && npm run perf to run the performance test suite.

locale-matcher 4,250,838
@formatjs/intl-localematcher 20,089