@formatjs/intl-localematcher 0.10.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +22 -30
- package/index.d.ts +1 -8
- package/index.js +377 -190
- package/index.js.map +1 -1
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,14 +1,61 @@
|
|
|
1
1
|
import { memoize } from "@formatjs/fast-memoize";
|
|
2
|
-
|
|
2
|
+
// packages/intl-localematcher/abstract/CanonicalizeLocaleList.ts
|
|
3
|
+
let canonicalIntl = globalThis.Intl;
|
|
4
|
+
const getOwnDescriptor = Object.getOwnPropertyDescriptor;
|
|
5
|
+
const apply$2 = Reflect.apply;
|
|
6
|
+
function ownCanonicalizer() {
|
|
7
|
+
const descriptor = canonicalIntl && getOwnDescriptor(canonicalIntl, "getCanonicalLocales");
|
|
8
|
+
return descriptor && getOwnDescriptor(descriptor, "value")?.value;
|
|
9
|
+
}
|
|
10
|
+
let canonicalize$1 = ownCanonicalizer();
|
|
3
11
|
/**
|
|
4
12
|
* http://ecma-international.org/ecma-402/7.0/index.html#sec-canonicalizelocalelist
|
|
5
13
|
* @param locales
|
|
6
14
|
*/
|
|
7
15
|
function CanonicalizeLocaleList(locales) {
|
|
8
|
-
|
|
16
|
+
if (canonicalIntl === void 0) canonicalIntl = globalThis.Intl;
|
|
17
|
+
const installed = ownCanonicalizer();
|
|
18
|
+
if (typeof installed === "function") canonicalize$1 = installed;
|
|
19
|
+
return apply$2(canonicalize$1, canonicalIntl, [locales]);
|
|
20
|
+
}
|
|
21
|
+
// packages/intl-localematcher/abstract/intrinsics.ts
|
|
22
|
+
const apply$1 = Reflect.apply;
|
|
23
|
+
const includes = Array.prototype.includes;
|
|
24
|
+
const indexOf = Array.prototype.indexOf;
|
|
25
|
+
function arrayIncludes(values, search) {
|
|
26
|
+
return apply$1(includes, values, [search]);
|
|
27
|
+
}
|
|
28
|
+
function arrayIndexOf(values, search) {
|
|
29
|
+
return apply$1(indexOf, values, [search]);
|
|
30
|
+
}
|
|
31
|
+
const find = Array.prototype.find;
|
|
32
|
+
function arrayFind(values, predicate) {
|
|
33
|
+
return apply$1(find, values, [predicate]);
|
|
34
|
+
}
|
|
35
|
+
const defineProperty = Object.defineProperty;
|
|
36
|
+
function setDataProperty(target, key, value) {
|
|
37
|
+
defineProperty(target, key, {
|
|
38
|
+
__proto__: null,
|
|
39
|
+
value,
|
|
40
|
+
writable: true,
|
|
41
|
+
enumerable: true,
|
|
42
|
+
configurable: true
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const stringSlice = Function.prototype.call.bind(String.prototype.slice);
|
|
46
|
+
const stringIndexOf = Function.prototype.call.bind(String.prototype.indexOf);
|
|
47
|
+
const stringLastIndexOf = Function.prototype.call.bind(String.prototype.lastIndexOf);
|
|
48
|
+
const stringLowerCase = Function.prototype.call.bind(String.prototype.toLowerCase);
|
|
49
|
+
const stringCharCodeAt = Function.prototype.call.bind(String.prototype.charCodeAt);
|
|
50
|
+
const split = String.prototype.split;
|
|
51
|
+
const hyphen = {
|
|
52
|
+
__proto__: null,
|
|
53
|
+
toString: () => "-"
|
|
54
|
+
};
|
|
55
|
+
function splitLocaleSubtags(value) {
|
|
56
|
+
return apply$1(split, value, [hyphen]);
|
|
9
57
|
}
|
|
10
|
-
|
|
11
|
-
//#region node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/matching.js
|
|
58
|
+
// node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/matching.js
|
|
12
59
|
const matchingData = {
|
|
13
60
|
"language": {
|
|
14
61
|
"nb|no": 1,
|
|
@@ -111,8 +158,12 @@ const matchingData = {
|
|
|
111
158
|
"ps|en": 30,
|
|
112
159
|
"qu|es": 30,
|
|
113
160
|
"rm|de": 20,
|
|
114
|
-
"rn|
|
|
115
|
-
"rw|
|
|
161
|
+
"rn|rw": 10,
|
|
162
|
+
"rw|rn": 10,
|
|
163
|
+
"rn|fr": 30,
|
|
164
|
+
"rn|en": 35,
|
|
165
|
+
"rw|en": 30,
|
|
166
|
+
"rw|fr": 35,
|
|
116
167
|
"sa|hi": 30,
|
|
117
168
|
"scn|it": 20,
|
|
118
169
|
"sd|en": 30,
|
|
@@ -969,7 +1020,7 @@ const matchingData = {
|
|
|
969
1020
|
"pt-BR",
|
|
970
1021
|
"pt-PT"
|
|
971
1022
|
],
|
|
972
|
-
"cldrVersion": "
|
|
1023
|
+
"cldrVersion": "49",
|
|
973
1024
|
"macroRegions": ["419"],
|
|
974
1025
|
"canonicalAliases": {
|
|
975
1026
|
"language": [
|
|
@@ -1083,7 +1134,6 @@ const matchingData = {
|
|
|
1083
1134
|
"ewe",
|
|
1084
1135
|
"fao",
|
|
1085
1136
|
"fas",
|
|
1086
|
-
"fat",
|
|
1087
1137
|
"fij",
|
|
1088
1138
|
"fin",
|
|
1089
1139
|
"fra",
|
|
@@ -1223,6 +1273,7 @@ const matchingData = {
|
|
|
1223
1273
|
"mof",
|
|
1224
1274
|
"mol",
|
|
1225
1275
|
"mon",
|
|
1276
|
+
"mrd",
|
|
1226
1277
|
"mri",
|
|
1227
1278
|
"msa",
|
|
1228
1279
|
"mst",
|
|
@@ -1304,6 +1355,7 @@ const matchingData = {
|
|
|
1304
1355
|
"sgl",
|
|
1305
1356
|
"sgn",
|
|
1306
1357
|
"sh",
|
|
1358
|
+
"shl",
|
|
1307
1359
|
"sin",
|
|
1308
1360
|
"skk",
|
|
1309
1361
|
"slk",
|
|
@@ -1398,6 +1450,7 @@ const matchingData = {
|
|
|
1398
1450
|
"yiy",
|
|
1399
1451
|
"yma",
|
|
1400
1452
|
"ymt",
|
|
1453
|
+
"yol",
|
|
1401
1454
|
"yor",
|
|
1402
1455
|
"yos",
|
|
1403
1456
|
"yuu",
|
|
@@ -1752,21 +1805,39 @@ const matchingData = {
|
|
|
1752
1805
|
"minLsrDistance": 1,
|
|
1753
1806
|
"demotion": 5
|
|
1754
1807
|
};
|
|
1755
|
-
|
|
1756
|
-
|
|
1808
|
+
// packages/intl-localematcher/abstract/utils.ts
|
|
1809
|
+
const apply = Reflect.apply;
|
|
1810
|
+
const some = Array.prototype.some;
|
|
1811
|
+
const sortLocaleIndexes = Array.prototype.sort;
|
|
1812
|
+
const freezeLocaleList = Object.freeze;
|
|
1813
|
+
const LocaleError = Error;
|
|
1814
|
+
const LocaleMap = Map;
|
|
1815
|
+
const LocaleSet$1 = Set;
|
|
1816
|
+
const addLocaleSet = Function.prototype.call.bind(LocaleSet$1.prototype.add);
|
|
1817
|
+
const hasLocaleSet = Function.prototype.call.bind(LocaleSet$1.prototype.has);
|
|
1818
|
+
const minDistance = Math.min;
|
|
1819
|
+
const maxDistance = Math.max;
|
|
1757
1820
|
function splitUnicodeExtension(locale) {
|
|
1758
|
-
const subtags = locale
|
|
1821
|
+
const subtags = splitLocaleSubtags(locale);
|
|
1759
1822
|
for (let i = 1; i < subtags.length; i++) {
|
|
1760
1823
|
if (subtags[i] === "x") break;
|
|
1761
1824
|
if (subtags[i] !== "u") continue;
|
|
1762
1825
|
let end = i + 1;
|
|
1763
1826
|
while (end < subtags.length && subtags[end].length > 1) end++;
|
|
1827
|
+
let base = subtags[0];
|
|
1828
|
+
for (let j = 1; j < i; j++) base += "-" + subtags[j];
|
|
1829
|
+
for (let j = end; j < subtags.length; j++) base += "-" + subtags[j];
|
|
1830
|
+
let extension = "";
|
|
1831
|
+
for (let j = i; j < end; j++) extension += "-" + subtags[j];
|
|
1764
1832
|
return {
|
|
1765
|
-
locale:
|
|
1766
|
-
extension
|
|
1833
|
+
locale: base,
|
|
1834
|
+
extension
|
|
1767
1835
|
};
|
|
1768
1836
|
}
|
|
1769
|
-
return {
|
|
1837
|
+
return {
|
|
1838
|
+
locale,
|
|
1839
|
+
extension: void 0
|
|
1840
|
+
};
|
|
1770
1841
|
}
|
|
1771
1842
|
/**
|
|
1772
1843
|
* Asserts that a condition is true, throwing an error if it is not.
|
|
@@ -1783,18 +1854,18 @@ function splitUnicodeExtension(locale) {
|
|
|
1783
1854
|
* // locale is now narrowed to non-undefined type
|
|
1784
1855
|
* ```
|
|
1785
1856
|
*/
|
|
1786
|
-
function invariant(condition, message, Err =
|
|
1857
|
+
function invariant(condition, message, Err = LocaleError) {
|
|
1787
1858
|
if (!condition) throw new Err(message);
|
|
1788
1859
|
}
|
|
1789
1860
|
const DEFAULT_MATCHING_THRESHOLD = matchingData.defaults[1];
|
|
1790
1861
|
const DISTANCE_SCALE = 8;
|
|
1791
1862
|
const MIN_LSR_DISTANCE = matchingData.minLsrDistance;
|
|
1792
|
-
const languageAliases = new
|
|
1793
|
-
const scriptAliases = new
|
|
1794
|
-
const regionAliases = new
|
|
1863
|
+
const languageAliases = new LocaleSet$1(matchingData.canonicalAliases.language);
|
|
1864
|
+
const scriptAliases = new LocaleSet$1(matchingData.canonicalAliases.script);
|
|
1865
|
+
const regionAliases = new LocaleSet$1(matchingData.canonicalAliases.region);
|
|
1795
1866
|
function ascii(value, start, end, min, max) {
|
|
1796
1867
|
for (let i = start; i < end; i++) {
|
|
1797
|
-
const code = value
|
|
1868
|
+
const code = stringCharCodeAt(value, i);
|
|
1798
1869
|
if (code < min || code > max) return false;
|
|
1799
1870
|
}
|
|
1800
1871
|
return true;
|
|
@@ -1812,21 +1883,21 @@ function pseudoRegion(value) {
|
|
|
1812
1883
|
return value === "XA" || value === "XB" || value === "XC";
|
|
1813
1884
|
}
|
|
1814
1885
|
function prefix(tag) {
|
|
1815
|
-
let end = tag
|
|
1886
|
+
let end = stringIndexOf(tag, "-");
|
|
1816
1887
|
if (end < 0) end = tag.length;
|
|
1817
|
-
const language = tag
|
|
1888
|
+
const language = stringSlice(tag, 0, end);
|
|
1818
1889
|
let script = "";
|
|
1819
1890
|
let region = "";
|
|
1820
1891
|
if (end < tag.length) {
|
|
1821
|
-
let next = tag
|
|
1892
|
+
let next = stringIndexOf(tag, "-", end + 1);
|
|
1822
1893
|
if (next < 0) next = tag.length;
|
|
1823
|
-
let part = tag
|
|
1894
|
+
let part = stringSlice(tag, end + 1, next);
|
|
1824
1895
|
if (scriptSubtag(part)) {
|
|
1825
1896
|
script = part;
|
|
1826
1897
|
end = next;
|
|
1827
|
-
next = tag
|
|
1898
|
+
next = stringIndexOf(tag, "-", end + 1);
|
|
1828
1899
|
if (next < 0) next = tag.length;
|
|
1829
|
-
part = tag
|
|
1900
|
+
part = stringSlice(tag, end + 1, next);
|
|
1830
1901
|
}
|
|
1831
1902
|
if (regionSubtag(part)) {
|
|
1832
1903
|
region = part;
|
|
@@ -1844,9 +1915,16 @@ function fields(tag) {
|
|
|
1844
1915
|
const parsed = prefix(tag);
|
|
1845
1916
|
const language = parsed.language;
|
|
1846
1917
|
let { script, region } = parsed;
|
|
1847
|
-
const parts = parsed.end < tag.length ? tag
|
|
1848
|
-
|
|
1849
|
-
|
|
1918
|
+
const parts = parsed.end < tag.length ? splitLocaleSubtags(stringSlice(tag, parsed.end + 1)) : [];
|
|
1919
|
+
let pseudo;
|
|
1920
|
+
for (let index = 0; index < parts.length; index++) {
|
|
1921
|
+
const part = parts[index];
|
|
1922
|
+
if (part.length === 1) break;
|
|
1923
|
+
if (part === "psaccent" || part === "psbidi" || part === "pscrack") {
|
|
1924
|
+
pseudo = part;
|
|
1925
|
+
break;
|
|
1926
|
+
}
|
|
1927
|
+
}
|
|
1850
1928
|
if (!pseudo && !pseudoRegion(region)) {
|
|
1851
1929
|
if (script === "Zzzz") script = "";
|
|
1852
1930
|
if (region === "ZZ") region = "";
|
|
@@ -1858,18 +1936,72 @@ function fields(tag) {
|
|
|
1858
1936
|
region,
|
|
1859
1937
|
flags,
|
|
1860
1938
|
pseudo,
|
|
1861
|
-
key: tag === "und" || tag
|
|
1939
|
+
key: tag === "und" || stringIndexOf(tag, "und-x-") === 0 ? tag : `${language}|${script}|${region}|${flags}|${pseudo || ""}`
|
|
1862
1940
|
};
|
|
1863
1941
|
}
|
|
1864
1942
|
const stringKey = (value) => value;
|
|
1865
1943
|
const canonicalize = memoize((tag) => {
|
|
1866
1944
|
const basic = prefix(tag);
|
|
1867
|
-
if (basic.end === tag.length && languageSubtag(basic.language) && !languageAliases
|
|
1868
|
-
return
|
|
1945
|
+
if (basic.end === tag.length && languageSubtag(basic.language) && !hasLocaleSet(languageAliases, basic.language) && !hasLocaleSet(scriptAliases, basic.script) && !hasLocaleSet(regionAliases, basic.region)) return tag;
|
|
1946
|
+
return CanonicalizeLocaleList(tag)[0];
|
|
1869
1947
|
}, { serializer: stringKey });
|
|
1948
|
+
const localeDescriptor = Object.getOwnPropertyDescriptor;
|
|
1949
|
+
const localePrototype = Object.getPrototypeOf;
|
|
1950
|
+
const LocaleTypeError = TypeError;
|
|
1951
|
+
const localeOperations = /* @__PURE__ */ new WeakMap();
|
|
1952
|
+
const getLocaleOperations = localeOperations.get.bind(localeOperations);
|
|
1953
|
+
const setLocaleOperations = localeOperations.set.bind(localeOperations);
|
|
1954
|
+
let localeIntl = globalThis.Intl;
|
|
1955
|
+
let localeConstructor;
|
|
1956
|
+
function localeDescriptorMember(object, key, member = "value") {
|
|
1957
|
+
const descriptor = localeDescriptor(object, key);
|
|
1958
|
+
return descriptor && localeDescriptor(descriptor, member)?.value;
|
|
1959
|
+
}
|
|
1960
|
+
function localeFieldReader(prototype, key) {
|
|
1961
|
+
const reader = localeDescriptorMember(prototype, key, "get");
|
|
1962
|
+
return typeof reader === "function" ? reader : void 0;
|
|
1963
|
+
}
|
|
1964
|
+
function captureLocaleOperations(prototype) {
|
|
1965
|
+
if (prototype === null) return void 0;
|
|
1966
|
+
const cached = getLocaleOperations(prototype);
|
|
1967
|
+
if (cached) return cached;
|
|
1968
|
+
const inherited = captureLocaleOperations(localePrototype(prototype));
|
|
1969
|
+
const ownMaximize = localeDescriptorMember(prototype, "maximize");
|
|
1970
|
+
const operations = {
|
|
1971
|
+
maximize: typeof ownMaximize === "function" ? ownMaximize : inherited?.maximize,
|
|
1972
|
+
language: localeFieldReader(prototype, "language") ?? inherited?.language,
|
|
1973
|
+
script: localeFieldReader(prototype, "script") ?? inherited?.script,
|
|
1974
|
+
region: localeFieldReader(prototype, "region") ?? inherited?.region
|
|
1975
|
+
};
|
|
1976
|
+
setLocaleOperations(prototype, operations);
|
|
1977
|
+
return operations;
|
|
1978
|
+
}
|
|
1979
|
+
function getLocaleConstructor() {
|
|
1980
|
+
if (localeIntl === void 0) localeIntl = globalThis.Intl;
|
|
1981
|
+
const installed = localeIntl && localeDescriptorMember(localeIntl, "Locale");
|
|
1982
|
+
if (typeof installed === "function" && installed !== localeConstructor) {
|
|
1983
|
+
const prototype = localeDescriptorMember(installed, "prototype");
|
|
1984
|
+
if (typeof prototype === "object" && prototype !== null || typeof prototype === "function") captureLocaleOperations(prototype);
|
|
1985
|
+
localeConstructor = installed;
|
|
1986
|
+
}
|
|
1987
|
+
return localeConstructor;
|
|
1988
|
+
}
|
|
1989
|
+
getLocaleConstructor();
|
|
1990
|
+
function maximizeLocale(tag) {
|
|
1991
|
+
const Constructor = getLocaleConstructor();
|
|
1992
|
+
if (!Constructor) throw new LocaleTypeError("Intl.Locale provider is unavailable");
|
|
1993
|
+
const locale = new Constructor(tag);
|
|
1994
|
+
const maximize = captureLocaleOperations(localePrototype(locale))?.maximize;
|
|
1995
|
+
if (!maximize) throw new LocaleTypeError("Intl.Locale maximize is unavailable");
|
|
1996
|
+
return apply(maximize, locale, []);
|
|
1997
|
+
}
|
|
1998
|
+
function readLocaleField(locale, key) {
|
|
1999
|
+
const reader = captureLocaleOperations(localePrototype(locale))?.[key];
|
|
2000
|
+
return reader && apply(reader, locale, []);
|
|
2001
|
+
}
|
|
1870
2002
|
const maximal = memoize((tag) => {
|
|
1871
2003
|
const input = fields(tag);
|
|
1872
|
-
if (tag === "und" || tag
|
|
2004
|
+
if (tag === "und" || stringIndexOf(tag, "und-x-") === 0) return {
|
|
1873
2005
|
...input,
|
|
1874
2006
|
language: tag,
|
|
1875
2007
|
script: "",
|
|
@@ -1892,24 +2024,20 @@ const maximal = memoize((tag) => {
|
|
|
1892
2024
|
key: `${language}|${script}|${region}|${flags}`
|
|
1893
2025
|
};
|
|
1894
2026
|
}
|
|
1895
|
-
const locale =
|
|
2027
|
+
const locale = maximizeLocale(tag);
|
|
1896
2028
|
let flags = input.flags;
|
|
1897
|
-
if (matchingData.macroRegions
|
|
1898
|
-
const regionLocale =
|
|
1899
|
-
if (input.language === "und" && (!input.script || input.script === regionLocale
|
|
2029
|
+
if (arrayIncludes(matchingData.macroRegions, input.region)) {
|
|
2030
|
+
const regionLocale = maximizeLocale(`und-${input.region}`);
|
|
2031
|
+
if (input.language === "und" && (!input.script || input.script === readLocaleField(regionLocale, "script")) || !input.script && !readLocaleField(maximizeLocale(input.language), "script")) flags &= -2;
|
|
1900
2032
|
}
|
|
1901
|
-
let language = locale
|
|
1902
|
-
let script = locale
|
|
1903
|
-
let region = locale
|
|
2033
|
+
let language = readLocaleField(locale, "language");
|
|
2034
|
+
let script = readLocaleField(locale, "script") || "";
|
|
2035
|
+
let region = readLocaleField(locale, "region") || "";
|
|
1904
2036
|
if (language === "und" || !script || !region) {
|
|
1905
|
-
const fallback =
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
].filter(Boolean).join("-")).maximize();
|
|
1910
|
-
if (language === "und") language = fallback.language === "und" ? "en" : fallback.language;
|
|
1911
|
-
script ||= fallback.script || "Latn";
|
|
1912
|
-
region ||= fallback.region || "US";
|
|
2037
|
+
const fallback = maximizeLocale("und" + (script ? "-" + script : "") + (region ? "-" + region : ""));
|
|
2038
|
+
if (language === "und") language = readLocaleField(fallback, "language") === "und" ? "en" : readLocaleField(fallback, "language");
|
|
2039
|
+
script ||= readLocaleField(fallback, "script") || "Latn";
|
|
2040
|
+
region ||= readLocaleField(fallback, "region") || "US";
|
|
1913
2041
|
}
|
|
1914
2042
|
return {
|
|
1915
2043
|
language,
|
|
@@ -1921,33 +2049,38 @@ const maximal = memoize((tag) => {
|
|
|
1921
2049
|
}, { serializer: stringKey });
|
|
1922
2050
|
function rawKey(tag) {
|
|
1923
2051
|
if (tag === "und") return "@und";
|
|
1924
|
-
if (tag
|
|
2052
|
+
if (stringIndexOf(tag, "und-x-") === 0) return tag;
|
|
1925
2053
|
const parsed = prefix(tag);
|
|
1926
|
-
const base = tag
|
|
2054
|
+
const base = stringSlice(tag, 0, parsed.end);
|
|
1927
2055
|
if (pseudoRegion(parsed.region)) return base;
|
|
1928
|
-
if (tag
|
|
2056
|
+
if (stringIndexOf(tag, "-ps") !== -1) {
|
|
1929
2057
|
const input = fields(tag);
|
|
1930
2058
|
if (input.pseudo) return input.key;
|
|
1931
2059
|
}
|
|
1932
2060
|
if (parsed.script !== "Zzzz" && parsed.region !== "ZZ") return base;
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
2061
|
+
const script = parsed.script === "Zzzz" ? "" : parsed.script;
|
|
2062
|
+
const region = parsed.region === "ZZ" ? "" : parsed.region;
|
|
2063
|
+
return parsed.language + (script ? "-" + script : "") + (region ? "-" + region : "");
|
|
2064
|
+
}
|
|
2065
|
+
const hasOwnMatchingKey = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
|
|
2066
|
+
function matchingValue(table, key) {
|
|
2067
|
+
return hasOwnMatchingKey(table, key) ? table[key] : void 0;
|
|
1938
2068
|
}
|
|
1939
2069
|
function distance(desired, supported) {
|
|
1940
2070
|
const languages = `${desired.language}|${supported.language}`;
|
|
1941
|
-
let result = desired.language === supported.language ? 0 : matchingData.language
|
|
1942
|
-
if (desired.script !== supported.script) result += matchingData.script
|
|
2071
|
+
let result = desired.language === supported.language ? 0 : matchingValue(matchingData.language, languages) ?? matchingData.defaults[0];
|
|
2072
|
+
if (desired.script !== supported.script) result += matchingValue(matchingData.script, `${languages}|${desired.script}|${supported.script}`) ?? matchingData.defaults[1];
|
|
1943
2073
|
if (desired.region !== supported.region) {
|
|
1944
|
-
const matrix = matchingData.region
|
|
1945
|
-
const desiredPartitions = matchingData.regions
|
|
1946
|
-
const supportedPartitions = matchingData.regions
|
|
2074
|
+
const matrix = matchingValue(matchingData.region, `${languages}|${desired.script}|${supported.script}`) ?? matchingValue(matchingData.region, `${languages}|*|*`);
|
|
2075
|
+
const desiredPartitions = matchingValue(matchingData.regions, desired.region);
|
|
2076
|
+
const supportedPartitions = matchingValue(matchingData.regions, supported.region);
|
|
1947
2077
|
let regionDistance = matchingData.defaults[2];
|
|
1948
2078
|
if (matrix && desiredPartitions && supportedPartitions) {
|
|
1949
2079
|
regionDistance = 0;
|
|
1950
|
-
for (
|
|
2080
|
+
for (let i = 0; i < desiredPartitions.length; i++) {
|
|
2081
|
+
const row = desiredPartitions[i] * matchingData.partitionCount;
|
|
2082
|
+
for (let j = 0; j < supportedPartitions.length; j++) regionDistance = maxDistance(regionDistance, matrix[row + supportedPartitions[j]]);
|
|
2083
|
+
}
|
|
1951
2084
|
}
|
|
1952
2085
|
result += regionDistance;
|
|
1953
2086
|
}
|
|
@@ -1956,59 +2089,83 @@ function distance(desired, supported) {
|
|
|
1956
2089
|
/** ICU language/script/region distance, including fractional explicit-subtag ties. */
|
|
1957
2090
|
const findMatchingDistance = memoize((desired, supported) => distance(maximal(canonicalize(desired)), maximal(canonicalize(supported))), { serializer: (args) => `${args[0]}|${args[1]}` });
|
|
1958
2091
|
const supportedCache = /* @__PURE__ */ new WeakMap();
|
|
2092
|
+
const getSupported = supportedCache.get.bind(supportedCache);
|
|
2093
|
+
const setSupported = supportedCache.set.bind(supportedCache);
|
|
2094
|
+
const immutableLocaleLists = /* @__PURE__ */ new WeakSet();
|
|
2095
|
+
const markLocaleListImmutable = immutableLocaleLists.add.bind(immutableLocaleLists);
|
|
2096
|
+
const isLocaleListImmutable = immutableLocaleLists.has.bind(immutableLocaleLists);
|
|
1959
2097
|
let paradigms;
|
|
2098
|
+
function createLocaleListSnapshot(locales) {
|
|
2099
|
+
const snapshot = [];
|
|
2100
|
+
for (let index = 0; index < locales.length; index++) setDataProperty(snapshot, `${index}`, locales[index]);
|
|
2101
|
+
markLocaleListImmutable(freezeLocaleList(snapshot));
|
|
2102
|
+
return snapshot;
|
|
2103
|
+
}
|
|
1960
2104
|
function equivalentKey(lsr) {
|
|
1961
2105
|
return `${lsr.language}|${lsr.script}|${lsr.region}`;
|
|
1962
2106
|
}
|
|
1963
2107
|
function prepare(locales) {
|
|
1964
|
-
|
|
1965
|
-
|
|
2108
|
+
const immutable = isLocaleListImmutable(locales);
|
|
2109
|
+
let result = getSupported(locales);
|
|
2110
|
+
if (result && !immutable && (result.original.length !== locales.length || apply(some, result.original, [(tag, index) => tag !== locales[index]]))) result = void 0;
|
|
1966
2111
|
if (!result) {
|
|
1967
2112
|
result = {
|
|
1968
|
-
|
|
2113
|
+
__proto__: null,
|
|
2114
|
+
original: immutable ? locales : Array.from(locales),
|
|
1969
2115
|
tags: [],
|
|
1970
|
-
raw:
|
|
1971
|
-
orders:
|
|
1972
|
-
best:
|
|
2116
|
+
raw: new LocaleMap(),
|
|
2117
|
+
orders: new LocaleMap(),
|
|
2118
|
+
best: new LocaleMap()
|
|
1973
2119
|
};
|
|
1974
|
-
|
|
2120
|
+
setSupported(locales, result);
|
|
1975
2121
|
}
|
|
1976
2122
|
return result;
|
|
1977
2123
|
}
|
|
2124
|
+
const getLocaleMap = Function.prototype.call.bind(LocaleMap.prototype.get);
|
|
2125
|
+
const hasLocaleMap = Function.prototype.call.bind(LocaleMap.prototype.has);
|
|
2126
|
+
const setLocaleMap = Function.prototype.call.bind(LocaleMap.prototype.set);
|
|
2127
|
+
const localeMapSize = Object.getOwnPropertyDescriptor(LocaleMap.prototype, "size").get;
|
|
2128
|
+
const clearLocaleMap = LocaleMap.prototype.clear;
|
|
1978
2129
|
function rawMatch(data, key) {
|
|
1979
|
-
const cached = data.raw
|
|
2130
|
+
const cached = getLocaleMap(data.raw, key);
|
|
1980
2131
|
if (cached !== void 0) return cached;
|
|
1981
2132
|
for (let index = data.tags.length; index < data.original.length; index++) {
|
|
1982
2133
|
const tag = canonicalize(data.original[index]);
|
|
1983
|
-
|
|
1984
|
-
value: tag,
|
|
1985
|
-
writable: true,
|
|
1986
|
-
enumerable: true,
|
|
1987
|
-
configurable: true
|
|
1988
|
-
});
|
|
2134
|
+
setDataProperty(data.tags, `${index}`, tag);
|
|
1989
2135
|
const candidate = rawKey(tag);
|
|
1990
|
-
if (!data.raw
|
|
1991
|
-
if (candidate === key) return data.raw
|
|
2136
|
+
if (!hasLocaleMap(data.raw, candidate)) setLocaleMap(data.raw, candidate, index);
|
|
2137
|
+
if (candidate === key) return getLocaleMap(data.raw, candidate);
|
|
1992
2138
|
}
|
|
1993
2139
|
}
|
|
1994
2140
|
function order(data, defaultLocale) {
|
|
1995
|
-
let result = data.orders
|
|
2141
|
+
let result = getLocaleMap(data.orders, defaultLocale);
|
|
1996
2142
|
if (!result) {
|
|
1997
2143
|
if (!data.maximal) {
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
data.maximal
|
|
2001
|
-
|
|
2002
|
-
|
|
2144
|
+
const profiles = [];
|
|
2145
|
+
for (let index = 0; index < data.tags.length; index++) setDataProperty(profiles, `${index}`, maximal(data.tags[index]));
|
|
2146
|
+
data.maximal = profiles;
|
|
2147
|
+
const exact = new LocaleMap();
|
|
2148
|
+
data.exact = exact;
|
|
2149
|
+
for (let index = 0; index < profiles.length; index++) {
|
|
2150
|
+
const { key } = profiles[index];
|
|
2151
|
+
if (!hasLocaleMap(exact, key)) setLocaleMap(exact, key, index);
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
if (!paradigms) {
|
|
2155
|
+
const keys = new LocaleSet$1();
|
|
2156
|
+
const tags = matchingData.paradigms;
|
|
2157
|
+
for (let index = 0; index < tags.length; index++) addLocaleSet(keys, equivalentKey(maximal(canonicalize(tags[index]))));
|
|
2158
|
+
paradigms = keys;
|
|
2003
2159
|
}
|
|
2004
|
-
paradigms ??= new Set(matchingData.paradigms.map((tag) => equivalentKey(maximal(canonicalize(tag)))));
|
|
2005
2160
|
const defaultKey = defaultLocale ? equivalentKey(maximal(canonicalize(defaultLocale))) : "";
|
|
2006
2161
|
const rank = (index) => {
|
|
2007
2162
|
const key = equivalentKey(data.maximal[index]);
|
|
2008
|
-
return key === defaultKey ? 0 : paradigms
|
|
2163
|
+
return key === defaultKey ? 0 : hasLocaleSet(paradigms, key) ? 1 : 2;
|
|
2009
2164
|
};
|
|
2010
|
-
result =
|
|
2011
|
-
data.
|
|
2165
|
+
result = [];
|
|
2166
|
+
for (let index = 0; index < data.tags.length; index++) setDataProperty(result, `${index}`, index);
|
|
2167
|
+
apply(sortLocaleIndexes, result, [(a, b) => rank(a) - rank(b)]);
|
|
2168
|
+
setLocaleMap(data.orders, defaultLocale, result);
|
|
2012
2169
|
}
|
|
2013
2170
|
return result;
|
|
2014
2171
|
}
|
|
@@ -2021,17 +2178,15 @@ function moreLikely(candidate, previous) {
|
|
|
2021
2178
|
function equivalentMatch(desired, data) {
|
|
2022
2179
|
let best;
|
|
2023
2180
|
for (let flags = 0; flags < 8; flags++) {
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
].filter(Boolean).join("-");
|
|
2029
|
-
const index = data.raw.get(key);
|
|
2181
|
+
let key = flags & 4 ? desired.language : "und";
|
|
2182
|
+
if (flags & 2 && desired.script) key += "-" + desired.script;
|
|
2183
|
+
if (flags & 1 && desired.region) key += "-" + desired.region;
|
|
2184
|
+
const index = getLocaleMap(data.raw, key);
|
|
2030
2185
|
if (index === void 0) continue;
|
|
2031
2186
|
const supported = maximal(data.tags[index]);
|
|
2032
2187
|
if (equivalentKey(desired) !== equivalentKey(supported)) continue;
|
|
2033
2188
|
const d = distance(desired, supported);
|
|
2034
|
-
if (d <
|
|
2189
|
+
if (d < minDistance(1, MIN_LSR_DISTANCE) && (!best || d < best.distance || d === best.distance && index < best.index)) best = {
|
|
2035
2190
|
index,
|
|
2036
2191
|
distance: d
|
|
2037
2192
|
};
|
|
@@ -2040,7 +2195,10 @@ function equivalentMatch(desired, data) {
|
|
|
2040
2195
|
}
|
|
2041
2196
|
/** Cached exact LSR lookup, then ICU distance matching. Threshold is exclusive. */
|
|
2042
2197
|
function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_MATCHING_THRESHOLD, defaultLocale = supportedLocales[0] || "", collectDistances = true) {
|
|
2043
|
-
const result = {
|
|
2198
|
+
const result = {
|
|
2199
|
+
__proto__: null,
|
|
2200
|
+
distances: {}
|
|
2201
|
+
};
|
|
2044
2202
|
if (!supportedLocales.length || !requestedLocales.length || threshold <= 0) return result;
|
|
2045
2203
|
const data = prepare(supportedLocales);
|
|
2046
2204
|
const demotion = matchingData.demotion;
|
|
@@ -2058,9 +2216,9 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2058
2216
|
const desired = maximal(canonical);
|
|
2059
2217
|
const cacheKey = `${canonical}|${defaultLocale}`;
|
|
2060
2218
|
if (!collectDistances) {
|
|
2061
|
-
const equivalent = data.best
|
|
2062
|
-
if (equivalent && equivalent.distance <
|
|
2063
|
-
data.best
|
|
2219
|
+
const equivalent = getLocaleMap(data.best, cacheKey) || equivalentMatch(desired, data);
|
|
2220
|
+
if (equivalent && equivalent.distance < minDistance(1, MIN_LSR_DISTANCE)) {
|
|
2221
|
+
setLocaleMap(data.best, cacheKey, equivalent);
|
|
2064
2222
|
if (equivalent.distance < bestDistance) {
|
|
2065
2223
|
bestDistance = equivalent.distance;
|
|
2066
2224
|
result.matchedDesiredLocale = desiredTag;
|
|
@@ -2071,21 +2229,22 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2071
2229
|
}
|
|
2072
2230
|
}
|
|
2073
2231
|
const indexes = order(data, defaultLocale);
|
|
2074
|
-
const exact = data.exact
|
|
2232
|
+
const exact = getLocaleMap(data.exact, desired.key);
|
|
2075
2233
|
if (exact !== void 0) {
|
|
2076
2234
|
result.matchedDesiredLocale = desiredTag;
|
|
2077
2235
|
result.matchedSupportedLocale = supportedLocales[exact];
|
|
2078
2236
|
if (collectDistances) result.distances[desiredTag] = { [supportedLocales[exact]]: i * demotion };
|
|
2079
2237
|
return result;
|
|
2080
2238
|
}
|
|
2081
|
-
let best = collectDistances ? void 0 : data.best
|
|
2239
|
+
let best = collectDistances ? void 0 : getLocaleMap(data.best, cacheKey);
|
|
2082
2240
|
if (!best) {
|
|
2083
2241
|
best = {
|
|
2084
2242
|
index: -1,
|
|
2085
|
-
distance:
|
|
2243
|
+
distance: 1e400
|
|
2086
2244
|
};
|
|
2087
2245
|
const distances = {};
|
|
2088
|
-
for (
|
|
2246
|
+
for (let candidate = 0; candidate < indexes.length; candidate++) {
|
|
2247
|
+
const index = indexes[candidate];
|
|
2089
2248
|
const d = collectDistances ? findMatchingDistance(desiredTag, supportedLocales[index]) : distance(desired, data.maximal[index]);
|
|
2090
2249
|
if (collectDistances) distances[supportedLocales[index]] = d + i * demotion;
|
|
2091
2250
|
if (d < best.distance || d === best.distance && d >= 1 && best.index >= 0 && moreLikely(data.maximal[index], data.maximal[best.index])) best = {
|
|
@@ -2095,8 +2254,8 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2095
2254
|
}
|
|
2096
2255
|
if (collectDistances) result.distances[desiredTag] = distances;
|
|
2097
2256
|
else {
|
|
2098
|
-
if (data.best
|
|
2099
|
-
data.best
|
|
2257
|
+
if (apply(localeMapSize, data.best, []) >= 256) apply(clearLocaleMap, data.best, []);
|
|
2258
|
+
setLocaleMap(data.best, cacheKey, best);
|
|
2100
2259
|
}
|
|
2101
2260
|
}
|
|
2102
2261
|
if (best.distance < bestDistance) {
|
|
@@ -2108,10 +2267,9 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2108
2267
|
}
|
|
2109
2268
|
return result;
|
|
2110
2269
|
}
|
|
2111
|
-
|
|
2112
|
-
//#region packages/intl-localematcher/abstract/BestFitMatcher.ts
|
|
2270
|
+
// packages/intl-localematcher/abstract/BestFitMatcher.ts
|
|
2113
2271
|
/**
|
|
2114
|
-
* https://tc39.es/ecma402/#sec-
|
|
2272
|
+
* https://tc39.es/ecma402/#sec-lookupmatchinglocalebybestfit
|
|
2115
2273
|
* @param availableLocales
|
|
2116
2274
|
* @param requestedLocales
|
|
2117
2275
|
* @param getDefaultLocale
|
|
@@ -2119,26 +2277,30 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2119
2277
|
function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
|
|
2120
2278
|
let foundLocale;
|
|
2121
2279
|
let extension;
|
|
2122
|
-
const noExtensionLocaleMap =
|
|
2123
|
-
const noExtensionLocales =
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2280
|
+
const noExtensionLocaleMap = { __proto__: null };
|
|
2281
|
+
const noExtensionLocales = [];
|
|
2282
|
+
for (let index = 0; index < requestedLocales.length; index++) {
|
|
2283
|
+
const locale = requestedLocales[index];
|
|
2284
|
+
const { locale: noExtensionLocale } = splitUnicodeExtension(locale);
|
|
2285
|
+
noExtensionLocaleMap[noExtensionLocale] ??= locale;
|
|
2286
|
+
setDataProperty(noExtensionLocales, `${index}`, noExtensionLocale);
|
|
2287
|
+
}
|
|
2128
2288
|
const defaultLocale = getDefaultLocale();
|
|
2129
2289
|
const result = findBestMatch(noExtensionLocales, availableLocales, void 0, defaultLocale, false);
|
|
2130
2290
|
if (result.matchedSupportedLocale && result.matchedDesiredLocale) {
|
|
2131
2291
|
foundLocale = result.matchedSupportedLocale;
|
|
2132
2292
|
extension = splitUnicodeExtension(noExtensionLocaleMap[result.matchedDesiredLocale]).extension;
|
|
2133
2293
|
}
|
|
2134
|
-
if (!foundLocale) return {
|
|
2294
|
+
if (!foundLocale) return {
|
|
2295
|
+
locale: defaultLocale,
|
|
2296
|
+
extension: void 0
|
|
2297
|
+
};
|
|
2135
2298
|
return {
|
|
2136
2299
|
locale: foundLocale,
|
|
2137
2300
|
extension
|
|
2138
2301
|
};
|
|
2139
2302
|
}
|
|
2140
|
-
|
|
2141
|
-
//#region node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/aliases.js
|
|
2303
|
+
// node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/aliases.js
|
|
2142
2304
|
const subdivisionAlias = {
|
|
2143
2305
|
"cn11": "cnbj",
|
|
2144
2306
|
"cn12": "cntj",
|
|
@@ -2326,13 +2488,11 @@ const extensionAlias = {
|
|
|
2326
2488
|
"cnckg": "cnsha",
|
|
2327
2489
|
"cnhrb": "cnsha",
|
|
2328
2490
|
"cnkhg": "cnurc",
|
|
2329
|
-
"cst6cdt": "uschi",
|
|
2330
2491
|
"cuba": "cuhav",
|
|
2331
2492
|
"eet": "grath",
|
|
2332
2493
|
"egypt": "egcai",
|
|
2333
2494
|
"eire": "iedub",
|
|
2334
2495
|
"est": "papty",
|
|
2335
|
-
"est5edt": "usnyc",
|
|
2336
2496
|
"factory": "unk",
|
|
2337
2497
|
"gaza": "gazastrp",
|
|
2338
2498
|
"gmt0": "gmt",
|
|
@@ -2347,13 +2507,11 @@ const extensionAlias = {
|
|
|
2347
2507
|
"met": "bebru",
|
|
2348
2508
|
"mncoq": "mnuln",
|
|
2349
2509
|
"mst": "usphx",
|
|
2350
|
-
"mst7mdt": "usden",
|
|
2351
2510
|
"mxstis": "mxtij",
|
|
2352
2511
|
"navajo": "usden",
|
|
2353
2512
|
"poland": "plwaw",
|
|
2354
2513
|
"portugal": "ptlis",
|
|
2355
2514
|
"prc": "cnsha",
|
|
2356
|
-
"pst8pdt": "uslax",
|
|
2357
2515
|
"roc": "twtpe",
|
|
2358
2516
|
"rok": "krsel",
|
|
2359
2517
|
"turkey": "trist",
|
|
@@ -2367,81 +2525,88 @@ const extensionAlias = {
|
|
|
2367
2525
|
}
|
|
2368
2526
|
}
|
|
2369
2527
|
};
|
|
2370
|
-
|
|
2371
|
-
|
|
2528
|
+
// packages/intl-localematcher/abstract/CanonicalizeUValue.ts
|
|
2529
|
+
const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
|
|
2530
|
+
const fromCharCode = String.fromCharCode;
|
|
2372
2531
|
function CanonicalizeUValue(ukey, uvalue) {
|
|
2373
2532
|
invariant(ukey !== void 0, `ukey must be defined`);
|
|
2374
2533
|
let canonicalized = "";
|
|
2375
2534
|
for (let i = 0; i < uvalue.length; i++) {
|
|
2376
|
-
const code = uvalue
|
|
2377
|
-
canonicalized += code >= 65 && code <= 90 ?
|
|
2535
|
+
const code = stringCharCodeAt(uvalue, i);
|
|
2536
|
+
canonicalized += code >= 65 && code <= 90 ? fromCharCode(code + 32) : uvalue[i];
|
|
2378
2537
|
}
|
|
2379
|
-
const aliases =
|
|
2380
|
-
if (aliases &&
|
|
2381
|
-
if ((ukey === "rg" || ukey === "sd") &&
|
|
2538
|
+
const aliases = hasOwn(extensionAlias.u, ukey) ? extensionAlias.u[ukey] : void 0;
|
|
2539
|
+
if (aliases && hasOwn(aliases, canonicalized)) canonicalized = aliases[canonicalized];
|
|
2540
|
+
if ((ukey === "rg" || ukey === "sd") && hasOwn(subdivisionAlias, canonicalized)) canonicalized = subdivisionAlias[canonicalized];
|
|
2382
2541
|
return canonicalized === "true" ? "" : canonicalized;
|
|
2383
2542
|
}
|
|
2384
|
-
|
|
2385
|
-
//#region packages/intl-localematcher/abstract/CanonicalizeUnicodeLocaleId.ts
|
|
2543
|
+
// packages/intl-localematcher/abstract/CanonicalizeUnicodeLocaleId.ts
|
|
2386
2544
|
function CanonicalizeUnicodeLocaleId(locale) {
|
|
2387
|
-
return
|
|
2545
|
+
return CanonicalizeLocaleList(locale)[0];
|
|
2388
2546
|
}
|
|
2389
|
-
|
|
2390
|
-
//#region packages/intl-localematcher/abstract/InsertUnicodeExtensionAndCanonicalize.ts
|
|
2547
|
+
// packages/intl-localematcher/abstract/InsertUnicodeExtensionAndCanonicalize.ts
|
|
2391
2548
|
function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
|
|
2392
2549
|
invariant(splitUnicodeExtension(locale).extension === void 0, "Expected locale to not have a Unicode locale extension");
|
|
2393
2550
|
let extension = "-u";
|
|
2394
|
-
for (
|
|
2395
|
-
for (
|
|
2396
|
-
const { key, value } =
|
|
2551
|
+
for (let i = 0; i < attributes.length; i++) extension += `-${attributes[i]}`;
|
|
2552
|
+
for (let i = 0; i < keywords.length; i++) {
|
|
2553
|
+
const { key, value } = keywords[i];
|
|
2397
2554
|
extension += `-${key}`;
|
|
2398
2555
|
if (value !== "") extension += `-${value}`;
|
|
2399
2556
|
}
|
|
2400
2557
|
if (extension === "-u") return CanonicalizeUnicodeLocaleId(locale);
|
|
2401
|
-
let privateIndex = locale
|
|
2558
|
+
let privateIndex = stringIndexOf(locale, "-x-");
|
|
2402
2559
|
let newLocale;
|
|
2403
2560
|
if (privateIndex === -1) newLocale = locale + extension;
|
|
2404
2561
|
else {
|
|
2405
|
-
let preExtension = locale
|
|
2406
|
-
let postExtension = locale
|
|
2562
|
+
let preExtension = stringSlice(locale, 0, privateIndex);
|
|
2563
|
+
let postExtension = stringSlice(locale, privateIndex);
|
|
2407
2564
|
newLocale = preExtension + extension + postExtension;
|
|
2408
2565
|
}
|
|
2409
2566
|
return CanonicalizeUnicodeLocaleId(newLocale);
|
|
2410
2567
|
}
|
|
2411
|
-
|
|
2412
|
-
|
|
2568
|
+
// packages/intl-localematcher/abstract/BestAvailableLocale.ts
|
|
2569
|
+
const LocaleSet = Set;
|
|
2570
|
+
const setAdd = Function.prototype.call.bind(Set.prototype.add);
|
|
2571
|
+
const setHas = Function.prototype.call.bind(Set.prototype.has);
|
|
2413
2572
|
const availableLocalesSetCache = /* @__PURE__ */ new WeakMap();
|
|
2573
|
+
const getAvailableLocales = availableLocalesSetCache.get.bind(availableLocalesSetCache);
|
|
2574
|
+
const setAvailableLocales = availableLocalesSetCache.set.bind(availableLocalesSetCache);
|
|
2414
2575
|
/**
|
|
2415
|
-
* https://tc39.es/ecma402/#sec-
|
|
2576
|
+
* https://tc39.es/ecma402/#sec-lookupmatchinglocalebyprefix
|
|
2416
2577
|
* @param availableLocales
|
|
2417
2578
|
* @param locale
|
|
2418
2579
|
*/
|
|
2419
2580
|
function BestAvailableLocale(availableLocales, locale) {
|
|
2420
|
-
let availableSet =
|
|
2581
|
+
let availableSet = getAvailableLocales(availableLocales);
|
|
2421
2582
|
if (!availableSet) {
|
|
2422
|
-
availableSet = new
|
|
2423
|
-
|
|
2583
|
+
availableSet = new LocaleSet();
|
|
2584
|
+
for (let i = 0; i < availableLocales.length; i++) setAdd(availableSet, availableLocales[i]);
|
|
2585
|
+
setAvailableLocales(availableLocales, availableSet);
|
|
2424
2586
|
}
|
|
2425
2587
|
let candidate = locale;
|
|
2426
2588
|
while (true) {
|
|
2427
|
-
if (availableSet
|
|
2428
|
-
let pos = candidate
|
|
2589
|
+
if (setHas(availableSet, candidate)) return candidate;
|
|
2590
|
+
let pos = stringLastIndexOf(candidate, "-");
|
|
2429
2591
|
if (!~pos) return;
|
|
2430
2592
|
if (pos >= 2 && candidate[pos - 2] === "-") pos -= 2;
|
|
2431
|
-
candidate = candidate
|
|
2593
|
+
candidate = stringSlice(candidate, 0, pos);
|
|
2432
2594
|
}
|
|
2433
2595
|
}
|
|
2434
|
-
|
|
2435
|
-
//#region packages/intl-localematcher/abstract/LookupMatcher.ts
|
|
2596
|
+
// packages/intl-localematcher/abstract/LookupMatcher.ts
|
|
2436
2597
|
/**
|
|
2437
|
-
* https://tc39.es/ecma402/#sec-
|
|
2598
|
+
* https://tc39.es/ecma402/#sec-lookupmatchinglocalebyprefix
|
|
2438
2599
|
* @param availableLocales
|
|
2439
2600
|
* @param requestedLocales
|
|
2440
2601
|
* @param getDefaultLocale
|
|
2441
2602
|
*/
|
|
2442
2603
|
function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
|
|
2443
|
-
const result = {
|
|
2444
|
-
|
|
2604
|
+
const result = {
|
|
2605
|
+
locale: "",
|
|
2606
|
+
extension: void 0
|
|
2607
|
+
};
|
|
2608
|
+
for (let i = 0; i < requestedLocales.length; i++) {
|
|
2609
|
+
const locale = requestedLocales[i];
|
|
2445
2610
|
const { locale: noExtensionLocale, extension } = splitUnicodeExtension(locale);
|
|
2446
2611
|
const availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
|
|
2447
2612
|
if (availableLocale) {
|
|
@@ -2453,31 +2618,30 @@ function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
|
|
|
2453
2618
|
result.locale = getDefaultLocale();
|
|
2454
2619
|
return result;
|
|
2455
2620
|
}
|
|
2456
|
-
|
|
2457
|
-
//#region packages/intl-localematcher/abstract/UnicodeExtensionComponents.ts
|
|
2621
|
+
// packages/intl-localematcher/abstract/UnicodeExtensionComponents.ts
|
|
2458
2622
|
function UnicodeExtensionComponents(extension) {
|
|
2459
|
-
invariant(extension === extension
|
|
2460
|
-
invariant(extension
|
|
2623
|
+
invariant(extension === stringLowerCase(extension), "Expected extension to be lowercase");
|
|
2624
|
+
invariant(stringSlice(extension, 0, 3) === "-u-", "Expected extension to be a Unicode locale extension");
|
|
2461
2625
|
const attributes = [];
|
|
2462
2626
|
const keywords = [];
|
|
2463
2627
|
let keyword;
|
|
2464
2628
|
let size = extension.length;
|
|
2465
2629
|
let k = 3;
|
|
2466
2630
|
while (k < size) {
|
|
2467
|
-
let e = extension
|
|
2631
|
+
let e = stringIndexOf(extension, "-", k);
|
|
2468
2632
|
let len;
|
|
2469
2633
|
if (e === -1) len = size - k;
|
|
2470
2634
|
else len = e - k;
|
|
2471
|
-
let subtag = extension
|
|
2635
|
+
let subtag = stringSlice(extension, k, k + len);
|
|
2472
2636
|
invariant(len >= 2, "Expected a subtag to have at least 2 characters");
|
|
2473
2637
|
if (keyword === void 0 && len != 2) {
|
|
2474
|
-
if (attributes
|
|
2638
|
+
if (arrayIndexOf(attributes, subtag) === -1) setDataProperty(attributes, `${attributes.length}`, subtag);
|
|
2475
2639
|
} else if (len === 2) {
|
|
2476
2640
|
keyword = {
|
|
2477
2641
|
key: subtag,
|
|
2478
2642
|
value: ""
|
|
2479
2643
|
};
|
|
2480
|
-
if (keywords
|
|
2644
|
+
if (arrayFind(keywords, (k) => k.key === keyword?.key) === void 0) setDataProperty(keywords, `${keywords.length}`, keyword);
|
|
2481
2645
|
} else if (keyword?.value === "") keyword.value = subtag;
|
|
2482
2646
|
else {
|
|
2483
2647
|
invariant(keyword !== void 0, "Expected keyword to be defined");
|
|
@@ -2490,25 +2654,50 @@ function UnicodeExtensionComponents(extension) {
|
|
|
2490
2654
|
keywords
|
|
2491
2655
|
};
|
|
2492
2656
|
}
|
|
2493
|
-
|
|
2494
|
-
//#region packages/intl-localematcher/abstract/ResolveLocale.ts
|
|
2657
|
+
// packages/intl-localematcher/abstract/ResolveLocale.ts
|
|
2495
2658
|
const availableLocaleArrays = /* @__PURE__ */ new WeakMap();
|
|
2496
|
-
|
|
2497
|
-
|
|
2659
|
+
const getLocaleArray = availableLocaleArrays.get.bind(availableLocaleArrays);
|
|
2660
|
+
const setLocaleArray = availableLocaleArrays.set.bind(availableLocaleArrays);
|
|
2661
|
+
const localeApply = Reflect.apply;
|
|
2662
|
+
const localeIsArray = Array.isArray;
|
|
2663
|
+
const localeSetValues = Set.prototype.values;
|
|
2664
|
+
const localeSetSize = Object.getOwnPropertyDescriptor(Set.prototype, "size").get;
|
|
2665
|
+
const localeSetNext = Object.getPrototypeOf((/* @__PURE__ */ new Set()).values()).next;
|
|
2666
|
+
const localeLowerCase = String.prototype.toLowerCase;
|
|
2667
|
+
function localeArray(locales, bestFit) {
|
|
2668
|
+
if (localeIsArray(locales)) return locales;
|
|
2498
2669
|
const available = locales;
|
|
2499
|
-
|
|
2500
|
-
if (cached?.length
|
|
2670
|
+
let cached = getLocaleArray(available);
|
|
2671
|
+
if (cached?.original.length !== localeApply(localeSetSize, available, [])) cached = void 0;
|
|
2672
|
+
if (cached) {
|
|
2673
|
+
const original = cached.original;
|
|
2501
2674
|
let index = 0;
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2505
|
-
|
|
2675
|
+
const iterator = localeApply(localeSetValues, available, []);
|
|
2676
|
+
for (;;) {
|
|
2677
|
+
const next = localeApply(localeSetNext, iterator, []);
|
|
2678
|
+
if (next.done) break;
|
|
2679
|
+
if (original[index++] !== next.value) {
|
|
2680
|
+
cached = void 0;
|
|
2681
|
+
break;
|
|
2682
|
+
}
|
|
2506
2683
|
}
|
|
2507
|
-
return cached;
|
|
2508
2684
|
}
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2685
|
+
if (!cached) {
|
|
2686
|
+
const original = [];
|
|
2687
|
+
const iterator = localeApply(localeSetValues, available, []);
|
|
2688
|
+
for (;;) {
|
|
2689
|
+
const next = localeApply(localeSetNext, iterator, []);
|
|
2690
|
+
if (next.done) break;
|
|
2691
|
+
setDataProperty(original, `${original.length}`, next.value);
|
|
2692
|
+
}
|
|
2693
|
+
cached = {
|
|
2694
|
+
original,
|
|
2695
|
+
snapshot: void 0
|
|
2696
|
+
};
|
|
2697
|
+
setLocaleArray(available, cached);
|
|
2698
|
+
}
|
|
2699
|
+
if (!bestFit) return cached.original;
|
|
2700
|
+
return cached.snapshot ??= createLocaleListSnapshot(cached.original);
|
|
2512
2701
|
}
|
|
2513
2702
|
/**
|
|
2514
2703
|
* https://tc39.es/ecma402/#sec-resolvelocale
|
|
@@ -2516,15 +2705,15 @@ function localeArray(locales) {
|
|
|
2516
2705
|
function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
|
|
2517
2706
|
const matcher = options.localeMatcher;
|
|
2518
2707
|
let r;
|
|
2519
|
-
if (matcher === "lookup") r = LookupMatcher(localeArray(availableLocales), requestedLocales, getDefaultLocale);
|
|
2520
|
-
else r = BestFitMatcher(localeArray(availableLocales), requestedLocales, getDefaultLocale);
|
|
2708
|
+
if (matcher === "lookup") r = LookupMatcher(localeArray(availableLocales, false), requestedLocales, getDefaultLocale);
|
|
2709
|
+
else r = BestFitMatcher(localeArray(availableLocales, true), requestedLocales, getDefaultLocale);
|
|
2521
2710
|
if (r == null) r = {
|
|
2522
2711
|
locale: getDefaultLocale(),
|
|
2523
2712
|
extension: ""
|
|
2524
2713
|
};
|
|
2525
2714
|
let foundLocale = r.locale;
|
|
2526
2715
|
let foundLocaleData = localeData[foundLocale];
|
|
2527
|
-
const result =
|
|
2716
|
+
const result = { __proto__: null };
|
|
2528
2717
|
result.locale = "en";
|
|
2529
2718
|
result.dataLocale = foundLocale;
|
|
2530
2719
|
let components;
|
|
@@ -2534,24 +2723,25 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
|
|
|
2534
2723
|
keywords = components.keywords;
|
|
2535
2724
|
} else keywords = [];
|
|
2536
2725
|
let supportedKeywords = [];
|
|
2537
|
-
for (
|
|
2726
|
+
for (let keyIndex = 0; keyIndex < relevantExtensionKeys.length; keyIndex++) {
|
|
2727
|
+
const key = relevantExtensionKeys[keyIndex];
|
|
2538
2728
|
let keyLocaleData = foundLocaleData?.[key] ?? [];
|
|
2539
|
-
invariant(
|
|
2729
|
+
invariant(localeIsArray(keyLocaleData), `keyLocaleData for ${key} must be an array`);
|
|
2540
2730
|
let value = keyLocaleData[0];
|
|
2541
2731
|
invariant(value == null || typeof value === "string", `value must be a string, null, or undefined`);
|
|
2542
2732
|
let supportedKeyword;
|
|
2543
|
-
let entry = keywords
|
|
2733
|
+
let entry = arrayFind(keywords, (k) => k.key === key);
|
|
2544
2734
|
if (entry) {
|
|
2545
2735
|
let requestedValue = entry.value;
|
|
2546
2736
|
if (requestedValue !== "") {
|
|
2547
|
-
if (keyLocaleData
|
|
2737
|
+
if (arrayIndexOf(keyLocaleData, requestedValue) > -1) {
|
|
2548
2738
|
value = requestedValue;
|
|
2549
2739
|
supportedKeyword = {
|
|
2550
2740
|
key,
|
|
2551
2741
|
value
|
|
2552
2742
|
};
|
|
2553
2743
|
}
|
|
2554
|
-
} else if (keyLocaleData
|
|
2744
|
+
} else if (arrayIndexOf(keyLocaleData, "true") > -1) {
|
|
2555
2745
|
value = "true";
|
|
2556
2746
|
supportedKeyword = {
|
|
2557
2747
|
key,
|
|
@@ -2562,14 +2752,14 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
|
|
|
2562
2752
|
let optionsValue = options[key];
|
|
2563
2753
|
invariant(optionsValue == null || typeof optionsValue === "string", `optionsValue must be a string or undefined`);
|
|
2564
2754
|
if (typeof optionsValue === "string") {
|
|
2565
|
-
optionsValue = CanonicalizeUValue(key
|
|
2755
|
+
optionsValue = CanonicalizeUValue(localeApply(localeLowerCase, key, []), optionsValue);
|
|
2566
2756
|
if (optionsValue === "") optionsValue = "true";
|
|
2567
2757
|
}
|
|
2568
|
-
if (optionsValue !== void 0 && optionsValue !== value && keyLocaleData
|
|
2758
|
+
if (optionsValue !== void 0 && optionsValue !== value && arrayIndexOf(keyLocaleData, optionsValue) > -1) {
|
|
2569
2759
|
value = optionsValue;
|
|
2570
2760
|
supportedKeyword = void 0;
|
|
2571
2761
|
}
|
|
2572
|
-
if (supportedKeyword) supportedKeywords.
|
|
2762
|
+
if (supportedKeyword) setDataProperty(supportedKeywords, `${supportedKeywords.length}`, supportedKeyword);
|
|
2573
2763
|
result[key] = value;
|
|
2574
2764
|
}
|
|
2575
2765
|
let supportedAttributes = [];
|
|
@@ -2580,8 +2770,7 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
|
|
|
2580
2770
|
result.locale = foundLocale;
|
|
2581
2771
|
return result;
|
|
2582
2772
|
}
|
|
2583
|
-
|
|
2584
|
-
//#region packages/intl-localematcher/abstract/LookupSupportedLocales.ts
|
|
2773
|
+
// packages/intl-localematcher/abstract/LookupSupportedLocales.ts
|
|
2585
2774
|
/**
|
|
2586
2775
|
* https://tc39.es/ecma402/#sec-lookupsupportedlocales
|
|
2587
2776
|
* @param availableLocales
|
|
@@ -2595,12 +2784,10 @@ function LookupSupportedLocales(availableLocales, requestedLocales) {
|
|
|
2595
2784
|
}
|
|
2596
2785
|
return subset;
|
|
2597
2786
|
}
|
|
2598
|
-
|
|
2599
|
-
//#region packages/intl-localematcher/index.ts
|
|
2787
|
+
// packages/intl-localematcher/index.ts
|
|
2600
2788
|
function match(requestedLocales, availableLocales, defaultLocale, opts) {
|
|
2601
2789
|
return ResolveLocale(availableLocales, CanonicalizeLocaleList(requestedLocales), { localeMatcher: opts?.algorithm || "best fit" }, [], {}, () => defaultLocale).locale;
|
|
2602
2790
|
}
|
|
2603
|
-
//#endregion
|
|
2604
2791
|
export { LookupSupportedLocales, ResolveLocale, match };
|
|
2605
2792
|
|
|
2606
2793
|
//# sourceMappingURL=index.js.map
|