Skip to main content
Version: 1.0.9

LocaleMatcher

Distance-based matching of a user's desired locales against a sorted list of application supported locales.

Matchers are relatively expensive to setup, since it requires parsing and resolving a potentially long list of locale identifiers. Ideally an application will setup one or more matchers on startup and reuse them.

new

Syntax
new LocaleMatcher(supported, options?)

Parameters

  • supported: string | string[] | LanguageTag[] | Locale[]
    • String / array of space and or comma-separated locale identifiers, or an array of language tags or locales, sorted in order of most- to least-supported. The first identifier will be used as the default.
  • options?: LocaleMatcherOptions
    • Options to control the matcher behavior.

Example

import { LocaleMatcher } from '@phensley/cldr';

const localeMatcher = new LocaleMatcher('en, es-419, en-GB, pt-BR, es');

match

Matches on or more desired locales against the list of supported locales, returning the matched locale and the distance.

Syntax

match(desired, options?): LanguageMatch

Parameters

  • desired: string | (Locale | LanguageTag | string)[]
    • Array of space and or comma-separated locale identifiers to match, in order of most- to least-desired.
  • options: LocaleMatcherOptions
    • Options for the matcher

Example

import { LocaleMatcher } from '@phensley/cldr';

const localeMatcher = new LocaleMatcher('en, es-419, en-GB, pt-BR, es');

let { distance, locale } = localeMatcher.match('pt');
log(`${locale.id} distance ${distance}`);

({ distance, locale } = localeMatcher.match('es-MX'));
log(`${locale.id} distance ${distance}`);

({ distance, locale } = localeMatcher.match('en-ZA'));
log(`${locale.id} distance ${distance}`);

({ distance, locale } = localeMatcher.match('en-ZA, es'));
log(`${locale.id} distance ${distance}`);
pt-BR distance 0
es-419 distance 4
en-GB distance 3
es distance 0

References