@formatjs/intl-displaynames 7.1.0 → 7.1.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/abstract/CanonicalCodeForDisplayNames.d.ts +1 -1
- package/abstract/CanonicalCodeForDisplayNames.js +40 -40
- package/abstract/IsValidDateTimeFieldCode.js +14 -14
- package/index.d.ts +21 -21
- package/index.js +184 -198
- package/package.json +5 -5
- package/polyfill-force.js +6 -6
- package/polyfill.iife.js +2611 -2945
- package/polyfill.js +8 -8
- package/should-polyfill.js +30 -37
- package/supported-locales.generated.js +573 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function CanonicalCodeForDisplayNames(type:
|
|
1
|
+
export declare function CanonicalCodeForDisplayNames(type: "language" | "region" | "script" | "calendar" | "dateTimeField" | "currency", code: string): string;
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import { CanonicalizeLocaleList, invariant, IsWellFormedCurrencyCode
|
|
2
|
-
import { IsValidDateTimeFieldCode } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { CanonicalizeLocaleList, invariant, IsWellFormedCurrencyCode } from "@formatjs/ecma402-abstract";
|
|
2
|
+
import { IsValidDateTimeFieldCode } from "./IsValidDateTimeFieldCode";
|
|
3
|
+
const UNICODE_REGION_SUBTAG_REGEX = /^([a-z]{2}|[0-9]{3})$/i;
|
|
4
|
+
const ALPHA_4 = /^[a-z]{4}$/i;
|
|
5
|
+
const UNICODE_TYPE_REGEX = /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i;
|
|
6
6
|
function isUnicodeRegionSubtag(region) {
|
|
7
|
-
|
|
7
|
+
return UNICODE_REGION_SUBTAG_REGEX.test(region);
|
|
8
8
|
}
|
|
9
9
|
function isUnicodeScriptSubtag(script) {
|
|
10
|
-
|
|
10
|
+
return ALPHA_4.test(script);
|
|
11
11
|
}
|
|
12
12
|
function isUnicodeLocaleIdentifierType(code) {
|
|
13
|
-
|
|
13
|
+
return UNICODE_TYPE_REGEX.test(code);
|
|
14
14
|
}
|
|
15
15
|
export function CanonicalCodeForDisplayNames(type, code) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
16
|
+
if (type === "language") {
|
|
17
|
+
return CanonicalizeLocaleList([code])[0];
|
|
18
|
+
}
|
|
19
|
+
if (type === "region") {
|
|
20
|
+
if (!isUnicodeRegionSubtag(code)) {
|
|
21
|
+
throw RangeError("invalid region");
|
|
22
|
+
}
|
|
23
|
+
return code.toUpperCase();
|
|
24
|
+
}
|
|
25
|
+
if (type === "script") {
|
|
26
|
+
if (!isUnicodeScriptSubtag(code)) {
|
|
27
|
+
throw RangeError("invalid script");
|
|
28
|
+
}
|
|
29
|
+
return `${code[0].toUpperCase()}${code.slice(1).toLowerCase()}`;
|
|
30
|
+
}
|
|
31
|
+
if (type === "calendar") {
|
|
32
|
+
if (!isUnicodeLocaleIdentifierType(code)) {
|
|
33
|
+
throw RangeError("invalid calendar");
|
|
34
|
+
}
|
|
35
|
+
return code.toLowerCase();
|
|
36
|
+
}
|
|
37
|
+
if (type === "dateTimeField") {
|
|
38
|
+
if (!IsValidDateTimeFieldCode(code)) {
|
|
39
|
+
throw RangeError("invalid dateTimeField");
|
|
40
|
+
}
|
|
41
|
+
return code;
|
|
42
|
+
}
|
|
43
|
+
invariant(type === "currency", "invalid type");
|
|
44
|
+
if (!IsWellFormedCurrencyCode(code)) {
|
|
45
|
+
throw RangeError("invalid currency");
|
|
46
|
+
}
|
|
47
|
+
return code.toUpperCase();
|
|
48
48
|
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
1
|
+
const CODES_FOR_DATE_TIME_FIELD = [
|
|
2
|
+
"era",
|
|
3
|
+
"year",
|
|
4
|
+
"quarter",
|
|
5
|
+
"month",
|
|
6
|
+
"weekOfYear",
|
|
7
|
+
"weekday",
|
|
8
|
+
"day",
|
|
9
|
+
"dayPeriod",
|
|
10
|
+
"hour",
|
|
11
|
+
"minute",
|
|
12
|
+
"second",
|
|
13
|
+
"timeZoneName"
|
|
14
14
|
];
|
|
15
15
|
export function IsValidDateTimeFieldCode(field) {
|
|
16
|
-
|
|
16
|
+
return CODES_FOR_DATE_TIME_FIELD.indexOf(field) >= 0;
|
|
17
17
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { DisplayNamesData, DisplayNamesLocaleData } from
|
|
1
|
+
import { type DisplayNamesData, type DisplayNamesLocaleData } from "@formatjs/ecma402-abstract";
|
|
2
2
|
export interface DisplayNamesOptions {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
localeMatcher?: "lookup" | "best fit";
|
|
4
|
+
style?: "narrow" | "short" | "long";
|
|
5
|
+
type: "language" | "region" | "script" | "currency" | "calendar" | "dateTimeField";
|
|
6
|
+
fallback?: "code" | "none";
|
|
7
|
+
languageDisplay?: "dialect" | "standard";
|
|
8
8
|
}
|
|
9
9
|
export interface DisplayNamesResolvedOptions {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
locale: string;
|
|
11
|
+
style: NonNullable<DisplayNamesOptions["style"]>;
|
|
12
|
+
type: NonNullable<DisplayNamesOptions["type"]>;
|
|
13
|
+
fallback: NonNullable<DisplayNamesOptions["fallback"]>;
|
|
14
|
+
languageDisplay: NonNullable<DisplayNamesOptions["languageDisplay"]>;
|
|
15
15
|
}
|
|
16
16
|
export declare class DisplayNames {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
17
|
+
constructor(locales: string | string[] | undefined, options: DisplayNamesOptions);
|
|
18
|
+
static supportedLocalesOf(locales?: string | string[], options?: Pick<DisplayNamesOptions, "localeMatcher">): string[];
|
|
19
|
+
static __addLocaleData(...data: DisplayNamesLocaleData[]): void;
|
|
20
|
+
of(code: string | number | Record<string, unknown>): string | undefined;
|
|
21
|
+
resolvedOptions(): DisplayNamesResolvedOptions;
|
|
22
|
+
static localeData: Record<string, DisplayNamesData | undefined>;
|
|
23
|
+
private static availableLocales;
|
|
24
|
+
private static __defaultLocale;
|
|
25
|
+
private static getDefaultLocale;
|
|
26
|
+
static readonly polyfilled: true;
|
|
27
27
|
}
|
package/index.js
CHANGED
|
@@ -1,210 +1,196 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
DisplayNames.polyfilled = true;
|
|
112
|
-
return DisplayNames;
|
|
113
|
-
}());
|
|
114
|
-
export { DisplayNames };
|
|
1
|
+
import { CanonicalizeLocaleList, GetOption, GetOptionsObject, IsWellFormedCurrencyCode, SupportedLocales, ToString, getInternalSlot, getMultiInternalSlots, invariant, setInternalSlot } from "@formatjs/ecma402-abstract";
|
|
2
|
+
import { CanonicalCodeForDisplayNames } from "./abstract/CanonicalCodeForDisplayNames.js";
|
|
3
|
+
import { IsValidDateTimeFieldCode } from "./abstract/IsValidDateTimeFieldCode.js";
|
|
4
|
+
import { ResolveLocale } from "@formatjs/intl-localematcher";
|
|
5
|
+
export class DisplayNames {
|
|
6
|
+
constructor(locales, options) {
|
|
7
|
+
if (new.target === undefined) {
|
|
8
|
+
throw TypeError(`Constructor Intl.DisplayNames requires 'new'`);
|
|
9
|
+
}
|
|
10
|
+
const requestedLocales = CanonicalizeLocaleList(locales);
|
|
11
|
+
options = GetOptionsObject(options);
|
|
12
|
+
const opt = Object.create(null);
|
|
13
|
+
const { localeData } = DisplayNames;
|
|
14
|
+
const matcher = GetOption(options, "localeMatcher", "string", ["lookup", "best fit"], "best fit");
|
|
15
|
+
opt.localeMatcher = matcher;
|
|
16
|
+
const r = ResolveLocale(Array.from(DisplayNames.availableLocales), requestedLocales, opt, [], DisplayNames.localeData, DisplayNames.getDefaultLocale);
|
|
17
|
+
const style = GetOption(options, "style", "string", [
|
|
18
|
+
"narrow",
|
|
19
|
+
"short",
|
|
20
|
+
"long"
|
|
21
|
+
], "long");
|
|
22
|
+
setSlot(this, "style", style);
|
|
23
|
+
const type = GetOption(options, "type", "string", [
|
|
24
|
+
"language",
|
|
25
|
+
"region",
|
|
26
|
+
"script",
|
|
27
|
+
"currency",
|
|
28
|
+
"calendar",
|
|
29
|
+
"dateTimeField"
|
|
30
|
+
], undefined);
|
|
31
|
+
if (type === undefined) {
|
|
32
|
+
throw TypeError(`Intl.DisplayNames constructor requires "type" option`);
|
|
33
|
+
}
|
|
34
|
+
setSlot(this, "type", type);
|
|
35
|
+
const fallback = GetOption(options, "fallback", "string", ["code", "none"], "code");
|
|
36
|
+
setSlot(this, "fallback", fallback);
|
|
37
|
+
setSlot(this, "locale", r.locale);
|
|
38
|
+
const { dataLocale } = r;
|
|
39
|
+
const dataLocaleData = localeData[dataLocale];
|
|
40
|
+
invariant(!!dataLocaleData, `Missing locale data for ${dataLocale}`);
|
|
41
|
+
setSlot(this, "localeData", dataLocaleData);
|
|
42
|
+
invariant(dataLocaleData !== undefined, `locale data for ${r.locale} does not exist.`);
|
|
43
|
+
const { types } = dataLocaleData;
|
|
44
|
+
invariant(typeof types === "object" && types != null, "invalid types data");
|
|
45
|
+
const typeFields = types[type];
|
|
46
|
+
invariant(typeof typeFields === "object" && typeFields != null, "invalid typeFields data");
|
|
47
|
+
const languageDisplay = GetOption(options, "languageDisplay", "string", ["dialect", "standard"], "dialect");
|
|
48
|
+
if (type === "language") {
|
|
49
|
+
setSlot(this, "languageDisplay", languageDisplay);
|
|
50
|
+
// Using types[type] instead of typeFields because TypeScript cannot infer the correct type
|
|
51
|
+
const typeFields = types[type][languageDisplay];
|
|
52
|
+
invariant(typeof typeFields === "object" && typeFields != null, "invalid language typeFields data");
|
|
53
|
+
}
|
|
54
|
+
// Using types[type] instead of typeFields because TypeScript cannot infer the correct type
|
|
55
|
+
const styleFields = type === "language" ? types[type][languageDisplay][style] : types[type][style];
|
|
56
|
+
invariant(typeof styleFields === "object" && styleFields != null, "invalid styleFields data");
|
|
57
|
+
setSlot(this, "fields", styleFields);
|
|
58
|
+
}
|
|
59
|
+
static supportedLocalesOf(locales, options) {
|
|
60
|
+
return SupportedLocales(DisplayNames.availableLocales, CanonicalizeLocaleList(locales), options);
|
|
61
|
+
}
|
|
62
|
+
static __addLocaleData(...data) {
|
|
63
|
+
for (const { data: d, locale } of data) {
|
|
64
|
+
const minimizedLocale = new Intl.Locale(locale).minimize().toString();
|
|
65
|
+
DisplayNames.localeData[locale] = DisplayNames.localeData[minimizedLocale] = d;
|
|
66
|
+
DisplayNames.availableLocales.add(minimizedLocale);
|
|
67
|
+
DisplayNames.availableLocales.add(locale);
|
|
68
|
+
if (!DisplayNames.__defaultLocale) {
|
|
69
|
+
DisplayNames.__defaultLocale = minimizedLocale;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
of(code) {
|
|
74
|
+
checkReceiver(this, "of");
|
|
75
|
+
const type = getSlot(this, "type");
|
|
76
|
+
const codeAsString = ToString(code);
|
|
77
|
+
if (!isValidCodeForDisplayNames(type, codeAsString)) {
|
|
78
|
+
throw RangeError("invalid code for Intl.DisplayNames.prototype.of");
|
|
79
|
+
}
|
|
80
|
+
const { localeData, style, fallback } = getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, "localeData", "style", "fallback");
|
|
81
|
+
// Canonicalize the case.
|
|
82
|
+
let canonicalCode = CanonicalCodeForDisplayNames(type, codeAsString);
|
|
83
|
+
let name;
|
|
84
|
+
if (type === "language") {
|
|
85
|
+
const languageDisplay = getSlot(this, "languageDisplay");
|
|
86
|
+
name = getNameForTypeLanguage(languageDisplay, localeData, style, canonicalCode, fallback);
|
|
87
|
+
} else {
|
|
88
|
+
// All the other types
|
|
89
|
+
const typesData = localeData.types[type];
|
|
90
|
+
name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
|
|
91
|
+
}
|
|
92
|
+
if (name !== undefined) {
|
|
93
|
+
return name;
|
|
94
|
+
}
|
|
95
|
+
if (fallback === "code") {
|
|
96
|
+
return codeAsString;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
resolvedOptions() {
|
|
100
|
+
checkReceiver(this, "resolvedOptions");
|
|
101
|
+
return { ...getMultiInternalSlots(__INTERNAL_SLOT_MAP__, this, "locale", "style", "type", "fallback", "languageDisplay") };
|
|
102
|
+
}
|
|
103
|
+
static localeData = {};
|
|
104
|
+
static availableLocales = new Set();
|
|
105
|
+
static __defaultLocale = "";
|
|
106
|
+
static getDefaultLocale() {
|
|
107
|
+
return DisplayNames.__defaultLocale;
|
|
108
|
+
}
|
|
109
|
+
static polyfilled = true;
|
|
110
|
+
}
|
|
115
111
|
// https://tc39.es/proposal-intl-displaynames/#sec-isvalidcodefordisplaynames
|
|
116
112
|
function isValidCodeForDisplayNames(type, code) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
return IsValidDateTimeFieldCode(code);
|
|
139
|
-
}
|
|
113
|
+
switch (type) {
|
|
114
|
+
case "language":
|
|
115
|
+
// subset of unicode_language_id
|
|
116
|
+
// languageCode ["-" scriptCode] ["-" regionCode] *("-" variant)
|
|
117
|
+
// where:
|
|
118
|
+
// - languageCode is either a two letters ISO 639-1 language code or a three letters ISO 639-2 language code.
|
|
119
|
+
// - scriptCode is should be an ISO-15924 four letters script code
|
|
120
|
+
// - regionCode is either an ISO-3166 two letters region code, or a three digits UN M49 Geographic Regions.
|
|
121
|
+
return /^[a-z]{2,3}(-[a-z]{4})?(-([a-z]{2}|\d{3}))?(-([a-z\d]{5,8}|\d[a-z\d]{3}))*$/i.test(code);
|
|
122
|
+
case "region":
|
|
123
|
+
// unicode_region_subtag
|
|
124
|
+
return /^([a-z]{2}|\d{3})$/i.test(code);
|
|
125
|
+
case "script":
|
|
126
|
+
// unicode_script_subtag
|
|
127
|
+
return /^[a-z]{4}$/i.test(code);
|
|
128
|
+
case "currency": return IsWellFormedCurrencyCode(code);
|
|
129
|
+
case "calendar":
|
|
130
|
+
// unicode locale identifier type
|
|
131
|
+
return /^[a-z0-9]{3,8}([-_][a-z0-9]{3,8})*$/i.test(code);
|
|
132
|
+
case "dateTimeField": return IsValidDateTimeFieldCode(code);
|
|
133
|
+
}
|
|
140
134
|
}
|
|
141
135
|
try {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// Make test 262 compliant
|
|
160
|
-
}
|
|
161
|
-
var __INTERNAL_SLOT_MAP__ = new WeakMap();
|
|
136
|
+
// IE11 does not have Symbol
|
|
137
|
+
if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
|
|
138
|
+
Object.defineProperty(DisplayNames.prototype, Symbol.toStringTag, {
|
|
139
|
+
value: "Intl.DisplayNames",
|
|
140
|
+
configurable: true,
|
|
141
|
+
enumerable: false,
|
|
142
|
+
writable: false
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
Object.defineProperty(DisplayNames, "length", {
|
|
146
|
+
value: 2,
|
|
147
|
+
writable: false,
|
|
148
|
+
enumerable: false,
|
|
149
|
+
configurable: true
|
|
150
|
+
});
|
|
151
|
+
} catch {}
|
|
152
|
+
const __INTERNAL_SLOT_MAP__ = new WeakMap();
|
|
162
153
|
function getSlot(instance, key) {
|
|
163
|
-
|
|
154
|
+
return getInternalSlot(__INTERNAL_SLOT_MAP__, instance, key);
|
|
164
155
|
}
|
|
165
156
|
function setSlot(instance, key, value) {
|
|
166
|
-
|
|
157
|
+
setInternalSlot(__INTERNAL_SLOT_MAP__, instance, key, value);
|
|
167
158
|
}
|
|
168
159
|
function checkReceiver(receiver, methodName) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
160
|
+
if (!(receiver instanceof DisplayNames)) {
|
|
161
|
+
throw TypeError(`Method Intl.DisplayNames.prototype.${methodName} called on incompatible receiver`);
|
|
162
|
+
}
|
|
172
163
|
}
|
|
173
164
|
function getNameForTypeLanguage(languageDisplay, localeData, style, canonicalCode, fallback) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
return name;
|
|
209
|
-
}
|
|
165
|
+
// First, try to get the name using the canonicalCode
|
|
166
|
+
const typesData = localeData.types.language[languageDisplay];
|
|
167
|
+
const name = typesData[style][canonicalCode] || typesData.long[canonicalCode];
|
|
168
|
+
if (name === undefined) {
|
|
169
|
+
// If no name has been found using the canonicalCode,
|
|
170
|
+
// check if the latter contains a region sub tag
|
|
171
|
+
const regionMatch = /-([a-z]{2}|\d{3})\b/i.exec(canonicalCode);
|
|
172
|
+
if (regionMatch) {
|
|
173
|
+
// Extract the language and region sub tags
|
|
174
|
+
const languageSubTag = canonicalCode.substring(0, regionMatch.index) + canonicalCode.substring(regionMatch.index + regionMatch[0].length);
|
|
175
|
+
const regionSubTag = regionMatch[1];
|
|
176
|
+
// Let's try again using languageSubTag this time
|
|
177
|
+
const name = typesData[style][languageSubTag] || typesData.long[languageSubTag];
|
|
178
|
+
// If a name has been found and a region sub tag exists,
|
|
179
|
+
// compose them together or use the code fallback
|
|
180
|
+
if (name !== undefined && regionSubTag) {
|
|
181
|
+
// Retrieve region display names
|
|
182
|
+
const regionsData = localeData.types.region;
|
|
183
|
+
const regionDisplayName = regionsData[style][regionSubTag] || regionsData.long[regionSubTag];
|
|
184
|
+
if (regionDisplayName || fallback === "code") {
|
|
185
|
+
// Interpolate into locale-specific pattern.
|
|
186
|
+
const pattern = localeData.patterns.locale;
|
|
187
|
+
return pattern.replace("{0}", name).replace("{1}", regionDisplayName || regionSubTag);
|
|
188
|
+
}
|
|
189
|
+
} else {
|
|
190
|
+
return name;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
return name;
|
|
195
|
+
}
|
|
210
196
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/intl-displaynames",
|
|
3
3
|
"description": "Polyfill for: https://tc39.es/proposal-intl-displaynames",
|
|
4
|
-
"version": "7.1.
|
|
4
|
+
"version": "7.1.2",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Linjie Ding <pyrocat101@users.noreply.github.com>",
|
|
7
7
|
"type": "module",
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"tslib": "^2.8.0",
|
|
18
|
-
"@formatjs/ecma402-abstract": "3.0.
|
|
19
|
-
"@formatjs/intl-localematcher": "0.7.
|
|
18
|
+
"@formatjs/ecma402-abstract": "3.0.8",
|
|
19
|
+
"@formatjs/intl-localematcher": "0.7.5"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@formatjs/intl-
|
|
23
|
-
"@formatjs/intl-
|
|
22
|
+
"@formatjs/intl-getcanonicallocales": "3.1.2",
|
|
23
|
+
"@formatjs/intl-locale": "5.1.2"
|
|
24
24
|
},
|
|
25
25
|
"bugs": "https://github.com/formatjs/formatjs/issues",
|
|
26
26
|
"homepage": "https://github.com/formatjs/formatjs",
|
package/polyfill-force.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DisplayNames } from
|
|
2
|
-
Object.defineProperty(Intl,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { DisplayNames } from "./index.js";
|
|
2
|
+
Object.defineProperty(Intl, "DisplayNames", {
|
|
3
|
+
value: DisplayNames,
|
|
4
|
+
enumerable: false,
|
|
5
|
+
writable: true,
|
|
6
|
+
configurable: true
|
|
7
7
|
});
|