@formatjs/intl-pluralrules
Version:
Polyfill for Intl.PluralRules
17 lines (16 loc) • 1.04 kB
JavaScript
import { CanonicalizeLocaleList, CoerceOptionsToObject, GetOption, SetNumberFormatDigitOptions } from "@formatjs/ecma402-abstract";
import { ResolveLocale } from "@formatjs/intl-localematcher";
export function InitializePluralRules(pl, locales, options, { availableLocales, relevantExtensionKeys, localeData, getDefaultLocale, getInternalSlots }) {
const requestedLocales = CanonicalizeLocaleList(locales);
const opt = Object.create(null);
const opts = CoerceOptionsToObject(options);
const internalSlots = getInternalSlots(pl);
internalSlots.initializedPluralRules = true;
const matcher = GetOption(opts, "localeMatcher", "string", ["best fit", "lookup"], "best fit");
opt.localeMatcher = matcher;
const r = ResolveLocale(availableLocales, requestedLocales, opt, relevantExtensionKeys, localeData, getDefaultLocale);
internalSlots.locale = r.locale;
internalSlots.type = GetOption(opts, "type", "string", ["cardinal", "ordinal"], "cardinal");
SetNumberFormatDigitOptions(internalSlots, opts, 0, 3, "standard");
return pl;
}