75 lines
3.4 KiB
Plaintext
Executable File
75 lines
3.4 KiB
Plaintext
Executable File
import { LookupMatcher } from './LookupMatcher';
|
|
import { BestFitMatcher } from './BestFitMatcher';
|
|
import { invariant } from './utils';
|
|
import { UnicodeExtensionValue } from './UnicodeExtensionValue';
|
|
/**
|
|
* https://tc39.es/ecma402/#sec-resolvelocale
|
|
*/
|
|
export function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
|
|
var matcher = options.localeMatcher;
|
|
var r;
|
|
if (matcher === 'lookup') {
|
|
r = LookupMatcher(availableLocales, requestedLocales, getDefaultLocale);
|
|
}
|
|
else {
|
|
r = BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale);
|
|
}
|
|
var foundLocale = r.locale;
|
|
var result = { locale: '', dataLocale: foundLocale };
|
|
var supportedExtension = '-u';
|
|
for (var _i = 0, relevantExtensionKeys_1 = relevantExtensionKeys; _i < relevantExtensionKeys_1.length; _i++) {
|
|
var key = relevantExtensionKeys_1[_i];
|
|
invariant(foundLocale in localeData, "Missing locale data for ".concat(foundLocale));
|
|
var foundLocaleData = localeData[foundLocale];
|
|
invariant(typeof foundLocaleData === 'object' && foundLocaleData !== null, "locale data ".concat(key, " must be an object"));
|
|
var keyLocaleData = foundLocaleData[key];
|
|
invariant(Array.isArray(keyLocaleData), "keyLocaleData for ".concat(key, " must be an array"));
|
|
var value = keyLocaleData[0];
|
|
invariant(typeof value === 'string' || value === null, "value must be string or null but got ".concat(typeof value, " in key ").concat(key));
|
|
var supportedExtensionAddition = '';
|
|
if (r.extension) {
|
|
var requestedValue = UnicodeExtensionValue(r.extension, key);
|
|
if (requestedValue !== undefined) {
|
|
if (requestedValue !== '') {
|
|
if (~keyLocaleData.indexOf(requestedValue)) {
|
|
value = requestedValue;
|
|
supportedExtensionAddition = "-".concat(key, "-").concat(value);
|
|
}
|
|
}
|
|
else if (~requestedValue.indexOf('true')) {
|
|
value = 'true';
|
|
supportedExtensionAddition = "-".concat(key);
|
|
}
|
|
}
|
|
}
|
|
if (key in options) {
|
|
var optionsValue = options[key];
|
|
invariant(typeof optionsValue === 'string' ||
|
|
typeof optionsValue === 'undefined' ||
|
|
optionsValue === null, 'optionsValue must be String, Undefined or Null');
|
|
if (~keyLocaleData.indexOf(optionsValue)) {
|
|
if (optionsValue !== value) {
|
|
value = optionsValue;
|
|
supportedExtensionAddition = '';
|
|
}
|
|
}
|
|
}
|
|
result[key] = value;
|
|
supportedExtension += supportedExtensionAddition;
|
|
}
|
|
if (supportedExtension.length > 2) {
|
|
var privateIndex = foundLocale.indexOf('-x-');
|
|
if (privateIndex === -1) {
|
|
foundLocale = foundLocale + supportedExtension;
|
|
}
|
|
else {
|
|
var preExtension = foundLocale.slice(0, privateIndex);
|
|
var postExtension = foundLocale.slice(privateIndex, foundLocale.length);
|
|
foundLocale = preExtension + supportedExtension + postExtension;
|
|
}
|
|
foundLocale = Intl.getCanonicalLocales(foundLocale)[0];
|
|
}
|
|
result.locale = foundLocale;
|
|
return result;
|
|
}
|