@formatjs/intl-getcanonicallocales 3.2.2 → 3.2.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.
@@ -1,19 +0,0 @@
1
- import { type UnicodeLanguageId, type UnicodeLocaleId } from "./types.js";
2
- /**
3
- * CAVEAT: We don't do this section in the spec bc they have no JSON data
4
- * Use the bcp47 data to replace keys, types, tfields, and tvalues by their canonical forms. See Section 3.6.4 U Extension Data Files) and Section 3.7.1 T Extension Data Files. The aliases are in the alias attribute value, while the canonical is in the name attribute value. For example,
5
- Because of the following bcp47 data:
6
- <key name="ms"…>…<type name="uksystem" … alias="imperial" … />…</key>
7
- We get the following transformation:
8
- en-u-ms-imperial ⇒ en-u-ms-uksystem
9
- * @param lang
10
- */
11
- export declare function canonicalizeUnicodeLanguageId(unicodeLanguageId: UnicodeLanguageId): UnicodeLanguageId;
12
- /**
13
- * Canonicalize based on
14
- * https://www.unicode.org/reports/tr35/tr35.html#Canonical_Unicode_Locale_Identifiers
15
- * https://tc39.es/ecma402/#sec-canonicalizeunicodelocaleid
16
- * IMPORTANT: This modifies the object inline
17
- * @param locale
18
- */
19
- export declare function CanonicalizeUnicodeLocaleId(locale: UnicodeLocaleId): UnicodeLocaleId;
@@ -1,210 +0,0 @@
1
- import { languageAlias, scriptAlias, territoryAlias, variantAlias } from "./aliases.generated.js";
2
- import { emitUnicodeLanguageId } from "./emitter.js";
3
- import { likelySubtags } from "./likelySubtags.generated.js";
4
- import { isUnicodeLanguageSubtag, isUnicodeVariantSubtag, parseUnicodeLanguageId, SEPARATOR } from "./parser.js";
5
- import "./types.js";
6
- function canonicalizeAttrs(strs) {
7
- return Object.keys(strs.reduce((all, str) => {
8
- all[str.toLowerCase()] = 1;
9
- return all;
10
- }, {})).sort();
11
- }
12
- function canonicalizeKVs(arr) {
13
- const all = {};
14
- const result = [];
15
- for (const kv of arr) {
16
- if (kv[0] in all) {
17
- continue;
18
- }
19
- all[kv[0]] = 1;
20
- if (!kv[1] || kv[1] === "true") {
21
- result.push([kv[0].toLowerCase()]);
22
- } else {
23
- result.push([kv[0].toLowerCase(), kv[1].toLowerCase()]);
24
- }
25
- }
26
- return result.sort(compareKV);
27
- }
28
- function compareKV(t1, t2) {
29
- return t1[0] < t2[0] ? -1 : t1[0] > t2[0] ? 1 : 0;
30
- }
31
- function compareExtension(e1, e2) {
32
- return e1.type < e2.type ? -1 : e1.type > e2.type ? 1 : 0;
33
- }
34
- function mergeVariants(v1, v2) {
35
- const result = [...v1];
36
- for (const v of v2) {
37
- if (v1.indexOf(v) < 0) {
38
- result.push(v);
39
- }
40
- }
41
- return result;
42
- }
43
- /**
44
- * CAVEAT: We don't do this section in the spec bc they have no JSON data
45
- * Use the bcp47 data to replace keys, types, tfields, and tvalues by their canonical forms. See Section 3.6.4 U Extension Data Files) and Section 3.7.1 T Extension Data Files. The aliases are in the alias attribute value, while the canonical is in the name attribute value. For example,
46
- Because of the following bcp47 data:
47
- <key name="ms"…>…<type name="uksystem" … alias="imperial" … />…</key>
48
- We get the following transformation:
49
- en-u-ms-imperial ⇒ en-u-ms-uksystem
50
- * @param lang
51
- */
52
- export function canonicalizeUnicodeLanguageId(unicodeLanguageId) {
53
- /**
54
- * If the language subtag matches the type attribute of a languageAlias element in Supplemental Data, replace the language subtag with the replacement value.
55
- * 1. If there are additional subtags in the replacement value, add them to the result, but only if there is no corresponding subtag already in the tag.
56
- * 2. Five special deprecated grandfathered codes (such as i-default) are in type attributes, and are also replaced.
57
- */
58
- // From https://github.com/unicode-org/icu/blob/master/icu4j/main/classes/core/src/com/ibm/icu/util/ULocale.java#L1246
59
- // Try language _ variant
60
- let finalLangAst = unicodeLanguageId;
61
- if (unicodeLanguageId.variants.length) {
62
- let replacedLang = "";
63
- for (const variant of unicodeLanguageId.variants) {
64
- if (replacedLang = languageAlias[emitUnicodeLanguageId({
65
- lang: unicodeLanguageId.lang,
66
- variants: [variant]
67
- })]) {
68
- const replacedLangAst = parseUnicodeLanguageId(replacedLang.split(SEPARATOR));
69
- finalLangAst = {
70
- lang: replacedLangAst.lang,
71
- script: finalLangAst.script || replacedLangAst.script,
72
- region: finalLangAst.region || replacedLangAst.region,
73
- variants: mergeVariants(finalLangAst.variants, replacedLangAst.variants)
74
- };
75
- break;
76
- }
77
- }
78
- }
79
- // language _ script _ country
80
- // ug-Arab-CN -> ug-CN
81
- if (finalLangAst.script && finalLangAst.region) {
82
- const replacedLang = languageAlias[emitUnicodeLanguageId({
83
- lang: finalLangAst.lang,
84
- script: finalLangAst.script,
85
- region: finalLangAst.region,
86
- variants: []
87
- })];
88
- if (replacedLang) {
89
- const replacedLangAst = parseUnicodeLanguageId(replacedLang.split(SEPARATOR));
90
- finalLangAst = {
91
- lang: replacedLangAst.lang,
92
- script: replacedLangAst.script,
93
- region: replacedLangAst.region,
94
- variants: finalLangAst.variants
95
- };
96
- }
97
- }
98
- // language _ country
99
- // eg. az_AZ -> az_Latn_A
100
- if (finalLangAst.region) {
101
- const replacedLang = languageAlias[emitUnicodeLanguageId({
102
- lang: finalLangAst.lang,
103
- region: finalLangAst.region,
104
- variants: []
105
- })];
106
- if (replacedLang) {
107
- const replacedLangAst = parseUnicodeLanguageId(replacedLang.split(SEPARATOR));
108
- finalLangAst = {
109
- lang: replacedLangAst.lang,
110
- script: finalLangAst.script || replacedLangAst.script,
111
- region: replacedLangAst.region,
112
- variants: finalLangAst.variants
113
- };
114
- }
115
- }
116
- // only language
117
- // e.g. twi -> ak
118
- const replacedLang = languageAlias[emitUnicodeLanguageId({
119
- lang: finalLangAst.lang,
120
- variants: []
121
- })];
122
- if (replacedLang) {
123
- const replacedLangAst = parseUnicodeLanguageId(replacedLang.split(SEPARATOR));
124
- finalLangAst = {
125
- lang: replacedLangAst.lang,
126
- script: finalLangAst.script || replacedLangAst.script,
127
- region: finalLangAst.region || replacedLangAst.region,
128
- variants: finalLangAst.variants
129
- };
130
- }
131
- if (finalLangAst.region) {
132
- const region = finalLangAst.region.toUpperCase();
133
- const regionAlias = territoryAlias[region];
134
- let replacedRegion;
135
- if (regionAlias) {
136
- const regions = regionAlias.split(" ");
137
- replacedRegion = regions[0];
138
- const likelySubtag = likelySubtags[emitUnicodeLanguageId({
139
- lang: finalLangAst.lang,
140
- script: finalLangAst.script,
141
- variants: []
142
- })];
143
- if (likelySubtag) {
144
- const { region: likelyRegion } = parseUnicodeLanguageId(likelySubtag.split(SEPARATOR));
145
- if (likelyRegion && regions.indexOf(likelyRegion) > -1) {
146
- replacedRegion = likelyRegion;
147
- }
148
- }
149
- }
150
- if (replacedRegion) {
151
- finalLangAst.region = replacedRegion;
152
- }
153
- finalLangAst.region = finalLangAst.region.toUpperCase();
154
- }
155
- if (finalLangAst.script) {
156
- finalLangAst.script = finalLangAst.script[0].toUpperCase() + finalLangAst.script.slice(1).toLowerCase();
157
- if (scriptAlias[finalLangAst.script]) {
158
- finalLangAst.script = scriptAlias[finalLangAst.script];
159
- }
160
- }
161
- if (finalLangAst.variants.length) {
162
- for (let i = 0; i < finalLangAst.variants.length; i++) {
163
- let variant = finalLangAst.variants[i].toLowerCase();
164
- if (variantAlias[variant]) {
165
- const alias = variantAlias[variant];
166
- if (isUnicodeVariantSubtag(alias)) {
167
- finalLangAst.variants[i] = alias;
168
- } else if (isUnicodeLanguageSubtag(alias)) {
169
- // Yes this can happen per the spec
170
- finalLangAst.lang = alias;
171
- }
172
- }
173
- }
174
- finalLangAst.variants.sort();
175
- }
176
- return finalLangAst;
177
- }
178
- /**
179
- * Canonicalize based on
180
- * https://www.unicode.org/reports/tr35/tr35.html#Canonical_Unicode_Locale_Identifiers
181
- * https://tc39.es/ecma402/#sec-canonicalizeunicodelocaleid
182
- * IMPORTANT: This modifies the object inline
183
- * @param locale
184
- */
185
- export function CanonicalizeUnicodeLocaleId(locale) {
186
- locale.lang = canonicalizeUnicodeLanguageId(locale.lang);
187
- if (locale.extensions) {
188
- for (const extension of locale.extensions) {
189
- switch (extension.type) {
190
- case "u":
191
- extension.keywords = canonicalizeKVs(extension.keywords);
192
- if (extension.attributes) {
193
- extension.attributes = canonicalizeAttrs(extension.attributes);
194
- }
195
- break;
196
- case "t":
197
- if (extension.lang) {
198
- extension.lang = canonicalizeUnicodeLanguageId(extension.lang);
199
- }
200
- extension.fields = canonicalizeKVs(extension.fields);
201
- break;
202
- default:
203
- extension.value = extension.value.toLowerCase();
204
- break;
205
- }
206
- }
207
- locale.extensions.sort(compareExtension);
208
- }
209
- return locale;
210
- }
package/src/emitter.d.ts DELETED
@@ -1,3 +0,0 @@
1
- import { type UnicodeLanguageId, type UnicodeLocaleId } from "./types.js";
2
- export declare function emitUnicodeLanguageId(lang?: UnicodeLanguageId): string;
3
- export declare function emitUnicodeLocaleId({ lang, extensions }: UnicodeLocaleId): string;
package/src/emitter.js DELETED
@@ -1,30 +0,0 @@
1
- import "./types.js";
2
- export function emitUnicodeLanguageId(lang) {
3
- if (!lang) {
4
- return "";
5
- }
6
- return [
7
- lang.lang,
8
- lang.script,
9
- lang.region,
10
- ...lang.variants || []
11
- ].filter(Boolean).join("-");
12
- }
13
- export function emitUnicodeLocaleId({ lang, extensions }) {
14
- const chunks = [emitUnicodeLanguageId(lang)];
15
- for (const ext of extensions) {
16
- chunks.push(ext.type);
17
- switch (ext.type) {
18
- case "u":
19
- chunks.push(...ext.attributes, ...ext.keywords.reduce((all, kv) => all.concat(kv), []));
20
- break;
21
- case "t":
22
- chunks.push(emitUnicodeLanguageId(ext.lang), ...ext.fields.reduce((all, kv) => all.concat(kv), []));
23
- break;
24
- default:
25
- chunks.push(ext.value);
26
- break;
27
- }
28
- }
29
- return chunks.filter(Boolean).join("-");
30
- }
@@ -1 +0,0 @@
1
- export declare const likelySubtags: Record<string, string>;