@formatjs/intl-localematcher 0.10.1 → 0.10.3
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 +18 -3
- package/index.d.ts +5 -9
- package/index.js +459 -201
- package/index.js.map +1 -1
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -1,11 +1,115 @@
|
|
|
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$3 = Reflect.apply;
|
|
6
|
+
const providerKey = Symbol.for("@formatjs/intlCanonicalizeLocaleList");
|
|
7
|
+
function ownCanonicalizer() {
|
|
8
|
+
const descriptor = canonicalIntl && getOwnDescriptor(canonicalIntl, "getCanonicalLocales");
|
|
9
|
+
return descriptor && getOwnDescriptor(descriptor, "value")?.value;
|
|
10
|
+
}
|
|
11
|
+
let canonicalize$1 = ownCanonicalizer();
|
|
12
|
+
function registeredCanonicalizer() {
|
|
13
|
+
try {
|
|
14
|
+
const descriptor = canonicalIntl && getOwnDescriptor(canonicalIntl, providerKey);
|
|
15
|
+
return descriptor && getOwnDescriptor(descriptor, "value")?.value;
|
|
16
|
+
} catch {}
|
|
17
|
+
}
|
|
18
|
+
function CanonicalizeLocaleList(locales) {
|
|
19
|
+
if (canonicalIntl === void 0) canonicalIntl = globalThis.Intl;
|
|
20
|
+
const registered = registeredCanonicalizer();
|
|
21
|
+
if (typeof registered === "function") return apply$3(registered, canonicalIntl, [locales]);
|
|
22
|
+
if (typeof canonicalize$1 !== "function") canonicalize$1 = ownCanonicalizer();
|
|
23
|
+
return apply$3(canonicalize$1, canonicalIntl, [locales]);
|
|
24
|
+
}
|
|
25
|
+
// packages/intl-localematcher/abstract/intrinsics.ts
|
|
26
|
+
const apply$2 = Reflect.apply;
|
|
27
|
+
const includes = Array.prototype.includes;
|
|
28
|
+
const indexOf = Array.prototype.indexOf;
|
|
29
|
+
function arrayIncludes(values, search) {
|
|
30
|
+
return apply$2(includes, values, [search]);
|
|
31
|
+
}
|
|
32
|
+
function arrayIndexOf(values, search) {
|
|
33
|
+
return apply$2(indexOf, values, [search]);
|
|
34
|
+
}
|
|
35
|
+
const find = Array.prototype.find;
|
|
36
|
+
function arrayFind(values, predicate) {
|
|
37
|
+
return apply$2(find, values, [predicate]);
|
|
38
|
+
}
|
|
39
|
+
const defineProperty = Object.defineProperty;
|
|
40
|
+
function setDataProperty(target, key, value) {
|
|
41
|
+
defineProperty(target, key, {
|
|
42
|
+
__proto__: null,
|
|
43
|
+
value,
|
|
44
|
+
writable: true,
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
const stringSlice = Function.prototype.call.bind(String.prototype.slice);
|
|
50
|
+
const stringIndexOf = Function.prototype.call.bind(String.prototype.indexOf);
|
|
51
|
+
const stringLastIndexOf = Function.prototype.call.bind(String.prototype.lastIndexOf);
|
|
52
|
+
const stringLowerCase = Function.prototype.call.bind(String.prototype.toLowerCase);
|
|
53
|
+
const stringCharCodeAt = Function.prototype.call.bind(String.prototype.charCodeAt);
|
|
54
|
+
// packages/intl-localematcher/abstract/BestAvailableLocale.ts
|
|
55
|
+
const LocaleSet$1 = Set;
|
|
56
|
+
const setAdd = Function.prototype.call.bind(Set.prototype.add);
|
|
57
|
+
const setHas = Function.prototype.call.bind(Set.prototype.has);
|
|
58
|
+
const availableLocalesSetCache = /* @__PURE__ */ new WeakMap();
|
|
59
|
+
const getAvailableLocales = availableLocalesSetCache.get.bind(availableLocalesSetCache);
|
|
60
|
+
const setAvailableLocales = availableLocalesSetCache.set.bind(availableLocalesSetCache);
|
|
3
61
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @param
|
|
62
|
+
* https://tc39.es/ecma402/#sec-lookupmatchinglocalebyprefix
|
|
63
|
+
* @param availableLocales
|
|
64
|
+
* @param locale
|
|
6
65
|
*/
|
|
7
|
-
function
|
|
8
|
-
|
|
66
|
+
function BestAvailableLocale(availableLocales, locale) {
|
|
67
|
+
let availableSet = getAvailableLocales(availableLocales);
|
|
68
|
+
if (!availableSet) {
|
|
69
|
+
availableSet = new LocaleSet$1();
|
|
70
|
+
for (let i = 0; i < availableLocales.length; i++) setAdd(availableSet, availableLocales[i]);
|
|
71
|
+
setAvailableLocales(availableLocales, availableSet);
|
|
72
|
+
}
|
|
73
|
+
let candidate = locale;
|
|
74
|
+
while (candidate.length) {
|
|
75
|
+
if (setHas(availableSet, candidate)) return candidate;
|
|
76
|
+
let pos = stringLastIndexOf(candidate, "-");
|
|
77
|
+
if (pos === -1) return;
|
|
78
|
+
while (pos >= 2 && candidate[pos - 2] === "-") pos -= 2;
|
|
79
|
+
candidate = stringSlice(candidate, 0, pos);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
// packages/ecma402-abstract/Locale/splitUnicodeExtension.js
|
|
83
|
+
const apply$1 = Reflect.apply;
|
|
84
|
+
const split = String.prototype.split;
|
|
85
|
+
const hyphen = {
|
|
86
|
+
__proto__: null,
|
|
87
|
+
toString: () => "-"
|
|
88
|
+
};
|
|
89
|
+
function splitLocaleSubtags(value) {
|
|
90
|
+
return apply$1(split, value, [hyphen]);
|
|
91
|
+
}
|
|
92
|
+
function splitUnicodeExtension(locale) {
|
|
93
|
+
const subtags = splitLocaleSubtags(locale);
|
|
94
|
+
for (let i = 1; i < subtags.length; i++) {
|
|
95
|
+
if (subtags[i] === "x") break;
|
|
96
|
+
if (subtags[i] !== "u") continue;
|
|
97
|
+
let end = i + 1;
|
|
98
|
+
while (end < subtags.length && subtags[end].length > 1) end++;
|
|
99
|
+
let base = subtags[0];
|
|
100
|
+
for (let j = 1; j < i; j++) base += "-" + subtags[j];
|
|
101
|
+
for (let j = end; j < subtags.length; j++) base += "-" + subtags[j];
|
|
102
|
+
let extension = "";
|
|
103
|
+
for (let j = i; j < end; j++) extension += "-" + subtags[j];
|
|
104
|
+
return {
|
|
105
|
+
locale: base,
|
|
106
|
+
extension
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
locale,
|
|
111
|
+
extension: void 0
|
|
112
|
+
};
|
|
9
113
|
}
|
|
10
114
|
// node_modules/.aspect_rules_js/@formatjs_generated+cldr.core@0.0.0/node_modules/@formatjs_generated/cldr.core/matching.js
|
|
11
115
|
const matchingData = {
|
|
@@ -1758,20 +1862,17 @@ const matchingData = {
|
|
|
1758
1862
|
"demotion": 5
|
|
1759
1863
|
};
|
|
1760
1864
|
// packages/intl-localematcher/abstract/utils.ts
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
}
|
|
1773
|
-
return { locale };
|
|
1774
|
-
}
|
|
1865
|
+
const apply = Reflect.apply;
|
|
1866
|
+
const some = Array.prototype.some;
|
|
1867
|
+
const sortLocaleIndexes = Array.prototype.sort;
|
|
1868
|
+
const freezeLocaleList = Object.freeze;
|
|
1869
|
+
const LocaleError = Error;
|
|
1870
|
+
const LocaleMap = Map;
|
|
1871
|
+
const LocaleSet = Set;
|
|
1872
|
+
const addLocaleSet = Function.prototype.call.bind(LocaleSet.prototype.add);
|
|
1873
|
+
const hasLocaleSet = Function.prototype.call.bind(LocaleSet.prototype.has);
|
|
1874
|
+
const minDistance = Math.min;
|
|
1875
|
+
const maxDistance = Math.max;
|
|
1775
1876
|
/**
|
|
1776
1877
|
* Asserts that a condition is true, throwing an error if it is not.
|
|
1777
1878
|
* Used for runtime validation and type narrowing.
|
|
@@ -1787,18 +1888,18 @@ function splitUnicodeExtension(locale) {
|
|
|
1787
1888
|
* // locale is now narrowed to non-undefined type
|
|
1788
1889
|
* ```
|
|
1789
1890
|
*/
|
|
1790
|
-
function invariant(condition, message, Err =
|
|
1891
|
+
function invariant(condition, message, Err = LocaleError) {
|
|
1791
1892
|
if (!condition) throw new Err(message);
|
|
1792
1893
|
}
|
|
1793
1894
|
const DEFAULT_MATCHING_THRESHOLD = matchingData.defaults[1];
|
|
1794
1895
|
const DISTANCE_SCALE = 8;
|
|
1795
1896
|
const MIN_LSR_DISTANCE = matchingData.minLsrDistance;
|
|
1796
|
-
const languageAliases = new
|
|
1797
|
-
const scriptAliases = new
|
|
1798
|
-
const regionAliases = new
|
|
1897
|
+
const languageAliases = new LocaleSet(matchingData.canonicalAliases.language);
|
|
1898
|
+
const scriptAliases = new LocaleSet(matchingData.canonicalAliases.script);
|
|
1899
|
+
const regionAliases = new LocaleSet(matchingData.canonicalAliases.region);
|
|
1799
1900
|
function ascii(value, start, end, min, max) {
|
|
1800
1901
|
for (let i = start; i < end; i++) {
|
|
1801
|
-
const code = value
|
|
1902
|
+
const code = stringCharCodeAt(value, i);
|
|
1802
1903
|
if (code < min || code > max) return false;
|
|
1803
1904
|
}
|
|
1804
1905
|
return true;
|
|
@@ -1816,21 +1917,21 @@ function pseudoRegion(value) {
|
|
|
1816
1917
|
return value === "XA" || value === "XB" || value === "XC";
|
|
1817
1918
|
}
|
|
1818
1919
|
function prefix(tag) {
|
|
1819
|
-
let end = tag
|
|
1920
|
+
let end = stringIndexOf(tag, "-");
|
|
1820
1921
|
if (end < 0) end = tag.length;
|
|
1821
|
-
const language = tag
|
|
1922
|
+
const language = stringSlice(tag, 0, end);
|
|
1822
1923
|
let script = "";
|
|
1823
1924
|
let region = "";
|
|
1824
1925
|
if (end < tag.length) {
|
|
1825
|
-
let next = tag
|
|
1926
|
+
let next = stringIndexOf(tag, "-", end + 1);
|
|
1826
1927
|
if (next < 0) next = tag.length;
|
|
1827
|
-
let part = tag
|
|
1928
|
+
let part = stringSlice(tag, end + 1, next);
|
|
1828
1929
|
if (scriptSubtag(part)) {
|
|
1829
1930
|
script = part;
|
|
1830
1931
|
end = next;
|
|
1831
|
-
next = tag
|
|
1932
|
+
next = stringIndexOf(tag, "-", end + 1);
|
|
1832
1933
|
if (next < 0) next = tag.length;
|
|
1833
|
-
part = tag
|
|
1934
|
+
part = stringSlice(tag, end + 1, next);
|
|
1834
1935
|
}
|
|
1835
1936
|
if (regionSubtag(part)) {
|
|
1836
1937
|
region = part;
|
|
@@ -1844,13 +1945,28 @@ function prefix(tag) {
|
|
|
1844
1945
|
end
|
|
1845
1946
|
};
|
|
1846
1947
|
}
|
|
1948
|
+
function scriptlessRegionLocale(tag) {
|
|
1949
|
+
const { language, script, region } = prefix(tag);
|
|
1950
|
+
if (script && region) return language + "-" + region;
|
|
1951
|
+
}
|
|
1952
|
+
function canUseBestFitPrefixFallback(tag) {
|
|
1953
|
+
const { language, script, region } = prefix(tag);
|
|
1954
|
+
return language !== "und" && script !== "Zzzz" && region !== "ZZ" && !pseudoRegion(region) && (stringIndexOf(tag, "-ps") === -1 || fields(tag).pseudo === void 0);
|
|
1955
|
+
}
|
|
1847
1956
|
function fields(tag) {
|
|
1848
1957
|
const parsed = prefix(tag);
|
|
1849
1958
|
const language = parsed.language;
|
|
1850
1959
|
let { script, region } = parsed;
|
|
1851
|
-
const parts = parsed.end < tag.length ? tag
|
|
1852
|
-
|
|
1853
|
-
|
|
1960
|
+
const parts = parsed.end < tag.length ? splitLocaleSubtags(stringSlice(tag, parsed.end + 1)) : [];
|
|
1961
|
+
let pseudo;
|
|
1962
|
+
for (let index = 0; index < parts.length; index++) {
|
|
1963
|
+
const part = parts[index];
|
|
1964
|
+
if (part.length === 1) break;
|
|
1965
|
+
if (part === "psaccent" || part === "psbidi" || part === "pscrack") {
|
|
1966
|
+
pseudo = part;
|
|
1967
|
+
break;
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1854
1970
|
if (!pseudo && !pseudoRegion(region)) {
|
|
1855
1971
|
if (script === "Zzzz") script = "";
|
|
1856
1972
|
if (region === "ZZ") region = "";
|
|
@@ -1862,18 +1978,72 @@ function fields(tag) {
|
|
|
1862
1978
|
region,
|
|
1863
1979
|
flags,
|
|
1864
1980
|
pseudo,
|
|
1865
|
-
key: tag === "und" || tag
|
|
1981
|
+
key: tag === "und" || stringIndexOf(tag, "und-x-") === 0 ? tag : `${language}|${script}|${region}|${flags}|${pseudo || ""}`
|
|
1866
1982
|
};
|
|
1867
1983
|
}
|
|
1868
1984
|
const stringKey = (value) => value;
|
|
1869
1985
|
const canonicalize = memoize((tag) => {
|
|
1870
1986
|
const basic = prefix(tag);
|
|
1871
|
-
if (basic.end === tag.length && languageSubtag(basic.language) && !languageAliases
|
|
1872
|
-
return
|
|
1987
|
+
if (basic.end === tag.length && languageSubtag(basic.language) && !hasLocaleSet(languageAliases, basic.language) && !hasLocaleSet(scriptAliases, basic.script) && !hasLocaleSet(regionAliases, basic.region)) return tag;
|
|
1988
|
+
return CanonicalizeLocaleList(tag)[0];
|
|
1873
1989
|
}, { serializer: stringKey });
|
|
1990
|
+
const localeDescriptor$1 = Object.getOwnPropertyDescriptor;
|
|
1991
|
+
const localePrototype = Object.getPrototypeOf;
|
|
1992
|
+
const LocaleTypeError = TypeError;
|
|
1993
|
+
const localeOperations = /* @__PURE__ */ new WeakMap();
|
|
1994
|
+
const getLocaleOperations = localeOperations.get.bind(localeOperations);
|
|
1995
|
+
const setLocaleOperations = localeOperations.set.bind(localeOperations);
|
|
1996
|
+
let localeIntl = globalThis.Intl;
|
|
1997
|
+
let localeConstructor;
|
|
1998
|
+
function localeDescriptorMember(object, key, member = "value") {
|
|
1999
|
+
const descriptor = localeDescriptor$1(object, key);
|
|
2000
|
+
return descriptor && localeDescriptor$1(descriptor, member)?.value;
|
|
2001
|
+
}
|
|
2002
|
+
function localeFieldReader(prototype, key) {
|
|
2003
|
+
const reader = localeDescriptorMember(prototype, key, "get");
|
|
2004
|
+
return typeof reader === "function" ? reader : void 0;
|
|
2005
|
+
}
|
|
2006
|
+
function captureLocaleOperations(prototype) {
|
|
2007
|
+
if (prototype === null) return void 0;
|
|
2008
|
+
const cached = getLocaleOperations(prototype);
|
|
2009
|
+
if (cached) return cached;
|
|
2010
|
+
const inherited = captureLocaleOperations(localePrototype(prototype));
|
|
2011
|
+
const ownMaximize = localeDescriptorMember(prototype, "maximize");
|
|
2012
|
+
const operations = {
|
|
2013
|
+
maximize: typeof ownMaximize === "function" ? ownMaximize : inherited?.maximize,
|
|
2014
|
+
language: localeFieldReader(prototype, "language") ?? inherited?.language,
|
|
2015
|
+
script: localeFieldReader(prototype, "script") ?? inherited?.script,
|
|
2016
|
+
region: localeFieldReader(prototype, "region") ?? inherited?.region
|
|
2017
|
+
};
|
|
2018
|
+
setLocaleOperations(prototype, operations);
|
|
2019
|
+
return operations;
|
|
2020
|
+
}
|
|
2021
|
+
function getLocaleConstructor() {
|
|
2022
|
+
if (localeIntl === void 0) localeIntl = globalThis.Intl;
|
|
2023
|
+
const installed = localeIntl && localeDescriptorMember(localeIntl, "Locale");
|
|
2024
|
+
if (typeof installed === "function" && installed !== localeConstructor) {
|
|
2025
|
+
const prototype = localeDescriptorMember(installed, "prototype");
|
|
2026
|
+
if (typeof prototype === "object" && prototype !== null || typeof prototype === "function") captureLocaleOperations(prototype);
|
|
2027
|
+
localeConstructor = installed;
|
|
2028
|
+
}
|
|
2029
|
+
return localeConstructor;
|
|
2030
|
+
}
|
|
2031
|
+
getLocaleConstructor();
|
|
2032
|
+
function maximizeLocale(tag) {
|
|
2033
|
+
const Constructor = getLocaleConstructor();
|
|
2034
|
+
if (!Constructor) throw new LocaleTypeError("Intl.Locale provider is unavailable");
|
|
2035
|
+
const locale = new Constructor(tag);
|
|
2036
|
+
const maximize = captureLocaleOperations(localePrototype(locale))?.maximize;
|
|
2037
|
+
if (!maximize) throw new LocaleTypeError("Intl.Locale maximize is unavailable");
|
|
2038
|
+
return apply(maximize, locale, []);
|
|
2039
|
+
}
|
|
2040
|
+
function readLocaleField(locale, key) {
|
|
2041
|
+
const reader = captureLocaleOperations(localePrototype(locale))?.[key];
|
|
2042
|
+
return reader && apply(reader, locale, []);
|
|
2043
|
+
}
|
|
1874
2044
|
const maximal = memoize((tag) => {
|
|
1875
2045
|
const input = fields(tag);
|
|
1876
|
-
if (tag === "und" || tag
|
|
2046
|
+
if (tag === "und" || stringIndexOf(tag, "und-x-") === 0) return {
|
|
1877
2047
|
...input,
|
|
1878
2048
|
language: tag,
|
|
1879
2049
|
script: "",
|
|
@@ -1896,24 +2066,20 @@ const maximal = memoize((tag) => {
|
|
|
1896
2066
|
key: `${language}|${script}|${region}|${flags}`
|
|
1897
2067
|
};
|
|
1898
2068
|
}
|
|
1899
|
-
const locale =
|
|
2069
|
+
const locale = maximizeLocale(tag);
|
|
1900
2070
|
let flags = input.flags;
|
|
1901
|
-
if (matchingData.macroRegions
|
|
1902
|
-
const regionLocale =
|
|
1903
|
-
if (input.language === "und" && (!input.script || input.script === regionLocale
|
|
2071
|
+
if (arrayIncludes(matchingData.macroRegions, input.region)) {
|
|
2072
|
+
const regionLocale = maximizeLocale(`und-${input.region}`);
|
|
2073
|
+
if (input.language === "und" && (!input.script || input.script === readLocaleField(regionLocale, "script")) || !input.script && !readLocaleField(maximizeLocale(input.language), "script")) flags &= -2;
|
|
1904
2074
|
}
|
|
1905
|
-
let language = locale
|
|
1906
|
-
let script = locale
|
|
1907
|
-
let region = locale
|
|
2075
|
+
let language = readLocaleField(locale, "language");
|
|
2076
|
+
let script = readLocaleField(locale, "script") || "";
|
|
2077
|
+
let region = readLocaleField(locale, "region") || "";
|
|
1908
2078
|
if (language === "und" || !script || !region) {
|
|
1909
|
-
const fallback =
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
].filter(Boolean).join("-")).maximize();
|
|
1914
|
-
if (language === "und") language = fallback.language === "und" ? "en" : fallback.language;
|
|
1915
|
-
script ||= fallback.script || "Latn";
|
|
1916
|
-
region ||= fallback.region || "US";
|
|
2079
|
+
const fallback = maximizeLocale("und" + (script ? "-" + script : "") + (region ? "-" + region : ""));
|
|
2080
|
+
if (language === "und") language = readLocaleField(fallback, "language") === "und" ? "en" : readLocaleField(fallback, "language");
|
|
2081
|
+
script ||= readLocaleField(fallback, "script") || "Latn";
|
|
2082
|
+
region ||= readLocaleField(fallback, "region") || "US";
|
|
1917
2083
|
}
|
|
1918
2084
|
return {
|
|
1919
2085
|
language,
|
|
@@ -1925,33 +2091,38 @@ const maximal = memoize((tag) => {
|
|
|
1925
2091
|
}, { serializer: stringKey });
|
|
1926
2092
|
function rawKey(tag) {
|
|
1927
2093
|
if (tag === "und") return "@und";
|
|
1928
|
-
if (tag
|
|
2094
|
+
if (stringIndexOf(tag, "und-x-") === 0) return tag;
|
|
1929
2095
|
const parsed = prefix(tag);
|
|
1930
|
-
const base = tag
|
|
2096
|
+
const base = stringSlice(tag, 0, parsed.end);
|
|
1931
2097
|
if (pseudoRegion(parsed.region)) return base;
|
|
1932
|
-
if (tag
|
|
2098
|
+
if (stringIndexOf(tag, "-ps") !== -1) {
|
|
1933
2099
|
const input = fields(tag);
|
|
1934
2100
|
if (input.pseudo) return input.key;
|
|
1935
2101
|
}
|
|
1936
2102
|
if (parsed.script !== "Zzzz" && parsed.region !== "ZZ") return base;
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
2103
|
+
const script = parsed.script === "Zzzz" ? "" : parsed.script;
|
|
2104
|
+
const region = parsed.region === "ZZ" ? "" : parsed.region;
|
|
2105
|
+
return parsed.language + (script ? "-" + script : "") + (region ? "-" + region : "");
|
|
2106
|
+
}
|
|
2107
|
+
const hasOwnMatchingKey = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
|
|
2108
|
+
function matchingValue(table, key) {
|
|
2109
|
+
return hasOwnMatchingKey(table, key) ? table[key] : void 0;
|
|
1942
2110
|
}
|
|
1943
2111
|
function distance(desired, supported) {
|
|
1944
2112
|
const languages = `${desired.language}|${supported.language}`;
|
|
1945
|
-
let result = desired.language === supported.language ? 0 : matchingData.language
|
|
1946
|
-
if (desired.script !== supported.script) result += matchingData.script
|
|
2113
|
+
let result = desired.language === supported.language ? 0 : matchingValue(matchingData.language, languages) ?? matchingData.defaults[0];
|
|
2114
|
+
if (desired.script !== supported.script) result += matchingValue(matchingData.script, `${languages}|${desired.script}|${supported.script}`) ?? matchingData.defaults[1];
|
|
1947
2115
|
if (desired.region !== supported.region) {
|
|
1948
|
-
const matrix = matchingData.region
|
|
1949
|
-
const desiredPartitions = matchingData.regions
|
|
1950
|
-
const supportedPartitions = matchingData.regions
|
|
2116
|
+
const matrix = matchingValue(matchingData.region, `${languages}|${desired.script}|${supported.script}`) ?? matchingValue(matchingData.region, `${languages}|*|*`);
|
|
2117
|
+
const desiredPartitions = matchingValue(matchingData.regions, desired.region);
|
|
2118
|
+
const supportedPartitions = matchingValue(matchingData.regions, supported.region);
|
|
1951
2119
|
let regionDistance = matchingData.defaults[2];
|
|
1952
2120
|
if (matrix && desiredPartitions && supportedPartitions) {
|
|
1953
2121
|
regionDistance = 0;
|
|
1954
|
-
for (
|
|
2122
|
+
for (let i = 0; i < desiredPartitions.length; i++) {
|
|
2123
|
+
const row = desiredPartitions[i] * matchingData.partitionCount;
|
|
2124
|
+
for (let j = 0; j < supportedPartitions.length; j++) regionDistance = maxDistance(regionDistance, matrix[row + supportedPartitions[j]]);
|
|
2125
|
+
}
|
|
1955
2126
|
}
|
|
1956
2127
|
result += regionDistance;
|
|
1957
2128
|
}
|
|
@@ -1960,66 +2131,84 @@ function distance(desired, supported) {
|
|
|
1960
2131
|
/** ICU language/script/region distance, including fractional explicit-subtag ties. */
|
|
1961
2132
|
const findMatchingDistance = memoize((desired, supported) => distance(maximal(canonicalize(desired)), maximal(canonicalize(supported))), { serializer: (args) => `${args[0]}|${args[1]}` });
|
|
1962
2133
|
const supportedCache = /* @__PURE__ */ new WeakMap();
|
|
2134
|
+
const getSupported = supportedCache.get.bind(supportedCache);
|
|
2135
|
+
const setSupported = supportedCache.set.bind(supportedCache);
|
|
1963
2136
|
const immutableLocaleLists = /* @__PURE__ */ new WeakSet();
|
|
2137
|
+
const markLocaleListImmutable = immutableLocaleLists.add.bind(immutableLocaleLists);
|
|
2138
|
+
const isLocaleListImmutable = immutableLocaleLists.has.bind(immutableLocaleLists);
|
|
1964
2139
|
let paradigms;
|
|
1965
|
-
function createLocaleListSnapshot(locales) {
|
|
1966
|
-
const snapshot =
|
|
1967
|
-
|
|
2140
|
+
function createLocaleListSnapshot(locales, additionalLocales = []) {
|
|
2141
|
+
const snapshot = [];
|
|
2142
|
+
for (let index = 0; index < locales.length; index++) setDataProperty(snapshot, `${index}`, locales[index]);
|
|
2143
|
+
for (let index = 0; index < additionalLocales.length; index++) setDataProperty(snapshot, `${snapshot.length}`, additionalLocales[index]);
|
|
2144
|
+
markLocaleListImmutable(freezeLocaleList(snapshot));
|
|
1968
2145
|
return snapshot;
|
|
1969
2146
|
}
|
|
1970
2147
|
function equivalentKey(lsr) {
|
|
1971
2148
|
return `${lsr.language}|${lsr.script}|${lsr.region}`;
|
|
1972
2149
|
}
|
|
1973
2150
|
function prepare(locales) {
|
|
1974
|
-
const immutable =
|
|
1975
|
-
let result =
|
|
1976
|
-
if (result && !immutable && (result.original.length !== locales.length || result.original
|
|
2151
|
+
const immutable = isLocaleListImmutable(locales);
|
|
2152
|
+
let result = getSupported(locales);
|
|
2153
|
+
if (result && !immutable && (result.original.length !== locales.length || apply(some, result.original, [(tag, index) => tag !== locales[index]]))) result = void 0;
|
|
1977
2154
|
if (!result) {
|
|
1978
2155
|
result = {
|
|
2156
|
+
__proto__: null,
|
|
1979
2157
|
original: immutable ? locales : Array.from(locales),
|
|
1980
2158
|
tags: [],
|
|
1981
|
-
raw:
|
|
1982
|
-
orders:
|
|
1983
|
-
best:
|
|
2159
|
+
raw: new LocaleMap(),
|
|
2160
|
+
orders: new LocaleMap(),
|
|
2161
|
+
best: new LocaleMap()
|
|
1984
2162
|
};
|
|
1985
|
-
|
|
2163
|
+
setSupported(locales, result);
|
|
1986
2164
|
}
|
|
1987
2165
|
return result;
|
|
1988
2166
|
}
|
|
2167
|
+
const getLocaleMap = Function.prototype.call.bind(LocaleMap.prototype.get);
|
|
2168
|
+
const hasLocaleMap = Function.prototype.call.bind(LocaleMap.prototype.has);
|
|
2169
|
+
const setLocaleMap = Function.prototype.call.bind(LocaleMap.prototype.set);
|
|
2170
|
+
const localeMapSize = Object.getOwnPropertyDescriptor(LocaleMap.prototype, "size").get;
|
|
2171
|
+
const clearLocaleMap = LocaleMap.prototype.clear;
|
|
1989
2172
|
function rawMatch(data, key) {
|
|
1990
|
-
const cached = data.raw
|
|
2173
|
+
const cached = getLocaleMap(data.raw, key);
|
|
1991
2174
|
if (cached !== void 0) return cached;
|
|
1992
2175
|
for (let index = data.tags.length; index < data.original.length; index++) {
|
|
1993
2176
|
const tag = canonicalize(data.original[index]);
|
|
1994
|
-
|
|
1995
|
-
value: tag,
|
|
1996
|
-
writable: true,
|
|
1997
|
-
enumerable: true,
|
|
1998
|
-
configurable: true
|
|
1999
|
-
});
|
|
2177
|
+
setDataProperty(data.tags, `${index}`, tag);
|
|
2000
2178
|
const candidate = rawKey(tag);
|
|
2001
|
-
if (!data.raw
|
|
2002
|
-
if (candidate === key) return data.raw
|
|
2179
|
+
if (!hasLocaleMap(data.raw, candidate)) setLocaleMap(data.raw, candidate, index);
|
|
2180
|
+
if (candidate === key) return getLocaleMap(data.raw, candidate);
|
|
2003
2181
|
}
|
|
2004
2182
|
}
|
|
2005
2183
|
function order(data, defaultLocale) {
|
|
2006
|
-
let result = data.orders
|
|
2184
|
+
let result = getLocaleMap(data.orders, defaultLocale);
|
|
2007
2185
|
if (!result) {
|
|
2008
2186
|
if (!data.maximal) {
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
data.maximal
|
|
2012
|
-
|
|
2013
|
-
|
|
2187
|
+
const profiles = [];
|
|
2188
|
+
for (let index = 0; index < data.tags.length; index++) setDataProperty(profiles, `${index}`, maximal(data.tags[index]));
|
|
2189
|
+
data.maximal = profiles;
|
|
2190
|
+
const exact = new LocaleMap();
|
|
2191
|
+
data.exact = exact;
|
|
2192
|
+
for (let index = 0; index < profiles.length; index++) {
|
|
2193
|
+
const { key } = profiles[index];
|
|
2194
|
+
if (!hasLocaleMap(exact, key)) setLocaleMap(exact, key, index);
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
if (!paradigms) {
|
|
2198
|
+
const keys = new LocaleSet();
|
|
2199
|
+
const tags = matchingData.paradigms;
|
|
2200
|
+
for (let index = 0; index < tags.length; index++) addLocaleSet(keys, equivalentKey(maximal(canonicalize(tags[index]))));
|
|
2201
|
+
paradigms = keys;
|
|
2014
2202
|
}
|
|
2015
|
-
paradigms ??= new Set(matchingData.paradigms.map((tag) => equivalentKey(maximal(canonicalize(tag)))));
|
|
2016
2203
|
const defaultKey = defaultLocale ? equivalentKey(maximal(canonicalize(defaultLocale))) : "";
|
|
2017
2204
|
const rank = (index) => {
|
|
2018
2205
|
const key = equivalentKey(data.maximal[index]);
|
|
2019
|
-
return key === defaultKey ? 0 : paradigms
|
|
2206
|
+
return key === defaultKey ? 0 : hasLocaleSet(paradigms, key) ? 1 : 2;
|
|
2020
2207
|
};
|
|
2021
|
-
result =
|
|
2022
|
-
data.
|
|
2208
|
+
result = [];
|
|
2209
|
+
for (let index = 0; index < data.tags.length; index++) setDataProperty(result, `${index}`, index);
|
|
2210
|
+
apply(sortLocaleIndexes, result, [(a, b) => rank(a) - rank(b)]);
|
|
2211
|
+
setLocaleMap(data.orders, defaultLocale, result);
|
|
2023
2212
|
}
|
|
2024
2213
|
return result;
|
|
2025
2214
|
}
|
|
@@ -2032,17 +2221,15 @@ function moreLikely(candidate, previous) {
|
|
|
2032
2221
|
function equivalentMatch(desired, data) {
|
|
2033
2222
|
let best;
|
|
2034
2223
|
for (let flags = 0; flags < 8; flags++) {
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
].filter(Boolean).join("-");
|
|
2040
|
-
const index = data.raw.get(key);
|
|
2224
|
+
let key = flags & 4 ? desired.language : "und";
|
|
2225
|
+
if (flags & 2 && desired.script) key += "-" + desired.script;
|
|
2226
|
+
if (flags & 1 && desired.region) key += "-" + desired.region;
|
|
2227
|
+
const index = getLocaleMap(data.raw, key);
|
|
2041
2228
|
if (index === void 0) continue;
|
|
2042
2229
|
const supported = maximal(data.tags[index]);
|
|
2043
2230
|
if (equivalentKey(desired) !== equivalentKey(supported)) continue;
|
|
2044
2231
|
const d = distance(desired, supported);
|
|
2045
|
-
if (d <
|
|
2232
|
+
if (d < minDistance(1, MIN_LSR_DISTANCE) && (!best || d < best.distance || d === best.distance && index < best.index)) best = {
|
|
2046
2233
|
index,
|
|
2047
2234
|
distance: d
|
|
2048
2235
|
};
|
|
@@ -2051,7 +2238,10 @@ function equivalentMatch(desired, data) {
|
|
|
2051
2238
|
}
|
|
2052
2239
|
/** Cached exact LSR lookup, then ICU distance matching. Threshold is exclusive. */
|
|
2053
2240
|
function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_MATCHING_THRESHOLD, defaultLocale = supportedLocales[0] || "", collectDistances = true) {
|
|
2054
|
-
const result = {
|
|
2241
|
+
const result = {
|
|
2242
|
+
__proto__: null,
|
|
2243
|
+
distances: {}
|
|
2244
|
+
};
|
|
2055
2245
|
if (!supportedLocales.length || !requestedLocales.length || threshold <= 0) return result;
|
|
2056
2246
|
const data = prepare(supportedLocales);
|
|
2057
2247
|
const demotion = matchingData.demotion;
|
|
@@ -2069,9 +2259,9 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2069
2259
|
const desired = maximal(canonical);
|
|
2070
2260
|
const cacheKey = `${canonical}|${defaultLocale}`;
|
|
2071
2261
|
if (!collectDistances) {
|
|
2072
|
-
const equivalent = data.best
|
|
2073
|
-
if (equivalent && equivalent.distance <
|
|
2074
|
-
data.best
|
|
2262
|
+
const equivalent = getLocaleMap(data.best, cacheKey) || equivalentMatch(desired, data);
|
|
2263
|
+
if (equivalent && equivalent.distance < minDistance(1, MIN_LSR_DISTANCE)) {
|
|
2264
|
+
setLocaleMap(data.best, cacheKey, equivalent);
|
|
2075
2265
|
if (equivalent.distance < bestDistance) {
|
|
2076
2266
|
bestDistance = equivalent.distance;
|
|
2077
2267
|
result.matchedDesiredLocale = desiredTag;
|
|
@@ -2082,21 +2272,22 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2082
2272
|
}
|
|
2083
2273
|
}
|
|
2084
2274
|
const indexes = order(data, defaultLocale);
|
|
2085
|
-
const exact = data.exact
|
|
2275
|
+
const exact = getLocaleMap(data.exact, desired.key);
|
|
2086
2276
|
if (exact !== void 0) {
|
|
2087
2277
|
result.matchedDesiredLocale = desiredTag;
|
|
2088
2278
|
result.matchedSupportedLocale = supportedLocales[exact];
|
|
2089
2279
|
if (collectDistances) result.distances[desiredTag] = { [supportedLocales[exact]]: i * demotion };
|
|
2090
2280
|
return result;
|
|
2091
2281
|
}
|
|
2092
|
-
let best = collectDistances ? void 0 : data.best
|
|
2282
|
+
let best = collectDistances ? void 0 : getLocaleMap(data.best, cacheKey);
|
|
2093
2283
|
if (!best) {
|
|
2094
2284
|
best = {
|
|
2095
2285
|
index: -1,
|
|
2096
|
-
distance:
|
|
2286
|
+
distance: 1e400
|
|
2097
2287
|
};
|
|
2098
2288
|
const distances = {};
|
|
2099
|
-
for (
|
|
2289
|
+
for (let candidate = 0; candidate < indexes.length; candidate++) {
|
|
2290
|
+
const index = indexes[candidate];
|
|
2100
2291
|
const d = collectDistances ? findMatchingDistance(desiredTag, supportedLocales[index]) : distance(desired, data.maximal[index]);
|
|
2101
2292
|
if (collectDistances) distances[supportedLocales[index]] = d + i * demotion;
|
|
2102
2293
|
if (d < best.distance || d === best.distance && d >= 1 && best.index >= 0 && moreLikely(data.maximal[index], data.maximal[best.index])) best = {
|
|
@@ -2106,8 +2297,8 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2106
2297
|
}
|
|
2107
2298
|
if (collectDistances) result.distances[desiredTag] = distances;
|
|
2108
2299
|
else {
|
|
2109
|
-
if (data.best
|
|
2110
|
-
data.best
|
|
2300
|
+
if (apply(localeMapSize, data.best, []) >= 256) apply(clearLocaleMap, data.best, []);
|
|
2301
|
+
setLocaleMap(data.best, cacheKey, best);
|
|
2111
2302
|
}
|
|
2112
2303
|
}
|
|
2113
2304
|
if (best.distance < bestDistance) {
|
|
@@ -2121,7 +2312,7 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2121
2312
|
}
|
|
2122
2313
|
// packages/intl-localematcher/abstract/BestFitMatcher.ts
|
|
2123
2314
|
/**
|
|
2124
|
-
* https://tc39.es/ecma402/#sec-
|
|
2315
|
+
* https://tc39.es/ecma402/#sec-lookupmatchinglocalebybestfit
|
|
2125
2316
|
* @param availableLocales
|
|
2126
2317
|
* @param requestedLocales
|
|
2127
2318
|
* @param getDefaultLocale
|
|
@@ -2129,19 +2320,24 @@ function findBestMatch(requestedLocales, supportedLocales, threshold = DEFAULT_M
|
|
|
2129
2320
|
function BestFitMatcher(availableLocales, requestedLocales, getDefaultLocale) {
|
|
2130
2321
|
let foundLocale;
|
|
2131
2322
|
let extension;
|
|
2132
|
-
const noExtensionLocaleMap =
|
|
2133
|
-
const noExtensionLocales =
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2323
|
+
const noExtensionLocaleMap = { __proto__: null };
|
|
2324
|
+
const noExtensionLocales = [];
|
|
2325
|
+
for (let index = 0; index < requestedLocales.length; index++) {
|
|
2326
|
+
const locale = requestedLocales[index];
|
|
2327
|
+
const { locale: noExtensionLocale } = splitUnicodeExtension(locale);
|
|
2328
|
+
noExtensionLocaleMap[noExtensionLocale] ??= locale;
|
|
2329
|
+
setDataProperty(noExtensionLocales, `${index}`, noExtensionLocale);
|
|
2330
|
+
}
|
|
2138
2331
|
const defaultLocale = getDefaultLocale();
|
|
2139
2332
|
const result = findBestMatch(noExtensionLocales, availableLocales, void 0, defaultLocale, false);
|
|
2140
2333
|
if (result.matchedSupportedLocale && result.matchedDesiredLocale) {
|
|
2141
2334
|
foundLocale = result.matchedSupportedLocale;
|
|
2142
2335
|
extension = splitUnicodeExtension(noExtensionLocaleMap[result.matchedDesiredLocale]).extension;
|
|
2143
2336
|
}
|
|
2144
|
-
if (!foundLocale) return {
|
|
2337
|
+
if (!foundLocale) return {
|
|
2338
|
+
locale: defaultLocale,
|
|
2339
|
+
extension: void 0
|
|
2340
|
+
};
|
|
2145
2341
|
return {
|
|
2146
2342
|
locale: foundLocale,
|
|
2147
2343
|
extension
|
|
@@ -2373,75 +2569,59 @@ const extensionAlias = {
|
|
|
2373
2569
|
}
|
|
2374
2570
|
};
|
|
2375
2571
|
// packages/intl-localematcher/abstract/CanonicalizeUValue.ts
|
|
2572
|
+
const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
|
|
2573
|
+
const fromCharCode = String.fromCharCode;
|
|
2376
2574
|
function CanonicalizeUValue(ukey, uvalue) {
|
|
2377
2575
|
invariant(ukey !== void 0, `ukey must be defined`);
|
|
2378
2576
|
let canonicalized = "";
|
|
2379
2577
|
for (let i = 0; i < uvalue.length; i++) {
|
|
2380
|
-
const code = uvalue
|
|
2381
|
-
canonicalized += code >= 65 && code <= 90 ?
|
|
2578
|
+
const code = stringCharCodeAt(uvalue, i);
|
|
2579
|
+
canonicalized += code >= 65 && code <= 90 ? fromCharCode(code + 32) : uvalue[i];
|
|
2382
2580
|
}
|
|
2383
|
-
const aliases =
|
|
2384
|
-
if (aliases &&
|
|
2385
|
-
if ((ukey === "rg" || ukey === "sd") &&
|
|
2581
|
+
const aliases = hasOwn(extensionAlias.u, ukey) ? extensionAlias.u[ukey] : void 0;
|
|
2582
|
+
if (aliases && hasOwn(aliases, canonicalized)) canonicalized = aliases[canonicalized];
|
|
2583
|
+
if ((ukey === "rg" || ukey === "sd") && hasOwn(subdivisionAlias, canonicalized)) canonicalized = subdivisionAlias[canonicalized];
|
|
2386
2584
|
return canonicalized === "true" ? "" : canonicalized;
|
|
2387
2585
|
}
|
|
2388
2586
|
// packages/intl-localematcher/abstract/CanonicalizeUnicodeLocaleId.ts
|
|
2389
2587
|
function CanonicalizeUnicodeLocaleId(locale) {
|
|
2390
|
-
return
|
|
2588
|
+
return CanonicalizeLocaleList(locale)[0];
|
|
2391
2589
|
}
|
|
2392
2590
|
// packages/intl-localematcher/abstract/InsertUnicodeExtensionAndCanonicalize.ts
|
|
2393
2591
|
function InsertUnicodeExtensionAndCanonicalize(locale, attributes, keywords) {
|
|
2394
2592
|
invariant(splitUnicodeExtension(locale).extension === void 0, "Expected locale to not have a Unicode locale extension");
|
|
2395
2593
|
let extension = "-u";
|
|
2396
|
-
for (
|
|
2397
|
-
for (
|
|
2398
|
-
const { key, value } =
|
|
2594
|
+
for (let i = 0; i < attributes.length; i++) extension += `-${attributes[i]}`;
|
|
2595
|
+
for (let i = 0; i < keywords.length; i++) {
|
|
2596
|
+
const { key, value } = keywords[i];
|
|
2399
2597
|
extension += `-${key}`;
|
|
2400
2598
|
if (value !== "") extension += `-${value}`;
|
|
2401
2599
|
}
|
|
2402
2600
|
if (extension === "-u") return CanonicalizeUnicodeLocaleId(locale);
|
|
2403
|
-
let privateIndex = locale
|
|
2601
|
+
let privateIndex = stringIndexOf(locale, "-x-");
|
|
2404
2602
|
let newLocale;
|
|
2405
2603
|
if (privateIndex === -1) newLocale = locale + extension;
|
|
2406
2604
|
else {
|
|
2407
|
-
let preExtension = locale
|
|
2408
|
-
let postExtension = locale
|
|
2605
|
+
let preExtension = stringSlice(locale, 0, privateIndex);
|
|
2606
|
+
let postExtension = stringSlice(locale, privateIndex);
|
|
2409
2607
|
newLocale = preExtension + extension + postExtension;
|
|
2410
2608
|
}
|
|
2411
2609
|
return CanonicalizeUnicodeLocaleId(newLocale);
|
|
2412
2610
|
}
|
|
2413
|
-
// packages/intl-localematcher/abstract/BestAvailableLocale.ts
|
|
2414
|
-
const availableLocalesSetCache = /* @__PURE__ */ new WeakMap();
|
|
2415
|
-
/**
|
|
2416
|
-
* https://tc39.es/ecma402/#sec-bestavailablelocale
|
|
2417
|
-
* @param availableLocales
|
|
2418
|
-
* @param locale
|
|
2419
|
-
*/
|
|
2420
|
-
function BestAvailableLocale(availableLocales, locale) {
|
|
2421
|
-
let availableSet = availableLocalesSetCache.get(availableLocales);
|
|
2422
|
-
if (!availableSet) {
|
|
2423
|
-
availableSet = new Set(availableLocales);
|
|
2424
|
-
availableLocalesSetCache.set(availableLocales, availableSet);
|
|
2425
|
-
}
|
|
2426
|
-
let candidate = locale;
|
|
2427
|
-
while (true) {
|
|
2428
|
-
if (availableSet.has(candidate)) return candidate;
|
|
2429
|
-
let pos = candidate.lastIndexOf("-");
|
|
2430
|
-
if (!~pos) return;
|
|
2431
|
-
if (pos >= 2 && candidate[pos - 2] === "-") pos -= 2;
|
|
2432
|
-
candidate = candidate.slice(0, pos);
|
|
2433
|
-
}
|
|
2434
|
-
}
|
|
2435
2611
|
// packages/intl-localematcher/abstract/LookupMatcher.ts
|
|
2436
2612
|
/**
|
|
2437
|
-
* https://tc39.es/ecma402/#sec-
|
|
2613
|
+
* https://tc39.es/ecma402/#sec-lookupmatchinglocalebyprefix
|
|
2438
2614
|
* @param availableLocales
|
|
2439
2615
|
* @param requestedLocales
|
|
2440
2616
|
* @param getDefaultLocale
|
|
2441
2617
|
*/
|
|
2442
2618
|
function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
|
|
2443
|
-
const result = {
|
|
2444
|
-
|
|
2619
|
+
const result = {
|
|
2620
|
+
locale: "",
|
|
2621
|
+
extension: void 0
|
|
2622
|
+
};
|
|
2623
|
+
for (let i = 0; i < requestedLocales.length; i++) {
|
|
2624
|
+
const locale = requestedLocales[i];
|
|
2445
2625
|
const { locale: noExtensionLocale, extension } = splitUnicodeExtension(locale);
|
|
2446
2626
|
const availableLocale = BestAvailableLocale(availableLocales, noExtensionLocale);
|
|
2447
2627
|
if (availableLocale) {
|
|
@@ -2455,28 +2635,28 @@ function LookupMatcher(availableLocales, requestedLocales, getDefaultLocale) {
|
|
|
2455
2635
|
}
|
|
2456
2636
|
// packages/intl-localematcher/abstract/UnicodeExtensionComponents.ts
|
|
2457
2637
|
function UnicodeExtensionComponents(extension) {
|
|
2458
|
-
invariant(extension === extension
|
|
2459
|
-
invariant(extension
|
|
2638
|
+
invariant(extension === stringLowerCase(extension), "Expected extension to be lowercase");
|
|
2639
|
+
invariant(stringSlice(extension, 0, 3) === "-u-", "Expected extension to be a Unicode locale extension");
|
|
2460
2640
|
const attributes = [];
|
|
2461
2641
|
const keywords = [];
|
|
2462
2642
|
let keyword;
|
|
2463
2643
|
let size = extension.length;
|
|
2464
2644
|
let k = 3;
|
|
2465
2645
|
while (k < size) {
|
|
2466
|
-
let e = extension
|
|
2646
|
+
let e = stringIndexOf(extension, "-", k);
|
|
2467
2647
|
let len;
|
|
2468
2648
|
if (e === -1) len = size - k;
|
|
2469
2649
|
else len = e - k;
|
|
2470
|
-
let subtag = extension
|
|
2650
|
+
let subtag = stringSlice(extension, k, k + len);
|
|
2471
2651
|
invariant(len >= 2, "Expected a subtag to have at least 2 characters");
|
|
2472
2652
|
if (keyword === void 0 && len != 2) {
|
|
2473
|
-
if (attributes
|
|
2653
|
+
if (arrayIndexOf(attributes, subtag) === -1) setDataProperty(attributes, `${attributes.length}`, subtag);
|
|
2474
2654
|
} else if (len === 2) {
|
|
2475
2655
|
keyword = {
|
|
2476
2656
|
key: subtag,
|
|
2477
2657
|
value: ""
|
|
2478
2658
|
};
|
|
2479
|
-
if (keywords
|
|
2659
|
+
if (arrayFind(keywords, (k) => k.key === keyword?.key) === void 0) setDataProperty(keywords, `${keywords.length}`, keyword);
|
|
2480
2660
|
} else if (keyword?.value === "") keyword.value = subtag;
|
|
2481
2661
|
else {
|
|
2482
2662
|
invariant(keyword !== void 0, "Expected keyword to be defined");
|
|
@@ -2491,25 +2671,100 @@ function UnicodeExtensionComponents(extension) {
|
|
|
2491
2671
|
}
|
|
2492
2672
|
// packages/intl-localematcher/abstract/ResolveLocale.ts
|
|
2493
2673
|
const availableLocaleArrays = /* @__PURE__ */ new WeakMap();
|
|
2494
|
-
|
|
2495
|
-
|
|
2674
|
+
const getLocaleArray = availableLocaleArrays.get.bind(availableLocaleArrays);
|
|
2675
|
+
const setLocaleArray = availableLocaleArrays.set.bind(availableLocaleArrays);
|
|
2676
|
+
const localeApply = Reflect.apply;
|
|
2677
|
+
const localeIsArray = Array.isArray;
|
|
2678
|
+
const localeSetValues = Set.prototype.values;
|
|
2679
|
+
const localeSetSize = Object.getOwnPropertyDescriptor(Set.prototype, "size").get;
|
|
2680
|
+
const localeSetNext = Object.getPrototypeOf((/* @__PURE__ */ new Set()).values()).next;
|
|
2681
|
+
const localeLowerCase = String.prototype.toLowerCase;
|
|
2682
|
+
const localeDescriptor = Object.getOwnPropertyDescriptor;
|
|
2683
|
+
const hostDefaultLocaleKey = Symbol.for("@formatjs/intlHostDefaultLocale");
|
|
2684
|
+
function readHostDefaultLocale() {
|
|
2685
|
+
const intl = localeDescriptorMember(globalThis, "Intl");
|
|
2686
|
+
if (!intl) return void 0;
|
|
2687
|
+
let canRetain = false;
|
|
2688
|
+
try {
|
|
2689
|
+
const retained = localeDescriptor(intl, hostDefaultLocaleKey);
|
|
2690
|
+
const cached = retained && localeDescriptor(retained, "value")?.value;
|
|
2691
|
+
if (typeof cached === "string") return cached;
|
|
2692
|
+
canRetain = retained === void 0;
|
|
2693
|
+
} catch {}
|
|
2694
|
+
for (const name of ["NumberFormat", "DateTimeFormat"]) try {
|
|
2695
|
+
const Constructor = localeDescriptorMember(intl, name);
|
|
2696
|
+
if (typeof Constructor !== "function" || localeDescriptorMember(Constructor, "polyfilled")) continue;
|
|
2697
|
+
const prototype = localeDescriptorMember(Constructor, "prototype");
|
|
2698
|
+
if (!prototype) continue;
|
|
2699
|
+
const resolve = localeDescriptorMember(prototype, "resolvedOptions");
|
|
2700
|
+
if (typeof resolve !== "function") continue;
|
|
2701
|
+
const locale = localeDescriptorMember(localeApply(resolve, new Constructor(), []), "locale");
|
|
2702
|
+
if (typeof locale !== "string" || !locale) continue;
|
|
2703
|
+
const tag = splitUnicodeExtension(locale).locale;
|
|
2704
|
+
try {
|
|
2705
|
+
if (canRetain && Object.isExtensible(intl)) Object.defineProperty(intl, hostDefaultLocaleKey, {
|
|
2706
|
+
__proto__: null,
|
|
2707
|
+
value: tag
|
|
2708
|
+
});
|
|
2709
|
+
} catch {}
|
|
2710
|
+
return tag;
|
|
2711
|
+
} catch {}
|
|
2712
|
+
}
|
|
2713
|
+
const hostDefaultLocale = readHostDefaultLocale();
|
|
2714
|
+
const hostRegionLocale = hostDefaultLocale && scriptlessRegionLocale(hostDefaultLocale);
|
|
2715
|
+
const hostLocaleSupport = /* @__PURE__ */ new WeakMap();
|
|
2716
|
+
const getHostLocaleSupport = hostLocaleSupport.get.bind(hostLocaleSupport);
|
|
2717
|
+
const setHostLocaleSupport = hostLocaleSupport.set.bind(hostLocaleSupport);
|
|
2718
|
+
function getServiceLocaleList(locales, bestFit) {
|
|
2719
|
+
const available = getAvailableLocaleList(locales, bestFit);
|
|
2720
|
+
const cacheable = !localeIsArray(locales);
|
|
2721
|
+
const cached = cacheable && getHostLocaleSupport(available);
|
|
2722
|
+
if (cached) return cached;
|
|
2723
|
+
const dataLocale = hostDefaultLocale && BestAvailableLocale(available, hostDefaultLocale);
|
|
2724
|
+
const aliases = [];
|
|
2725
|
+
if (hostDefaultLocale && dataLocale && dataLocale !== hostDefaultLocale) setDataProperty(aliases, 0, hostDefaultLocale);
|
|
2726
|
+
const regionAlias = dataLocale && hostRegionLocale && arrayIndexOf(available, hostRegionLocale) < 0 ? hostRegionLocale : void 0;
|
|
2727
|
+
if (regionAlias) setDataProperty(aliases, aliases.length, regionAlias);
|
|
2728
|
+
const support = {
|
|
2729
|
+
available: aliases.length ? createLocaleListSnapshot(available, aliases) : available,
|
|
2730
|
+
defaultLocale: dataLocale ? hostDefaultLocale : void 0,
|
|
2731
|
+
dataLocale,
|
|
2732
|
+
regionAlias
|
|
2733
|
+
};
|
|
2734
|
+
if (cacheable) setHostLocaleSupport(available, support);
|
|
2735
|
+
return support;
|
|
2736
|
+
}
|
|
2737
|
+
function getAvailableLocaleList(locales, bestFit) {
|
|
2738
|
+
if (localeIsArray(locales)) return locales;
|
|
2496
2739
|
const available = locales;
|
|
2497
|
-
let cached =
|
|
2498
|
-
if (cached?.original.length !== available
|
|
2740
|
+
let cached = getLocaleArray(available);
|
|
2741
|
+
if (cached?.original.length !== localeApply(localeSetSize, available, [])) cached = void 0;
|
|
2499
2742
|
if (cached) {
|
|
2500
2743
|
const original = cached.original;
|
|
2501
2744
|
let index = 0;
|
|
2502
|
-
|
|
2503
|
-
|
|
2504
|
-
|
|
2745
|
+
const iterator = localeApply(localeSetValues, available, []);
|
|
2746
|
+
for (;;) {
|
|
2747
|
+
const next = localeApply(localeSetNext, iterator, []);
|
|
2748
|
+
if (next.done) break;
|
|
2749
|
+
if (original[index++] !== next.value) {
|
|
2750
|
+
cached = void 0;
|
|
2751
|
+
break;
|
|
2752
|
+
}
|
|
2505
2753
|
}
|
|
2506
2754
|
}
|
|
2507
2755
|
if (!cached) {
|
|
2756
|
+
const original = [];
|
|
2757
|
+
const iterator = localeApply(localeSetValues, available, []);
|
|
2758
|
+
for (;;) {
|
|
2759
|
+
const next = localeApply(localeSetNext, iterator, []);
|
|
2760
|
+
if (next.done) break;
|
|
2761
|
+
setDataProperty(original, `${original.length}`, next.value);
|
|
2762
|
+
}
|
|
2508
2763
|
cached = {
|
|
2509
|
-
original
|
|
2764
|
+
original,
|
|
2510
2765
|
snapshot: void 0
|
|
2511
2766
|
};
|
|
2512
|
-
|
|
2767
|
+
setLocaleArray(available, cached);
|
|
2513
2768
|
}
|
|
2514
2769
|
if (!bestFit) return cached.original;
|
|
2515
2770
|
return cached.snapshot ??= createLocaleListSnapshot(cached.original);
|
|
@@ -2517,20 +2772,25 @@ function localeArray(locales, bestFit) {
|
|
|
2517
2772
|
/**
|
|
2518
2773
|
* https://tc39.es/ecma402/#sec-resolvelocale
|
|
2519
2774
|
*/
|
|
2520
|
-
function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale) {
|
|
2775
|
+
function ResolveLocale(availableLocales, requestedLocales, options, relevantExtensionKeys, localeData, getDefaultLocale, mode = void 0) {
|
|
2521
2776
|
const matcher = options.localeMatcher;
|
|
2777
|
+
const hostSupport = mode === "hostDefaultWithLocaleData" ? getServiceLocaleList(availableLocales, matcher !== "lookup") : void 0;
|
|
2778
|
+
const available = hostSupport?.available ?? getAvailableLocaleList(availableLocales, matcher !== "lookup");
|
|
2779
|
+
const hostLocale = hostSupport?.defaultLocale;
|
|
2780
|
+
const defaultLocale = hostLocale ? () => hostLocale : getDefaultLocale;
|
|
2522
2781
|
let r;
|
|
2523
|
-
if (matcher === "lookup") r = LookupMatcher(
|
|
2524
|
-
else r = BestFitMatcher(
|
|
2782
|
+
if (matcher === "lookup") r = LookupMatcher(available, requestedLocales, defaultLocale);
|
|
2783
|
+
else r = BestFitMatcher(available, requestedLocales, defaultLocale);
|
|
2525
2784
|
if (r == null) r = {
|
|
2526
|
-
locale:
|
|
2785
|
+
locale: defaultLocale(),
|
|
2527
2786
|
extension: ""
|
|
2528
2787
|
};
|
|
2529
2788
|
let foundLocale = r.locale;
|
|
2530
|
-
|
|
2531
|
-
const
|
|
2532
|
-
|
|
2533
|
-
result.dataLocale =
|
|
2789
|
+
const result = { __proto__: null };
|
|
2790
|
+
const dataLocale = foundLocale === hostLocale || foundLocale === hostSupport?.regionAlias ? hostSupport?.dataLocale ?? foundLocale : foundLocale;
|
|
2791
|
+
const foundLocaleData = localeData?.[dataLocale];
|
|
2792
|
+
result.dataLocale = dataLocale;
|
|
2793
|
+
if (mode) result.localeData = foundLocaleData;
|
|
2534
2794
|
let components;
|
|
2535
2795
|
let keywords;
|
|
2536
2796
|
if (r.extension) {
|
|
@@ -2538,24 +2798,25 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
|
|
|
2538
2798
|
keywords = components.keywords;
|
|
2539
2799
|
} else keywords = [];
|
|
2540
2800
|
let supportedKeywords = [];
|
|
2541
|
-
for (
|
|
2801
|
+
for (let keyIndex = 0; keyIndex < relevantExtensionKeys.length; keyIndex++) {
|
|
2802
|
+
const key = relevantExtensionKeys[keyIndex];
|
|
2542
2803
|
let keyLocaleData = foundLocaleData?.[key] ?? [];
|
|
2543
|
-
invariant(
|
|
2804
|
+
invariant(localeIsArray(keyLocaleData), `keyLocaleData for ${key} must be an array`);
|
|
2544
2805
|
let value = keyLocaleData[0];
|
|
2545
2806
|
invariant(value == null || typeof value === "string", `value must be a string, null, or undefined`);
|
|
2546
2807
|
let supportedKeyword;
|
|
2547
|
-
let entry = keywords
|
|
2808
|
+
let entry = arrayFind(keywords, (k) => k.key === key);
|
|
2548
2809
|
if (entry) {
|
|
2549
2810
|
let requestedValue = entry.value;
|
|
2550
2811
|
if (requestedValue !== "") {
|
|
2551
|
-
if (keyLocaleData
|
|
2812
|
+
if (arrayIndexOf(keyLocaleData, requestedValue) > -1) {
|
|
2552
2813
|
value = requestedValue;
|
|
2553
2814
|
supportedKeyword = {
|
|
2554
2815
|
key,
|
|
2555
2816
|
value
|
|
2556
2817
|
};
|
|
2557
2818
|
}
|
|
2558
|
-
} else if (keyLocaleData
|
|
2819
|
+
} else if (arrayIndexOf(keyLocaleData, "true") > -1) {
|
|
2559
2820
|
value = "true";
|
|
2560
2821
|
supportedKeyword = {
|
|
2561
2822
|
key,
|
|
@@ -2566,42 +2827,39 @@ function ResolveLocale(availableLocales, requestedLocales, options, relevantExte
|
|
|
2566
2827
|
let optionsValue = options[key];
|
|
2567
2828
|
invariant(optionsValue == null || typeof optionsValue === "string", `optionsValue must be a string or undefined`);
|
|
2568
2829
|
if (typeof optionsValue === "string") {
|
|
2569
|
-
optionsValue = CanonicalizeUValue(key
|
|
2830
|
+
optionsValue = CanonicalizeUValue(localeApply(localeLowerCase, key, []), optionsValue);
|
|
2570
2831
|
if (optionsValue === "") optionsValue = "true";
|
|
2571
2832
|
}
|
|
2572
|
-
if (optionsValue !== void 0 && optionsValue !== value && keyLocaleData
|
|
2833
|
+
if (optionsValue !== void 0 && optionsValue !== value && arrayIndexOf(keyLocaleData, optionsValue) > -1) {
|
|
2573
2834
|
value = optionsValue;
|
|
2574
2835
|
supportedKeyword = void 0;
|
|
2575
2836
|
}
|
|
2576
|
-
if (supportedKeyword) supportedKeywords.
|
|
2837
|
+
if (supportedKeyword) setDataProperty(supportedKeywords, `${supportedKeywords.length}`, supportedKeyword);
|
|
2577
2838
|
result[key] = value;
|
|
2578
2839
|
}
|
|
2579
|
-
|
|
2580
|
-
if (supportedKeywords.length > 0) {
|
|
2581
|
-
supportedAttributes = [];
|
|
2582
|
-
foundLocale = InsertUnicodeExtensionAndCanonicalize(foundLocale, supportedAttributes, supportedKeywords);
|
|
2583
|
-
}
|
|
2840
|
+
if (supportedKeywords.length > 0) foundLocale = InsertUnicodeExtensionAndCanonicalize(foundLocale, [], supportedKeywords);
|
|
2584
2841
|
result.locale = foundLocale;
|
|
2585
2842
|
return result;
|
|
2586
2843
|
}
|
|
2587
2844
|
// packages/intl-localematcher/abstract/LookupSupportedLocales.ts
|
|
2588
|
-
/**
|
|
2589
|
-
* https://tc39.es/ecma402/#sec-lookupsupportedlocales
|
|
2590
|
-
* @param availableLocales
|
|
2591
|
-
* @param requestedLocales
|
|
2592
|
-
*/
|
|
2593
2845
|
function LookupSupportedLocales(availableLocales, requestedLocales) {
|
|
2846
|
+
return FilterLocales(availableLocales, requestedLocales, "lookup");
|
|
2847
|
+
}
|
|
2848
|
+
function FilterLocales(availableLocales, requestedLocales, matcher, includeHostLocale = false) {
|
|
2849
|
+
const bestFit = matcher === "best fit";
|
|
2850
|
+
const available = includeHostLocale ? getServiceLocaleList(availableLocales, bestFit).available : getAvailableLocaleList(availableLocales, bestFit);
|
|
2594
2851
|
const subset = [];
|
|
2595
|
-
for (
|
|
2852
|
+
for (let index = 0; index < requestedLocales.length; index++) {
|
|
2853
|
+
const locale = requestedLocales[index];
|
|
2596
2854
|
const { locale: noExtensionLocale } = splitUnicodeExtension(locale);
|
|
2597
|
-
if (BestAvailableLocale(
|
|
2855
|
+
if (matcher === "lookup" ? BestAvailableLocale(available, noExtensionLocale) : findBestMatch([noExtensionLocale], available, void 0, void 0, false).matchedSupportedLocale ?? (canUseBestFitPrefixFallback(noExtensionLocale) ? BestAvailableLocale(available, noExtensionLocale) : void 0)) setDataProperty(subset, subset.length, locale);
|
|
2598
2856
|
}
|
|
2599
2857
|
return subset;
|
|
2600
2858
|
}
|
|
2601
2859
|
// packages/intl-localematcher/index.ts
|
|
2602
2860
|
function match(requestedLocales, availableLocales, defaultLocale, opts) {
|
|
2603
|
-
return ResolveLocale(availableLocales, CanonicalizeLocaleList(requestedLocales), { localeMatcher: opts?.algorithm || "best fit" }, [],
|
|
2861
|
+
return ResolveLocale(availableLocales, CanonicalizeLocaleList(requestedLocales), { localeMatcher: opts?.algorithm || "best fit" }, [], void 0, () => defaultLocale).locale;
|
|
2604
2862
|
}
|
|
2605
|
-
export { LookupSupportedLocales, ResolveLocale, match };
|
|
2863
|
+
export { FilterLocales, LookupSupportedLocales, ResolveLocale, match };
|
|
2606
2864
|
|
|
2607
2865
|
//# sourceMappingURL=index.js.map
|