locale-matcher
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

locale-matcher build

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(/*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.

Performance

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

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

Package Sidebar

Install

npm i locale-matcher

Weekly Downloads

19

Version

2.0.0

License

MIT

Unpacked Size

21.1 kB

Total Files

19

Last publish

Collaborators

  • smikhalevski