@intlify/core 12.0.0-alpha.4 → 12.0.0-alpha.6
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/dist/core.esm-browser.js +473 -377
- package/dist/core.esm-browser.prod.js +640 -568
- package/dist/core.global.js +475 -377
- package/dist/core.global.prod.js +659 -579
- package/dist/core.js +1 -1
- package/dist/core.node.js +1 -1
- package/dist/core.runtime.esm-browser.js +448 -363
- package/dist/core.runtime.esm-browser.prod.js +504 -433
- package/dist/core.runtime.global.js +450 -363
- package/dist/core.runtime.global.prod.js +503 -429
- package/dist/core.runtime.js +1 -5
- package/dist/core.runtime.node.js +1 -5
- package/package.json +4 -4
package/dist/core.esm-browser.js
CHANGED
|
@@ -1,24 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @intlify/core v12.0.0-alpha.
|
|
2
|
+
* @intlify/core v12.0.0-alpha.6
|
|
3
3
|
* (c) 2016-present kazuya kawaguchi and contributors
|
|
4
4
|
* @license MIT
|
|
5
5
|
**/
|
|
6
|
-
//#region packages/shared/src/warn.ts
|
|
7
|
-
function warn(msg, err) {
|
|
8
|
-
if (typeof console !== "undefined") {
|
|
9
|
-
console.warn(`[intlify] ` + msg);
|
|
10
|
-
/* istanbul ignore if */
|
|
11
|
-
if (err) console.warn(err.stack);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
const hasWarned = {};
|
|
15
|
-
function warnOnce(msg) {
|
|
16
|
-
if (!hasWarned[msg]) {
|
|
17
|
-
hasWarned[msg] = true;
|
|
18
|
-
warn(msg);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
//#endregion
|
|
22
6
|
//#region packages/shared/src/utils.ts
|
|
23
7
|
/**
|
|
24
8
|
* Original Utilities
|
|
@@ -37,6 +21,7 @@ let measure;
|
|
|
37
21
|
perf.measure(name, startTag, endTag);
|
|
38
22
|
perf.clearMarks(startTag);
|
|
39
23
|
perf.clearMarks(endTag);
|
|
24
|
+
perf.clearMeasures(name);
|
|
40
25
|
};
|
|
41
26
|
}
|
|
42
27
|
}
|
|
@@ -55,75 +40,36 @@ const generateFormatCacheKey = (locale, key, source) => friendlyJSONstringify({
|
|
|
55
40
|
});
|
|
56
41
|
const friendlyJSONstringify = (json) => JSON.stringify(json).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/\u0027/g, "\\u0027");
|
|
57
42
|
const isNumber = (val) => typeof val === "number" && isFinite(val);
|
|
43
|
+
function toDevtoolsGroupId(type, key) {
|
|
44
|
+
return isString(key) || isNumber(key) ? `${type}:${key}` : type;
|
|
45
|
+
}
|
|
58
46
|
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
59
47
|
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
60
48
|
const isKeylessObject = (val) => Object.keys(val).length === 0;
|
|
61
49
|
const assign = Object.assign;
|
|
62
50
|
const _create = Object.create;
|
|
63
51
|
const create = (obj = null) => _create(obj);
|
|
64
|
-
function escapeHtml(rawText) {
|
|
65
|
-
return rawText.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/").replace(/=/g, "=");
|
|
66
|
-
}
|
|
67
|
-
function escapeAttributeValue(value) {
|
|
68
|
-
return value.replace(/&(?![a-z0-9#]{2,6};)/gi, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
69
|
-
}
|
|
70
|
-
const javascriptSchemePattern = /^\s*javascript\s*(?::|�*58;?|�*3a;?|:?)/i;
|
|
71
|
-
const urlAttributePattern = /^(?:href|src|action|formaction)$/i;
|
|
72
|
-
function hasJavascriptScheme(value) {
|
|
73
|
-
return javascriptSchemePattern.test(value);
|
|
74
|
-
}
|
|
75
|
-
function sanitizeStyleValue(value) {
|
|
76
|
-
const urlPattern = /url\s*\(/gi;
|
|
77
|
-
let sanitized = "";
|
|
78
|
-
let cursor = 0;
|
|
79
|
-
let match;
|
|
80
|
-
while ((match = urlPattern.exec(value)) !== null) {
|
|
81
|
-
const urlStart = match.index;
|
|
82
|
-
const openParenIndex = urlPattern.lastIndex - 1;
|
|
83
|
-
let index = openParenIndex + 1;
|
|
84
|
-
let depth = 1;
|
|
85
|
-
let quote = null;
|
|
86
|
-
for (; index < value.length; index++) {
|
|
87
|
-
const char = value[index];
|
|
88
|
-
if (quote) {
|
|
89
|
-
if (char === quote) quote = null;
|
|
90
|
-
continue;
|
|
91
|
-
}
|
|
92
|
-
if (char === "\"" || char === "'") quote = char;
|
|
93
|
-
else if (char === "(") depth++;
|
|
94
|
-
else if (char === ")") {
|
|
95
|
-
depth--;
|
|
96
|
-
if (depth === 0) break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
if (depth !== 0) break;
|
|
100
|
-
const rawUrlValue = value.slice(openParenIndex + 1, index).trim();
|
|
101
|
-
const unquotedUrlValue = rawUrlValue.startsWith("\"") && rawUrlValue.endsWith("\"") || rawUrlValue.startsWith("'") && rawUrlValue.endsWith("'") ? rawUrlValue.slice(1, -1).trim() : rawUrlValue;
|
|
102
|
-
sanitized += value.slice(cursor, urlStart);
|
|
103
|
-
sanitized += hasJavascriptScheme(unquotedUrlValue) ? "url(about:blank)" : value.slice(urlStart, index + 1);
|
|
104
|
-
cursor = index + 1;
|
|
105
|
-
}
|
|
106
|
-
return sanitized + value.slice(cursor);
|
|
107
|
-
}
|
|
108
|
-
function sanitizeAttributeValue(attrName, value) {
|
|
109
|
-
if (urlAttributePattern.test(attrName) && hasJavascriptScheme(value)) return "about:blank";
|
|
110
|
-
return escapeAttributeValue(attrName.toLowerCase() === "style" ? sanitizeStyleValue(value) : value);
|
|
111
|
-
}
|
|
112
|
-
function sanitizeTranslatedHtml(html) {
|
|
113
|
-
html = html.replace(/([\w:-]+)\s*=\s*"([^"]*)"/g, (_, attrName, attrValue) => `${attrName}="${sanitizeAttributeValue(attrName, attrValue)}"`);
|
|
114
|
-
html = html.replace(/([\w:-]+)\s*=\s*'([^']*)'/g, (_, attrName, attrValue) => `${attrName}='${sanitizeAttributeValue(attrName, attrValue)}'`);
|
|
115
|
-
if (/\s*on\w+\s*=\s*["']?[^"'>]+["']?/i.test(html)) {
|
|
116
|
-
warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
|
|
117
|
-
html = html.replace(/(\s+)on(\w+\s*=)/gi, "$1on$2");
|
|
118
|
-
}
|
|
119
|
-
html = html.replace(/(\s+(?:href|src|action|formaction)\s*=\s*)([^\s"'=<>`]+)/gi, (match, prefix, attrValue) => hasJavascriptScheme(attrValue) ? `${prefix}about:blank` : match);
|
|
120
|
-
return html;
|
|
121
|
-
}
|
|
122
52
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
123
53
|
function hasOwn(obj, key) {
|
|
124
54
|
return hasOwnProperty.call(obj, key);
|
|
125
55
|
}
|
|
126
56
|
/**
|
|
57
|
+
* Get `obj[key]` only when `key` is an own property of `obj`, so that a key such as `constructor`
|
|
58
|
+
* does not read a property inherited from `Object.prototype`.
|
|
59
|
+
* The property is read before the check, so that a reactive `obj` tracks `key` even when it is missing.
|
|
60
|
+
*/
|
|
61
|
+
function getOwn(obj, key) {
|
|
62
|
+
const value = obj[key];
|
|
63
|
+
return hasOwn(obj, key) ? value : void 0;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Whether `key` is a property name of `Object.prototype`, such as `__proto__`, `constructor` or `toString`.
|
|
67
|
+
* On a plain object, such a key reads a built-in, and assigning to `__proto__` replaces the prototype.
|
|
68
|
+
*/
|
|
69
|
+
function isObjectPrototypeKey(key) {
|
|
70
|
+
return hasOwn(Object.prototype, key);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
127
73
|
* Useful Utilities By Evan you
|
|
128
74
|
* Modified by kazuya kawaguchi
|
|
129
75
|
* MIT License
|
|
@@ -178,25 +124,130 @@ function generateCodeFrame(source, start = 0, end = source.length) {
|
|
|
178
124
|
return res.join("\n");
|
|
179
125
|
}
|
|
180
126
|
//#endregion
|
|
127
|
+
//#region packages/shared/src/warn.ts
|
|
128
|
+
function warn(msg, err) {
|
|
129
|
+
if (typeof console !== "undefined") {
|
|
130
|
+
console.warn(`[intlify] ` + msg);
|
|
131
|
+
/* istanbul ignore if */
|
|
132
|
+
if (err) console.warn(err.stack);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const hasWarned = {};
|
|
136
|
+
function warnOnce(msg) {
|
|
137
|
+
if (!hasWarned[msg]) {
|
|
138
|
+
hasWarned[msg] = true;
|
|
139
|
+
warn(msg);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region packages/shared/src/sanitize.ts
|
|
144
|
+
function escapeHtml(rawText) {
|
|
145
|
+
return rawText.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/").replace(/=/g, "=");
|
|
146
|
+
}
|
|
147
|
+
function escapeAttributeValue(value) {
|
|
148
|
+
return value.replace(/&(?![a-z0-9#]{2,6};)/gi, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
149
|
+
}
|
|
150
|
+
const javascriptSchemePattern = /^javascript:/i;
|
|
151
|
+
const urlAttributePattern = /^(?:href|src|action|formaction)$/i;
|
|
152
|
+
const numericCharacterReferencePattern = /&#(?:x([0-9a-f]+)|(\d+));?/gi;
|
|
153
|
+
const namedWhitespaceCharacterReferencePattern = /&(?:Tab|NewLine);/g;
|
|
154
|
+
const colonCharacterReferencePattern = /:?/gi;
|
|
155
|
+
const controlOrWhitespacePattern = /[\u0000-\u0020\u007f-\u009f]/g;
|
|
156
|
+
const eventHandlerPattern = /(?:^|[\s"'<>/])on\w+\s*=\s*["']?[^"'>]+["']?/i;
|
|
157
|
+
const eventHandlerAttributePattern = /(^|[\s"'<>/])on(\w+\s*=)/gi;
|
|
158
|
+
const srcdocPattern = /(?:^|[\s"'<>/])srcdoc\s*=/i;
|
|
159
|
+
const srcdocAttributePattern = /(^|[\s"'<>/])s(rcdoc\s*=)/gi;
|
|
160
|
+
const unquotedUrlAttributePattern = /(^|[\s"'<>/])((?:href|src|action|formaction)\s*=\s*)([^\s"'=<>`]+)/gi;
|
|
161
|
+
function decodeNumericCharacterReference(match, hex, decimal) {
|
|
162
|
+
const digits = hex || decimal;
|
|
163
|
+
if (!digits) return match;
|
|
164
|
+
const codePoint = Number.parseInt(digits, hex ? 16 : 10);
|
|
165
|
+
return codePoint <= 127 ? String.fromCharCode(codePoint) : match;
|
|
166
|
+
}
|
|
167
|
+
function hasJavascriptScheme(value) {
|
|
168
|
+
const normalized = value.replace(numericCharacterReferencePattern, decodeNumericCharacterReference).replace(namedWhitespaceCharacterReferencePattern, "").replace(colonCharacterReferencePattern, ":").replace(controlOrWhitespacePattern, "");
|
|
169
|
+
return javascriptSchemePattern.test(normalized);
|
|
170
|
+
}
|
|
171
|
+
function sanitizeStyleValue(value) {
|
|
172
|
+
const urlPattern = /url\s*\(/gi;
|
|
173
|
+
let sanitized = "";
|
|
174
|
+
let cursor = 0;
|
|
175
|
+
let match;
|
|
176
|
+
while ((match = urlPattern.exec(value)) !== null) {
|
|
177
|
+
const urlStart = match.index;
|
|
178
|
+
const openParenIndex = urlPattern.lastIndex - 1;
|
|
179
|
+
let index = openParenIndex + 1;
|
|
180
|
+
let depth = 1;
|
|
181
|
+
let quote = null;
|
|
182
|
+
for (; index < value.length; index++) {
|
|
183
|
+
const char = value[index];
|
|
184
|
+
if (quote) {
|
|
185
|
+
if (char === quote) quote = null;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (char === "\"" || char === "'") quote = char;
|
|
189
|
+
else if (char === "(") depth++;
|
|
190
|
+
else if (char === ")") {
|
|
191
|
+
depth--;
|
|
192
|
+
if (depth === 0) break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (depth !== 0) break;
|
|
196
|
+
const rawUrlValue = value.slice(openParenIndex + 1, index).trim();
|
|
197
|
+
const unquotedUrlValue = rawUrlValue.startsWith("\"") && rawUrlValue.endsWith("\"") || rawUrlValue.startsWith("'") && rawUrlValue.endsWith("'") ? rawUrlValue.slice(1, -1).trim() : rawUrlValue;
|
|
198
|
+
sanitized += value.slice(cursor, urlStart);
|
|
199
|
+
sanitized += hasJavascriptScheme(unquotedUrlValue) ? "url(about:blank)" : value.slice(urlStart, index + 1);
|
|
200
|
+
cursor = index + 1;
|
|
201
|
+
}
|
|
202
|
+
return sanitized + value.slice(cursor);
|
|
203
|
+
}
|
|
204
|
+
function sanitizeAttributeValue(attrName, value) {
|
|
205
|
+
if (urlAttributePattern.test(attrName) && hasJavascriptScheme(value)) return "about:blank";
|
|
206
|
+
return escapeAttributeValue(attrName.toLowerCase() === "style" ? sanitizeStyleValue(value) : value);
|
|
207
|
+
}
|
|
208
|
+
function sanitizeTranslatedHtml(html) {
|
|
209
|
+
html = html.replace(/([\w:-]+)\s*=\s*"([^"]*)"/g, (_, attrName, attrValue) => `${attrName}="${sanitizeAttributeValue(attrName, attrValue)}"`);
|
|
210
|
+
html = html.replace(/([\w:-]+)\s*=\s*'([^']*)'/g, (_, attrName, attrValue) => `${attrName}='${sanitizeAttributeValue(attrName, attrValue)}'`);
|
|
211
|
+
if (eventHandlerPattern.test(html)) {
|
|
212
|
+
warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
|
|
213
|
+
html = html.replace(eventHandlerAttributePattern, "$1on$2");
|
|
214
|
+
}
|
|
215
|
+
if (srcdocPattern.test(html)) {
|
|
216
|
+
warn("Potentially dangerous srcdoc attribute detected in translation. Consider removing srcdoc from your translation messages.");
|
|
217
|
+
html = html.replace(srcdocAttributePattern, "$1s$2");
|
|
218
|
+
}
|
|
219
|
+
html = html.replace(unquotedUrlAttributePattern, (match, boundary, prefix, attrValue) => hasJavascriptScheme(attrValue) ? `${boundary}${prefix}about:blank` : match);
|
|
220
|
+
return html;
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
181
223
|
//#region packages/shared/src/messages.ts
|
|
182
224
|
const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
|
|
183
225
|
function deepCopy(src, des) {
|
|
184
|
-
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) throw new Error("Invalid value");
|
|
185
|
-
const stack = [
|
|
186
|
-
src,
|
|
187
|
-
des
|
|
188
|
-
}];
|
|
226
|
+
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des) || des === Object.prototype) throw new Error("Invalid value");
|
|
227
|
+
const stack = [src, des];
|
|
189
228
|
while (stack.length) {
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
229
|
+
const des = stack.pop();
|
|
230
|
+
const src = stack.pop();
|
|
231
|
+
const keys = Object.keys(src);
|
|
232
|
+
for (let i = 0; i < keys.length; i++) {
|
|
233
|
+
const key = keys[i];
|
|
234
|
+
if (key === "__proto__") continue;
|
|
235
|
+
const value = src[key];
|
|
236
|
+
if (!isObject(value)) {
|
|
237
|
+
des[key] = value;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (isArray(value)) {
|
|
241
|
+
const copied = [];
|
|
242
|
+
copied.length = value.length;
|
|
243
|
+
des[key] = copied;
|
|
244
|
+
stack.push(value, copied);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
let desValue = des[key];
|
|
248
|
+
if (!isObject(desValue) || isArray(desValue)) desValue = des[key] = create();
|
|
249
|
+
stack.push(value, desValue);
|
|
250
|
+
}
|
|
200
251
|
}
|
|
201
252
|
}
|
|
202
253
|
//#endregion
|
|
@@ -474,10 +525,13 @@ function createTokenizer(source, options = {}) {
|
|
|
474
525
|
const ctx = context();
|
|
475
526
|
pos.column += offset;
|
|
476
527
|
pos.offset += offset;
|
|
477
|
-
if (onError)
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
528
|
+
if (onError) {
|
|
529
|
+
const err = createCompileError(code, location ? createLocation(ctx.startLoc, pos) : null, {
|
|
530
|
+
domain: ERROR_DOMAIN$1,
|
|
531
|
+
args
|
|
532
|
+
});
|
|
533
|
+
onError(err);
|
|
534
|
+
}
|
|
481
535
|
}
|
|
482
536
|
function getToken(context, type, value) {
|
|
483
537
|
context.endLoc = currentPosition();
|
|
@@ -610,7 +664,7 @@ function createTokenizer(source, options = {}) {
|
|
|
610
664
|
}
|
|
611
665
|
function takeChar(scnr, fn) {
|
|
612
666
|
const ch = scnr.currentChar();
|
|
613
|
-
if (ch === EOF) return
|
|
667
|
+
if (ch === EOF) return;
|
|
614
668
|
if (fn(ch)) {
|
|
615
669
|
scnr.next();
|
|
616
670
|
return ch;
|
|
@@ -667,15 +721,16 @@ function createTokenizer(source, options = {}) {
|
|
|
667
721
|
scnr.next();
|
|
668
722
|
}
|
|
669
723
|
} else if (ch === "{" || ch === "}" || ch === "@" || ch === "|" || !ch) break;
|
|
670
|
-
else if (ch === " " || ch === "\n")
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
724
|
+
else if (ch === " " || ch === "\n") {
|
|
725
|
+
if (isTextStart(scnr)) {
|
|
726
|
+
buf += ch;
|
|
727
|
+
scnr.next();
|
|
728
|
+
} else if (isPluralStart(scnr)) break;
|
|
729
|
+
else {
|
|
730
|
+
buf += ch;
|
|
731
|
+
scnr.next();
|
|
732
|
+
}
|
|
733
|
+
} else {
|
|
679
734
|
buf += ch;
|
|
680
735
|
scnr.next();
|
|
681
736
|
}
|
|
@@ -923,7 +978,6 @@ function createTokenizer(source, options = {}) {
|
|
|
923
978
|
return token;
|
|
924
979
|
}
|
|
925
980
|
if (isTextStart(scnr)) return getToken(context, 0, readText(scnr));
|
|
926
|
-
break;
|
|
927
981
|
}
|
|
928
982
|
return token;
|
|
929
983
|
}
|
|
@@ -971,10 +1025,13 @@ function createParser(options = {}) {
|
|
|
971
1025
|
const end = tokenzer.currentPosition();
|
|
972
1026
|
end.offset += offset;
|
|
973
1027
|
end.column += offset;
|
|
974
|
-
if (onError)
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
1028
|
+
if (onError) {
|
|
1029
|
+
const err = createCompileError(code, location ? createLocation(start, end) : null, {
|
|
1030
|
+
domain: ERROR_DOMAIN,
|
|
1031
|
+
args
|
|
1032
|
+
});
|
|
1033
|
+
onError(err);
|
|
1034
|
+
}
|
|
978
1035
|
}
|
|
979
1036
|
function startNode(type, offset, loc) {
|
|
980
1037
|
const node = { type };
|
|
@@ -1311,7 +1368,7 @@ function baseCompile(message, options = {}) {
|
|
|
1311
1368
|
code: ""
|
|
1312
1369
|
};
|
|
1313
1370
|
}
|
|
1314
|
-
/*
|
|
1371
|
+
/* #__NO_SIDE_EFFECTS__ */
|
|
1315
1372
|
function compile(message, context) {
|
|
1316
1373
|
if (isString(message)) {
|
|
1317
1374
|
checkHtmlMessage(message, isBoolean(context.warnHtmlMessage) ? context.warnHtmlMessage : true);
|
|
@@ -1328,7 +1385,7 @@ function compile(message, context) {
|
|
|
1328
1385
|
return !detectError ? compileCache[cacheKey] = msg : msg;
|
|
1329
1386
|
} else {
|
|
1330
1387
|
if (!isMessageAST(message)) {
|
|
1331
|
-
warn(`the message
|
|
1388
|
+
warn(`Cannot compile the message of '${context.key}' key, because the message compiler is not included in this build. Pre-compile the message. So translate function returns the message as is.`);
|
|
1332
1389
|
return (() => message);
|
|
1333
1390
|
}
|
|
1334
1391
|
const cacheKey = message.cacheKey;
|
|
@@ -1345,12 +1402,11 @@ const CoreErrorCodes = {
|
|
|
1345
1402
|
INVALID_ARGUMENT: 17,
|
|
1346
1403
|
INVALID_DATE_ARGUMENT: 18,
|
|
1347
1404
|
INVALID_ISO_DATE_ARGUMENT: 19,
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
NOT_SUPPORT_LOCALE_TYPE: 23
|
|
1405
|
+
NOT_SUPPORT_LOCALE_PROMISE_VALUE: 20,
|
|
1406
|
+
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 21,
|
|
1407
|
+
NOT_SUPPORT_LOCALE_TYPE: 22
|
|
1352
1408
|
};
|
|
1353
|
-
const CORE_ERROR_CODES_EXTEND_POINT =
|
|
1409
|
+
const CORE_ERROR_CODES_EXTEND_POINT = 23;
|
|
1354
1410
|
function createCoreError(code) {
|
|
1355
1411
|
return createCompileError(code, null, { messages: errorMessages });
|
|
1356
1412
|
}
|
|
@@ -1359,7 +1415,6 @@ const errorMessages = {
|
|
|
1359
1415
|
[CoreErrorCodes.INVALID_ARGUMENT]: "Invalid arguments",
|
|
1360
1416
|
[CoreErrorCodes.INVALID_DATE_ARGUMENT]: "The date provided is an invalid Date object.Make sure your Date represents a valid date.",
|
|
1361
1417
|
[CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: "The argument provided is not a valid ISO date string",
|
|
1362
|
-
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: "Not support non-string message",
|
|
1363
1418
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE]: "cannot support promise value",
|
|
1364
1419
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: "cannot support async function",
|
|
1365
1420
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: "cannot support locale type"
|
|
@@ -1374,13 +1429,14 @@ let _resolveLocale;
|
|
|
1374
1429
|
/** @internal */
|
|
1375
1430
|
function resolveLocale(locale) {
|
|
1376
1431
|
if (isString(locale)) return locale;
|
|
1377
|
-
else if (isFunction(locale))
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1432
|
+
else if (isFunction(locale)) {
|
|
1433
|
+
if (locale.resolvedOnce && _resolveLocale != null) return _resolveLocale;
|
|
1434
|
+
else if (locale.constructor.name === "Function") {
|
|
1435
|
+
const resolve = locale();
|
|
1436
|
+
if (isPromise(resolve)) throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
|
|
1437
|
+
return _resolveLocale = resolve;
|
|
1438
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
|
|
1439
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
|
|
1384
1440
|
}
|
|
1385
1441
|
/**
|
|
1386
1442
|
* Fallback with simple implemenation
|
|
@@ -1388,7 +1444,7 @@ function resolveLocale(locale) {
|
|
|
1388
1444
|
* @remarks
|
|
1389
1445
|
* A fallback locale function implemented with a simple fallback algorithm.
|
|
1390
1446
|
*
|
|
1391
|
-
* Basically,
|
|
1447
|
+
* Basically, the chain consists of `start` plus the specified fallback: for a string or array `fallbackLocale`, the value as specified in the props is used; for a map `fallbackLocale`, the map's **keys** are used and the `default` key is skipped since it is not a locale name.
|
|
1392
1448
|
*
|
|
1393
1449
|
* @param ctx - A {@link CoreContext | context}
|
|
1394
1450
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
@@ -1399,7 +1455,7 @@ function resolveLocale(locale) {
|
|
|
1399
1455
|
* @VueI18nGeneral
|
|
1400
1456
|
*/
|
|
1401
1457
|
function fallbackWithSimple(_ctx, fallback, start) {
|
|
1402
|
-
return [
|
|
1458
|
+
return [.../* @__PURE__ */ new Set([start, ...isArray(fallback) ? fallback : isObject(fallback) ? Object.keys(fallback).filter((locale) => locale !== "default") : isString(fallback) ? [fallback] : [start]])];
|
|
1403
1459
|
}
|
|
1404
1460
|
/**
|
|
1405
1461
|
* Fallback with locale chain
|
|
@@ -1407,6 +1463,9 @@ function fallbackWithSimple(_ctx, fallback, start) {
|
|
|
1407
1463
|
* @remarks
|
|
1408
1464
|
* A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
|
|
1409
1465
|
*
|
|
1466
|
+
* Chains are cached per fallback identity. If you mutate an object or array fallback in place,
|
|
1467
|
+
* pass a new object or array instead so that the chain is recomputed.
|
|
1468
|
+
*
|
|
1410
1469
|
* @param ctx - A {@link CoreContext | context}
|
|
1411
1470
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
1412
1471
|
* @param start - A starting {@link Locale | locale}
|
|
@@ -1420,17 +1479,31 @@ function fallbackWithSimple(_ctx, fallback, start) {
|
|
|
1420
1479
|
function fallbackWithLocaleChain(ctx, fallback, start) {
|
|
1421
1480
|
const startLocale = isString(start) ? start : DEFAULT_LOCALE;
|
|
1422
1481
|
const context = ctx;
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
context.__localeChainCache.
|
|
1482
|
+
let chains;
|
|
1483
|
+
if (isObject(fallback)) {
|
|
1484
|
+
if (!context.__localeChainObjectCache) context.__localeChainObjectCache = /* @__PURE__ */ new WeakMap();
|
|
1485
|
+
chains = context.__localeChainObjectCache.get(fallback);
|
|
1486
|
+
if (!chains) {
|
|
1487
|
+
chains = /* @__PURE__ */ new Map();
|
|
1488
|
+
context.__localeChainObjectCache.set(fallback, chains);
|
|
1489
|
+
}
|
|
1490
|
+
} else {
|
|
1491
|
+
if (!context.__localeChainCache) context.__localeChainCache = /* @__PURE__ */ new Map();
|
|
1492
|
+
chains = context.__localeChainCache.get(fallback);
|
|
1493
|
+
if (!chains) {
|
|
1494
|
+
chains = /* @__PURE__ */ new Map();
|
|
1495
|
+
context.__localeChainCache.set(fallback, chains);
|
|
1496
|
+
}
|
|
1433
1497
|
}
|
|
1498
|
+
const cached = chains.get(startLocale);
|
|
1499
|
+
if (cached) return cached;
|
|
1500
|
+
const chain = [];
|
|
1501
|
+
let block = [start];
|
|
1502
|
+
while (isArray(block)) block = appendBlockToChain(chain, block, fallback);
|
|
1503
|
+
const defaults = isArray(fallback) || !isPlainObject(fallback) ? fallback : fallback["default"] ? fallback["default"] : null;
|
|
1504
|
+
block = isString(defaults) ? [defaults] : defaults;
|
|
1505
|
+
if (isArray(block)) appendBlockToChain(chain, block, false);
|
|
1506
|
+
chains.set(startLocale, chain);
|
|
1434
1507
|
return chain;
|
|
1435
1508
|
}
|
|
1436
1509
|
function appendBlockToChain(chain, block, blocks) {
|
|
@@ -1643,7 +1716,7 @@ const cache = /* @__PURE__ */ new Map();
|
|
|
1643
1716
|
* @VueI18nGeneral
|
|
1644
1717
|
*/
|
|
1645
1718
|
function resolveWithKeyValue(obj, path) {
|
|
1646
|
-
return isObject(obj) ? obj
|
|
1719
|
+
return isObject(obj) ? getOwn(obj, path) : null;
|
|
1647
1720
|
}
|
|
1648
1721
|
/**
|
|
1649
1722
|
* message resolver
|
|
@@ -1698,12 +1771,15 @@ const CoreWarnCodes = {
|
|
|
1698
1771
|
FALLBACK_TO_DATE_FORMAT: 6,
|
|
1699
1772
|
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
|
|
1700
1773
|
INVALID_NUMBER_ARGUMENT: 8,
|
|
1701
|
-
INVALID_DATE_ARGUMENT: 9
|
|
1774
|
+
INVALID_DATE_ARGUMENT: 9,
|
|
1775
|
+
IGNORE_UNSAFE_LOCALE: 10,
|
|
1776
|
+
FALLBACK_TO_DEFAULT_PLURAL_RULE: 11,
|
|
1777
|
+
FALLBACK_FROM_INVALID_LOCALE: 12
|
|
1702
1778
|
};
|
|
1703
|
-
const CORE_WARN_CODES_EXTEND_POINT =
|
|
1779
|
+
const CORE_WARN_CODES_EXTEND_POINT = 13;
|
|
1704
1780
|
/** @internal */
|
|
1705
1781
|
const warnMessages = {
|
|
1706
|
-
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale
|
|
1782
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale {resource}.`,
|
|
1707
1783
|
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
1708
1784
|
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
1709
1785
|
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
@@ -1711,7 +1787,10 @@ const warnMessages = {
|
|
|
1711
1787
|
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
1712
1788
|
[CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER]: `This project is using Custom Message Compiler, which is an experimental feature. It may receive breaking changes or be removed in the future.`,
|
|
1713
1789
|
[CoreWarnCodes.INVALID_NUMBER_ARGUMENT]: `Invalid argument for number formatting: expected a number but received '{value}'.`,
|
|
1714
|
-
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'
|
|
1790
|
+
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'.`,
|
|
1791
|
+
[CoreWarnCodes.IGNORE_UNSAFE_LOCALE]: `Ignore '{locale}' locale: it is a property name of Object.prototype`,
|
|
1792
|
+
[CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE]: `Fall back to the default plural rule for '{locale}' locale: Intl.PluralRules does not support it`,
|
|
1793
|
+
[CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE]: `Fall back to '{target}' locale for {type} formatting with '{locale}' locale: it is not a BCP 47 language tag`
|
|
1715
1794
|
};
|
|
1716
1795
|
function getWarnMessage(code, ...args) {
|
|
1717
1796
|
return format$1(warnMessages[code], ...args);
|
|
@@ -1722,7 +1801,7 @@ function getWarnMessage(code, ...args) {
|
|
|
1722
1801
|
* Intlify core-base version
|
|
1723
1802
|
* @internal
|
|
1724
1803
|
*/
|
|
1725
|
-
const VERSION = "12.0.0-alpha.
|
|
1804
|
+
const VERSION = "12.0.0-alpha.6";
|
|
1726
1805
|
const NOT_RESOLVED = -1;
|
|
1727
1806
|
const DEFAULT_LOCALE = "en-US";
|
|
1728
1807
|
const MISSING_RESOLVE_VALUE = "";
|
|
@@ -1782,26 +1861,21 @@ function registerLocaleFallbacker(fallbacker) {
|
|
|
1782
1861
|
warnOnce(`registerLocaleFallbacker is deprecated. Use 'localeFallbacker' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
1783
1862
|
_fallbacker = fallbacker;
|
|
1784
1863
|
}
|
|
1785
|
-
let _fallbackContext = null;
|
|
1786
|
-
const setFallbackContext = (context) => {
|
|
1787
|
-
_fallbackContext = context;
|
|
1788
|
-
};
|
|
1789
|
-
const getFallbackContext = () => _fallbackContext;
|
|
1790
1864
|
let _cid = 0;
|
|
1791
1865
|
function createCoreContext(options = {}) {
|
|
1792
1866
|
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
1793
1867
|
const version = isString(options.version) ? options.version : VERSION;
|
|
1794
1868
|
const locale = isString(options.locale) || isFunction(options.locale) ? options.locale : DEFAULT_LOCALE;
|
|
1795
1869
|
const _locale = isFunction(locale) ? DEFAULT_LOCALE : locale;
|
|
1796
|
-
const fallbackLocale =
|
|
1870
|
+
const fallbackLocale = isFallbackLocale(options.fallbackLocale) ? options.fallbackLocale : _locale;
|
|
1797
1871
|
const messages = isPlainObject(options.messages) ? options.messages : createResources(_locale);
|
|
1798
1872
|
const datetimeFormats = isPlainObject(options.datetimeFormats) ? options.datetimeFormats : createResources(_locale);
|
|
1799
1873
|
const numberFormats = isPlainObject(options.numberFormats) ? options.numberFormats : createResources(_locale);
|
|
1800
1874
|
const modifiers = assign(create(), options.modifiers, getDefaultLinkedModifiers());
|
|
1801
1875
|
const pluralRules = options.pluralRules || create();
|
|
1802
1876
|
const missing = isFunction(options.missing) ? options.missing : null;
|
|
1803
|
-
const missingWarn =
|
|
1804
|
-
const fallbackWarn =
|
|
1877
|
+
const missingWarn = isWarnOption(options.missingWarn) ? options.missingWarn : true;
|
|
1878
|
+
const fallbackWarn = isWarnOption(options.fallbackWarn) ? options.fallbackWarn : true;
|
|
1805
1879
|
const fallbackFormat = !!options.fallbackFormat;
|
|
1806
1880
|
const unresolving = !!options.unresolving;
|
|
1807
1881
|
const postTranslation = isFunction(options.postTranslation) ? options.postTranslation : null;
|
|
@@ -1809,13 +1883,9 @@ function createCoreContext(options = {}) {
|
|
|
1809
1883
|
const warnHtmlMessage = isBoolean(options.warnHtmlMessage) ? options.warnHtmlMessage : true;
|
|
1810
1884
|
const escapeParameter = !!options.escapeParameter;
|
|
1811
1885
|
const messageCompiler = isFunction(options.messageCompiler) ? options.messageCompiler : _compiler || null;
|
|
1812
|
-
if (isFunction(options.messageCompiler)) warnOnce(getWarnMessage(CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER));
|
|
1813
1886
|
const messageResolver = isFunction(options.messageResolver) ? options.messageResolver : _resolver || resolveWithKeyValue;
|
|
1814
1887
|
const localeFallbacker = isFunction(options.localeFallbacker) ? options.localeFallbacker : _fallbacker || fallbackWithSimple;
|
|
1815
1888
|
const fallbackContext = isObject(options.fallbackContext) ? options.fallbackContext : void 0;
|
|
1816
|
-
const internalOptions = options;
|
|
1817
|
-
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters) ? internalOptions.__datetimeFormatters : /* @__PURE__ */ new Map();
|
|
1818
|
-
const __numberFormatters = isObject(internalOptions.__numberFormatters) ? internalOptions.__numberFormatters : /* @__PURE__ */ new Map();
|
|
1819
1889
|
_cid++;
|
|
1820
1890
|
const context = {
|
|
1821
1891
|
version,
|
|
@@ -1842,9 +1912,8 @@ function createCoreContext(options = {}) {
|
|
|
1842
1912
|
};
|
|
1843
1913
|
context.datetimeFormats = datetimeFormats;
|
|
1844
1914
|
context.numberFormats = numberFormats;
|
|
1845
|
-
context.__datetimeFormatters =
|
|
1846
|
-
context.__numberFormatters =
|
|
1847
|
-
context.__v_emitter = internalOptions.__v_emitter != null ? internalOptions.__v_emitter : void 0;
|
|
1915
|
+
context.__datetimeFormatters = /* @__PURE__ */ new Map();
|
|
1916
|
+
context.__numberFormatters = /* @__PURE__ */ new Map();
|
|
1848
1917
|
return context;
|
|
1849
1918
|
}
|
|
1850
1919
|
const createResources = (locale) => ({ [locale]: create() });
|
|
@@ -1852,7 +1921,7 @@ const createResources = (locale) => ({ [locale]: create() });
|
|
|
1852
1921
|
* @version 12.0.0
|
|
1853
1922
|
*/
|
|
1854
1923
|
function getLocaleMessage(ctx, locale) {
|
|
1855
|
-
const src = ctx.messages
|
|
1924
|
+
const src = getOwn(ctx.messages, locale);
|
|
1856
1925
|
if (src == null) return;
|
|
1857
1926
|
const dest = create();
|
|
1858
1927
|
deepCopy(src, dest);
|
|
@@ -1862,9 +1931,21 @@ function getLocaleMessage(ctx, locale) {
|
|
|
1862
1931
|
* @version 12.0.0
|
|
1863
1932
|
*/
|
|
1864
1933
|
function setLocaleMessage(ctx, locale, messages) {
|
|
1934
|
+
if (isObjectPrototypeKey(locale)) {
|
|
1935
|
+
ctx.onWarn(getWarnMessage(CoreWarnCodes.IGNORE_UNSAFE_LOCALE, { locale }));
|
|
1936
|
+
return;
|
|
1937
|
+
}
|
|
1865
1938
|
ctx.messages[locale] = messages;
|
|
1866
1939
|
}
|
|
1867
1940
|
/** @internal */
|
|
1941
|
+
function isFallbackLocale(val) {
|
|
1942
|
+
return isString(val) || isArray(val) || isPlainObject(val) || val === false;
|
|
1943
|
+
}
|
|
1944
|
+
/** @internal */
|
|
1945
|
+
function isWarnOption(val) {
|
|
1946
|
+
return isBoolean(val) || isRegExp(val);
|
|
1947
|
+
}
|
|
1948
|
+
/** @internal */
|
|
1868
1949
|
function isTranslateFallbackWarn(fallback, key) {
|
|
1869
1950
|
return isRegExp(fallback) ? fallback.test(key) : fallback;
|
|
1870
1951
|
}
|
|
@@ -1881,7 +1962,7 @@ function handleMissing(context, key, locale, missingWarn, type) {
|
|
|
1881
1962
|
locale,
|
|
1882
1963
|
key,
|
|
1883
1964
|
type,
|
|
1884
|
-
groupId:
|
|
1965
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1885
1966
|
});
|
|
1886
1967
|
}
|
|
1887
1968
|
if (missing !== null) {
|
|
@@ -1890,15 +1971,26 @@ function handleMissing(context, key, locale, missingWarn, type) {
|
|
|
1890
1971
|
} else {
|
|
1891
1972
|
if (isTranslateMissingWarn(missingWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, {
|
|
1892
1973
|
key,
|
|
1893
|
-
locale
|
|
1974
|
+
locale,
|
|
1975
|
+
resource: type === "datetime format" ? "datetime formats" : type === "number format" ? "number formats" : "messages"
|
|
1894
1976
|
}));
|
|
1895
1977
|
return key;
|
|
1896
1978
|
}
|
|
1897
1979
|
}
|
|
1980
|
+
function invalidateLocaleChainCache(ctx, fallback) {
|
|
1981
|
+
const context = ctx;
|
|
1982
|
+
if (isObject(fallback)) context.__localeChainObjectCache?.delete(fallback);
|
|
1983
|
+
else context.__localeChainCache?.delete(fallback);
|
|
1984
|
+
}
|
|
1898
1985
|
/** @internal */
|
|
1899
1986
|
function updateFallbackLocale(ctx, locale, fallback) {
|
|
1900
|
-
|
|
1901
|
-
|
|
1987
|
+
invalidateLocaleChainCache(ctx, fallback);
|
|
1988
|
+
if (ctx.fallbackContext) invalidateLocaleChainCache(ctx.fallbackContext, fallback);
|
|
1989
|
+
{
|
|
1990
|
+
const { __datetimeFormatters, __numberFormatters } = ctx;
|
|
1991
|
+
__datetimeFormatters.clear();
|
|
1992
|
+
__numberFormatters.clear();
|
|
1993
|
+
}
|
|
1902
1994
|
ctx.localeFallbacker(ctx, fallback, locale);
|
|
1903
1995
|
}
|
|
1904
1996
|
/** @internal */
|
|
@@ -1914,41 +2006,14 @@ function isImplicitFallback(targetLocale, locales) {
|
|
|
1914
2006
|
return false;
|
|
1915
2007
|
}
|
|
1916
2008
|
//#endregion
|
|
1917
|
-
//#region packages/core-base/src/
|
|
1918
|
-
|
|
1919
|
-
const
|
|
1920
|
-
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
1921
|
-
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
1922
|
-
};
|
|
1923
|
-
//#endregion
|
|
1924
|
-
//#region packages/core-base/src/datetime.ts
|
|
1925
|
-
function datetime(context, ...args) {
|
|
1926
|
-
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
1927
|
-
const { __datetimeFormatters } = context;
|
|
1928
|
-
if (!Availabilities.dateTimeFormat) {
|
|
1929
|
-
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
1930
|
-
return "";
|
|
1931
|
-
}
|
|
1932
|
-
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
1933
|
-
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
1934
|
-
return "";
|
|
1935
|
-
}
|
|
1936
|
-
const [key, value, options, overrides] = parseDateTimeArgs(...args);
|
|
1937
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
1938
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1939
|
-
const part = !!options.part;
|
|
1940
|
-
const locale = getLocale(context, options);
|
|
2009
|
+
//#region packages/core-base/src/formatter.ts
|
|
2010
|
+
function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type) {
|
|
2011
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
1941
2012
|
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
1942
|
-
if (!isString(key) || key === "") return new Intl.DateTimeFormat(locale.replace(/!/g, ""), overrides).format(value);
|
|
1943
|
-
let datetimeFormat = {};
|
|
1944
|
-
let targetLocale;
|
|
1945
|
-
let format = null;
|
|
1946
2013
|
let from = locale;
|
|
1947
|
-
let to = null;
|
|
1948
|
-
const type = "datetime format";
|
|
1949
2014
|
for (let i = 0; i < locales.length; i++) {
|
|
1950
|
-
targetLocale =
|
|
1951
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
2015
|
+
const targetLocale = locales[i];
|
|
2016
|
+
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(type === "datetime format" ? CoreWarnCodes.FALLBACK_TO_DATE_FORMAT : CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1952
2017
|
key,
|
|
1953
2018
|
target: targetLocale
|
|
1954
2019
|
}));
|
|
@@ -1958,26 +2023,119 @@ function datetime(context, ...args) {
|
|
|
1958
2023
|
type,
|
|
1959
2024
|
key,
|
|
1960
2025
|
from,
|
|
1961
|
-
to,
|
|
1962
|
-
groupId:
|
|
2026
|
+
to: targetLocale,
|
|
2027
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1963
2028
|
});
|
|
1964
2029
|
}
|
|
1965
|
-
|
|
1966
|
-
format
|
|
1967
|
-
if (isPlainObject(format)) break;
|
|
2030
|
+
const format = getOwn(getOwn(formats, targetLocale) || {}, key);
|
|
2031
|
+
if (isPlainObject(format) && isString(targetLocale)) return targetLocale;
|
|
1968
2032
|
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1969
|
-
from =
|
|
2033
|
+
from = targetLocale;
|
|
1970
2034
|
}
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
2035
|
+
return null;
|
|
2036
|
+
}
|
|
2037
|
+
function isLanguageTag(locale) {
|
|
2038
|
+
try {
|
|
2039
|
+
Intl.getCanonicalLocales(locale);
|
|
2040
|
+
return true;
|
|
2041
|
+
} catch {
|
|
2042
|
+
return false;
|
|
2043
|
+
}
|
|
2044
|
+
}
|
|
2045
|
+
/**
|
|
2046
|
+
* Create an `Intl.DateTimeFormat` or `Intl.NumberFormat` formatter
|
|
2047
|
+
*
|
|
2048
|
+
* @remarks
|
|
2049
|
+
* `Intl` throws a RangeError for a locale that is not a BCP 47 language tag (e.g. `pt_BR`),
|
|
2050
|
+
* so fall back to the first language tag in the locale chain, or the default locale
|
|
2051
|
+
*/
|
|
2052
|
+
function createFormatter(context, IntlFormatter, locale, options, type) {
|
|
2053
|
+
try {
|
|
2054
|
+
return new IntlFormatter(locale, options);
|
|
2055
|
+
} catch (error) {
|
|
2056
|
+
if (isLanguageTag(locale)) throw error;
|
|
2057
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
2058
|
+
const target = localeFallbacker(context, fallbackLocale, locale).find(isLanguageTag) || "en-US";
|
|
2059
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE, {
|
|
2060
|
+
locale,
|
|
2061
|
+
target,
|
|
2062
|
+
type: type === "datetime format" ? "datetime" : "number"
|
|
2063
|
+
}));
|
|
2064
|
+
return new IntlFormatter(target, options);
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
/**
|
|
2068
|
+
* Format a value with the format of a key, or only with the overrides when no key is given
|
|
2069
|
+
*
|
|
2070
|
+
* @remarks
|
|
2071
|
+
* The common part of `datetime()` and `number()`, after they check and parse the arguments
|
|
2072
|
+
*/
|
|
2073
|
+
function formatValue(context, [key, value, options, overrides], IntlFormatter, formatters, formats, type) {
|
|
2074
|
+
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2075
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2076
|
+
const part = !!options.part;
|
|
2077
|
+
const locale = getLocale(context, options);
|
|
2078
|
+
if (!isString(key) || key === "") {
|
|
2079
|
+
const formatter = createFormatter(context, IntlFormatter, locale.replace(/!/g, ""), overrides, type);
|
|
2080
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2081
|
+
}
|
|
2082
|
+
const targetLocale = resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type);
|
|
2083
|
+
if (!isString(targetLocale)) return context.unresolving ? -1 : key;
|
|
2084
|
+
const format = formats[targetLocale][key];
|
|
2085
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
2086
|
+
let formatter = formatters.get(id);
|
|
1975
2087
|
if (!formatter) {
|
|
1976
|
-
formatter =
|
|
1977
|
-
|
|
2088
|
+
formatter = createFormatter(context, IntlFormatter, targetLocale, assign({}, format, overrides), type);
|
|
2089
|
+
formatters.set(id, formatter);
|
|
1978
2090
|
}
|
|
1979
2091
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1980
2092
|
}
|
|
2093
|
+
function getFormatterCacheKey(locale, key, overrides) {
|
|
2094
|
+
let id = `${locale}__${key}`;
|
|
2095
|
+
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
2096
|
+
return id;
|
|
2097
|
+
}
|
|
2098
|
+
function clearFormatCache(formatters, locale, format) {
|
|
2099
|
+
for (const key in format) {
|
|
2100
|
+
const prefix = `${locale}__${key}`;
|
|
2101
|
+
for (const id of formatters.keys()) if (id === prefix || id.startsWith(`${prefix}__`)) formatters.delete(id);
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
function parseFormatArgs(args, options, initialOverrides, optionsKeys) {
|
|
2105
|
+
const [, arg2, arg3, arg4] = args;
|
|
2106
|
+
let overrides = initialOverrides;
|
|
2107
|
+
if (isString(arg2)) options.key = arg2;
|
|
2108
|
+
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
2109
|
+
if (optionsKeys.includes(key)) overrides[key] = arg2[key];
|
|
2110
|
+
else options[key] = arg2[key];
|
|
2111
|
+
});
|
|
2112
|
+
if (isString(arg3)) options.locale = arg3;
|
|
2113
|
+
else if (isPlainObject(arg3)) overrides = arg3;
|
|
2114
|
+
if (isPlainObject(arg4)) overrides = arg4;
|
|
2115
|
+
return overrides;
|
|
2116
|
+
}
|
|
2117
|
+
//#endregion
|
|
2118
|
+
//#region packages/core-base/src/intl.ts
|
|
2119
|
+
const intlDefined = typeof Intl !== "undefined";
|
|
2120
|
+
const Availabilities = {
|
|
2121
|
+
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
2122
|
+
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
2123
|
+
};
|
|
2124
|
+
//#endregion
|
|
2125
|
+
//#region packages/core-base/src/datetime.ts
|
|
2126
|
+
function datetime(context, ...args) {
|
|
2127
|
+
const { datetimeFormats, onWarn } = context;
|
|
2128
|
+
const { __datetimeFormatters } = context;
|
|
2129
|
+
if (!Availabilities.dateTimeFormat) {
|
|
2130
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
2131
|
+
return "";
|
|
2132
|
+
}
|
|
2133
|
+
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
2134
|
+
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
2135
|
+
return "";
|
|
2136
|
+
}
|
|
2137
|
+
return formatValue(context, parseDateTimeArgs(...args), Intl.DateTimeFormat, __datetimeFormatters, datetimeFormats, "datetime format");
|
|
2138
|
+
}
|
|
1981
2139
|
/** @internal */
|
|
1982
2140
|
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
1983
2141
|
"localeMatcher",
|
|
@@ -2003,12 +2161,12 @@ const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
|
2003
2161
|
];
|
|
2004
2162
|
/** @internal */
|
|
2005
2163
|
function parseDateTimeArgs(...args) {
|
|
2006
|
-
const [arg1
|
|
2164
|
+
const [arg1] = args;
|
|
2007
2165
|
const options = create();
|
|
2008
|
-
|
|
2166
|
+
const initialOverrides = create();
|
|
2009
2167
|
let value;
|
|
2010
2168
|
if (isString(arg1)) {
|
|
2011
|
-
const matches = arg1.match(
|
|
2169
|
+
const matches = arg1.match(/^(\d{4}-\d{2}-\d{2})(?:(T|\s)(.+))?$/);
|
|
2012
2170
|
if (!matches) throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
2013
2171
|
const dateTime = matches[3] ? matches[3].trim().startsWith("T") ? `${matches[1].trim()}${matches[3].trim()}` : `${matches[1].trim()}T${matches[3].trim()}` : matches[1].trim();
|
|
2014
2172
|
value = new Date(dateTime);
|
|
@@ -2022,14 +2180,7 @@ function parseDateTimeArgs(...args) {
|
|
|
2022
2180
|
value = arg1;
|
|
2023
2181
|
} else if (isNumber(arg1)) value = arg1;
|
|
2024
2182
|
else throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
2025
|
-
|
|
2026
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
2027
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
2028
|
-
else options[key] = arg2[key];
|
|
2029
|
-
});
|
|
2030
|
-
if (isString(arg3)) options.locale = arg3;
|
|
2031
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
2032
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
2183
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, DATETIME_FORMAT_OPTIONS_KEYS);
|
|
2033
2184
|
return [
|
|
2034
2185
|
options.key || "",
|
|
2035
2186
|
value,
|
|
@@ -2039,17 +2190,12 @@ function parseDateTimeArgs(...args) {
|
|
|
2039
2190
|
}
|
|
2040
2191
|
/** @internal */
|
|
2041
2192
|
function clearDateTimeFormat(ctx, locale, format) {
|
|
2042
|
-
|
|
2043
|
-
for (const key in format) {
|
|
2044
|
-
const id = `${locale}__${key}`;
|
|
2045
|
-
if (!context.__datetimeFormatters.has(id)) continue;
|
|
2046
|
-
context.__datetimeFormatters.delete(id);
|
|
2047
|
-
}
|
|
2193
|
+
clearFormatCache(ctx.__datetimeFormatters, locale, format);
|
|
2048
2194
|
}
|
|
2049
2195
|
//#endregion
|
|
2050
2196
|
//#region packages/core-base/src/number.ts
|
|
2051
2197
|
function number(context, ...args) {
|
|
2052
|
-
const { numberFormats,
|
|
2198
|
+
const { numberFormats, onWarn } = context;
|
|
2053
2199
|
const { __numberFormatters } = context;
|
|
2054
2200
|
if (!Availabilities.numberFormat) {
|
|
2055
2201
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
@@ -2059,50 +2205,7 @@ function number(context, ...args) {
|
|
|
2059
2205
|
onWarn(getWarnMessage(CoreWarnCodes.INVALID_NUMBER_ARGUMENT, { value: String(args[0]) }));
|
|
2060
2206
|
return "";
|
|
2061
2207
|
}
|
|
2062
|
-
|
|
2063
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2064
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2065
|
-
const part = !!options.part;
|
|
2066
|
-
const locale = getLocale(context, options);
|
|
2067
|
-
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
2068
|
-
if (!isString(key) || key === "") return new Intl.NumberFormat(locale.replace(/!/g, ""), overrides).format(value);
|
|
2069
|
-
let numberFormat = {};
|
|
2070
|
-
let targetLocale;
|
|
2071
|
-
let format = null;
|
|
2072
|
-
let from = locale;
|
|
2073
|
-
let to = null;
|
|
2074
|
-
const type = "number format";
|
|
2075
|
-
for (let i = 0; i < locales.length; i++) {
|
|
2076
|
-
targetLocale = to = locales[i];
|
|
2077
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
2078
|
-
key,
|
|
2079
|
-
target: targetLocale
|
|
2080
|
-
}));
|
|
2081
|
-
if (locale !== targetLocale) {
|
|
2082
|
-
const emitter = context.__v_emitter;
|
|
2083
|
-
if (emitter) emitter.emit("fallback", {
|
|
2084
|
-
type,
|
|
2085
|
-
key,
|
|
2086
|
-
from,
|
|
2087
|
-
to,
|
|
2088
|
-
groupId: `${type}:${key}`
|
|
2089
|
-
});
|
|
2090
|
-
}
|
|
2091
|
-
numberFormat = numberFormats[targetLocale] || {};
|
|
2092
|
-
format = numberFormat[key];
|
|
2093
|
-
if (isPlainObject(format)) break;
|
|
2094
|
-
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
2095
|
-
from = to;
|
|
2096
|
-
}
|
|
2097
|
-
if (!isPlainObject(format) || !isString(targetLocale)) return unresolving ? -1 : key;
|
|
2098
|
-
let id = `${targetLocale}__${key}`;
|
|
2099
|
-
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
2100
|
-
let formatter = __numberFormatters.get(id);
|
|
2101
|
-
if (!formatter) {
|
|
2102
|
-
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
2103
|
-
__numberFormatters.set(id, formatter);
|
|
2104
|
-
}
|
|
2105
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2208
|
+
return formatValue(context, parseNumberArgs(...args), Intl.NumberFormat, __numberFormatters, numberFormats, "number format");
|
|
2106
2209
|
}
|
|
2107
2210
|
/** @internal */
|
|
2108
2211
|
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
@@ -2129,19 +2232,12 @@ const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
|
2129
2232
|
];
|
|
2130
2233
|
/** @internal */
|
|
2131
2234
|
function parseNumberArgs(...args) {
|
|
2132
|
-
const [arg1
|
|
2235
|
+
const [arg1] = args;
|
|
2133
2236
|
const options = create();
|
|
2134
|
-
|
|
2237
|
+
const initialOverrides = create();
|
|
2135
2238
|
if (!isNumber(arg1)) throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
2136
2239
|
const value = arg1;
|
|
2137
|
-
|
|
2138
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
2139
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
2140
|
-
else options[key] = arg2[key];
|
|
2141
|
-
});
|
|
2142
|
-
if (isString(arg3)) options.locale = arg3;
|
|
2143
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
2144
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
2240
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, NUMBER_FORMAT_OPTIONS_KEYS);
|
|
2145
2241
|
return [
|
|
2146
2242
|
options.key || "",
|
|
2147
2243
|
value,
|
|
@@ -2151,12 +2247,7 @@ function parseNumberArgs(...args) {
|
|
|
2151
2247
|
}
|
|
2152
2248
|
/** @internal */
|
|
2153
2249
|
function clearNumberFormat(ctx, locale, format) {
|
|
2154
|
-
|
|
2155
|
-
for (const key in format) {
|
|
2156
|
-
const id = `${locale}__${key}`;
|
|
2157
|
-
if (!context.__numberFormatters.has(id)) continue;
|
|
2158
|
-
context.__numberFormatters.delete(id);
|
|
2159
|
-
}
|
|
2250
|
+
clearFormatCache(ctx.__numberFormatters, locale, format);
|
|
2160
2251
|
}
|
|
2161
2252
|
//#endregion
|
|
2162
2253
|
//#region packages/core-base/src/runtime.ts
|
|
@@ -2179,14 +2270,25 @@ const PLURAL_CATEGORY_ORDER = [
|
|
|
2179
2270
|
"other"
|
|
2180
2271
|
];
|
|
2181
2272
|
const intlPluralRulesCache = /* @__PURE__ */ new Map();
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2273
|
+
const INTL_PLURAL_RULES_CACHE_SIZE = 1e3;
|
|
2274
|
+
function createIntlPluralRules(locale) {
|
|
2275
|
+
try {
|
|
2276
|
+
if (Intl.PluralRules.supportedLocalesOf(locale).length === 0) return null;
|
|
2185
2277
|
const rules = new Intl.PluralRules(locale);
|
|
2186
|
-
|
|
2278
|
+
return {
|
|
2187
2279
|
rules,
|
|
2188
2280
|
categories: rules.resolvedOptions().pluralCategories.slice().sort((a, b) => PLURAL_CATEGORY_ORDER.indexOf(a) - PLURAL_CATEGORY_ORDER.indexOf(b))
|
|
2189
2281
|
};
|
|
2282
|
+
} catch {
|
|
2283
|
+
return null;
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
function getIntlPluralRules(locale) {
|
|
2287
|
+
let cached = intlPluralRulesCache.get(locale);
|
|
2288
|
+
if (cached === void 0) {
|
|
2289
|
+
cached = createIntlPluralRules(locale);
|
|
2290
|
+
if (cached === null) warn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE, { locale }));
|
|
2291
|
+
if (intlPluralRulesCache.size >= INTL_PLURAL_RULES_CACHE_SIZE) intlPluralRulesCache.delete(intlPluralRulesCache.keys().next().value);
|
|
2190
2292
|
intlPluralRulesCache.set(locale, cached);
|
|
2191
2293
|
}
|
|
2192
2294
|
return cached;
|
|
@@ -2199,10 +2301,11 @@ function createMessageContext(options = {}) {
|
|
|
2199
2301
|
const locale = options.locale;
|
|
2200
2302
|
const pluralIndex = getPluralIndex(options);
|
|
2201
2303
|
let resolvedPluralRule;
|
|
2202
|
-
if (isString(locale) && isFunction(options.pluralRules
|
|
2304
|
+
if (isString(locale) && options.pluralRules && isFunction(getOwn(options.pluralRules, locale))) resolvedPluralRule = options.pluralRules[locale];
|
|
2203
2305
|
else if (isString(locale) && typeof Intl !== "undefined" && typeof Intl.PluralRules !== "undefined") resolvedPluralRule = (choice, choicesLength) => {
|
|
2204
|
-
const
|
|
2205
|
-
if (choicesLength > categories.length) return pluralDefault(choice, choicesLength);
|
|
2306
|
+
const intlPluralRules = getIntlPluralRules(locale);
|
|
2307
|
+
if (intlPluralRules === null || choicesLength > intlPluralRules.categories.length) return pluralDefault(choice, choicesLength);
|
|
2308
|
+
const { rules, categories } = intlPluralRules;
|
|
2206
2309
|
const category = rules.select(Math.abs(choice));
|
|
2207
2310
|
const index = categories.indexOf(category);
|
|
2208
2311
|
return Math.min(index === -1 ? choicesLength - 1 : index, choicesLength - 1);
|
|
@@ -2213,16 +2316,16 @@ function createMessageContext(options = {}) {
|
|
|
2213
2316
|
const _list = options.list || [];
|
|
2214
2317
|
const list = (index) => _list[index];
|
|
2215
2318
|
const _named = options.named || create();
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
_named
|
|
2219
|
-
|
|
2220
|
-
|
|
2319
|
+
const hasPluralIndex = isNumber(options.pluralIndex);
|
|
2320
|
+
const named = (key) => {
|
|
2321
|
+
const value = isObjectPrototypeKey(key) ? getOwn(_named, key) : _named[key];
|
|
2322
|
+
return hasPluralIndex && value == null && (key === "count" || key === "n") ? options.pluralIndex : value;
|
|
2323
|
+
};
|
|
2221
2324
|
function message(key, useLinked) {
|
|
2222
2325
|
const msg = isFunction(options.messages) ? options.messages(key, !!useLinked) : isObject(options.messages) ? options.messages[key] : false;
|
|
2223
2326
|
return !msg ? options.parent ? options.parent.message(key) : DEFAULT_MESSAGE : msg;
|
|
2224
2327
|
}
|
|
2225
|
-
const _modifier = (name) => options.modifiers ? options.modifiers
|
|
2328
|
+
const _modifier = (name) => options.modifiers ? getOwn(options.modifiers, name) : DEFAULT_MODIFIER;
|
|
2226
2329
|
const normalize = isFunction(options.processor?.normalize) ? options.processor.normalize : DEFAULT_NORMALIZE;
|
|
2227
2330
|
const interpolate = isFunction(options.processor?.interpolate) ? options.processor.interpolate : DEFAULT_INTERPOLATE;
|
|
2228
2331
|
const type = isString(options.processor?.type) ? options.processor.type : DEFAULT_MESSAGE_DATA_TYPE;
|
|
@@ -2242,7 +2345,11 @@ function createMessageContext(options = {}) {
|
|
|
2242
2345
|
const ret = message(key, true)(ctx);
|
|
2243
2346
|
const resolved = ret === "" || ret === void 0 ? key : ret;
|
|
2244
2347
|
const msg = type === "vnode" && isArray(resolved) && modifier ? resolved[0] : resolved;
|
|
2245
|
-
|
|
2348
|
+
if (!modifier) return msg;
|
|
2349
|
+
const modify = _modifier(modifier);
|
|
2350
|
+
if (isFunction(modify)) return modify(msg, type);
|
|
2351
|
+
warnOnce(`Not found '${modifier}' modifier for the linked message of '${key}' key. Use a built-in modifier, or set a custom modifier with 'modifiers' option. So the linked message is used as is.`);
|
|
2352
|
+
return msg;
|
|
2246
2353
|
};
|
|
2247
2354
|
const ctx = {
|
|
2248
2355
|
["list"]: list,
|
|
@@ -2253,7 +2360,10 @@ function createMessageContext(options = {}) {
|
|
|
2253
2360
|
["type"]: type,
|
|
2254
2361
|
["interpolate"]: interpolate,
|
|
2255
2362
|
["normalize"]: normalize,
|
|
2256
|
-
["values"]: assign(create(), _list, _named
|
|
2363
|
+
["values"]: assign(create(), _list, _named, hasPluralIndex ? {
|
|
2364
|
+
count: named("count"),
|
|
2365
|
+
n: named("n")
|
|
2366
|
+
} : {})
|
|
2257
2367
|
};
|
|
2258
2368
|
return ctx;
|
|
2259
2369
|
}
|
|
@@ -2261,6 +2371,8 @@ function createMessageContext(options = {}) {
|
|
|
2261
2371
|
//#region packages/core-base/src/translate.ts
|
|
2262
2372
|
const NOOP_MESSAGE_FUNCTION = () => "";
|
|
2263
2373
|
const isMessageFunction = (val) => isFunction(val);
|
|
2374
|
+
/** @internal */
|
|
2375
|
+
const isMessageFormat = (val) => isString(val) || isMessageAST(val) || isMessageFunction(val);
|
|
2264
2376
|
function translate(context, ...args) {
|
|
2265
2377
|
const [key, options] = parseTranslateArgs(...args);
|
|
2266
2378
|
const locale = getLocale(context, options);
|
|
@@ -2270,21 +2382,18 @@ function translate(context, ...args) {
|
|
|
2270
2382
|
let [format, targetLocale, message] = !resolvedMessage ? resolveMessageFormat(context, key, locale, context.fallbackLocale, isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn, isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn) : [
|
|
2271
2383
|
key,
|
|
2272
2384
|
locale,
|
|
2273
|
-
context.messages
|
|
2385
|
+
getOwn(context.messages, locale) || create()
|
|
2274
2386
|
];
|
|
2275
2387
|
let cacheBaseKey = key;
|
|
2276
|
-
if (!resolvedMessage && !(
|
|
2388
|
+
if (!resolvedMessage && !isMessageFormat(format)) {
|
|
2277
2389
|
const defaultMsgOrKey = isString(options.default) ? options.default : isBoolean(options.default) || context.fallbackFormat ? !context.messageCompiler ? () => key : key : null;
|
|
2278
2390
|
if (context.fallbackFormat || defaultMsgOrKey != null && (isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey))) {
|
|
2279
2391
|
format = defaultMsgOrKey;
|
|
2280
2392
|
cacheBaseKey = format;
|
|
2281
2393
|
}
|
|
2282
2394
|
}
|
|
2283
|
-
if (!resolvedMessage && (!(
|
|
2284
|
-
if (isString(format) && context.messageCompiler == null) {
|
|
2285
|
-
warn(`The message format compilation is not supported in this build. Because message compiler isn't included. You need to pre-compilation all message format. So translate function return '${isString(key) ? key : isObject(key) ? JSON.stringify(key) : isFunction(key) ? key.name || "function" : ""}'.`);
|
|
2286
|
-
return key;
|
|
2287
|
-
}
|
|
2395
|
+
if (!resolvedMessage && (!isMessageFormat(format) || !isString(targetLocale))) return context.unresolving ? -1 : key;
|
|
2396
|
+
if (isString(format) && context.messageCompiler == null) warn(`Cannot compile the message of '${stringifyKey(key)}' key, because no message compiler is set. Use 'messageCompiler' option of 'createCoreContext', or pre-compile the message. So translate function returns the message as is.`);
|
|
2288
2397
|
let occurred = false;
|
|
2289
2398
|
const onError = () => {
|
|
2290
2399
|
occurred = true;
|
|
@@ -2292,16 +2401,29 @@ function translate(context, ...args) {
|
|
|
2292
2401
|
const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
|
|
2293
2402
|
if (occurred) return format;
|
|
2294
2403
|
const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
|
|
2295
|
-
const
|
|
2404
|
+
const timeline = startTimelineMeasure(context, "evaluation");
|
|
2405
|
+
const messaged = msg(msgContext);
|
|
2406
|
+
if (timeline) timeline.emitter.emit("message-evaluation", {
|
|
2407
|
+
type: "message-evaluation",
|
|
2408
|
+
value: messaged,
|
|
2409
|
+
time: endTimelineMeasure(timeline),
|
|
2410
|
+
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
2411
|
+
});
|
|
2296
2412
|
let ret = context.postTranslation ? context.postTranslation(messaged, key) : messaged;
|
|
2297
2413
|
if (escapeParameter && isString(ret)) ret = sanitizeTranslatedHtml(ret);
|
|
2298
2414
|
return ret;
|
|
2299
2415
|
}
|
|
2300
2416
|
function escapeParams(options) {
|
|
2301
2417
|
if (isArray(options.list)) options.list = options.list.map((item) => isString(item) ? escapeHtml(item) : item);
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2418
|
+
if (isObject(options.named)) {
|
|
2419
|
+
const source = options.named;
|
|
2420
|
+
const named = create();
|
|
2421
|
+
for (const key of Object.keys(source)) {
|
|
2422
|
+
const value = source[key];
|
|
2423
|
+
named[key] = isString(value) ? escapeHtml(value) : value;
|
|
2424
|
+
}
|
|
2425
|
+
options.named = named;
|
|
2426
|
+
}
|
|
2305
2427
|
}
|
|
2306
2428
|
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
2307
2429
|
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
@@ -2324,36 +2446,23 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
|
|
|
2324
2446
|
key,
|
|
2325
2447
|
from,
|
|
2326
2448
|
to: targetLocale,
|
|
2327
|
-
groupId:
|
|
2449
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
2328
2450
|
});
|
|
2329
2451
|
}
|
|
2330
|
-
message = messages
|
|
2331
|
-
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
startTag = "intlify-message-resolve-start";
|
|
2337
|
-
endTag = "intlify-message-resolve-end";
|
|
2338
|
-
mark?.(startTag);
|
|
2339
|
-
}
|
|
2340
|
-
if ((format = resolveValue(message, key)) === null) format = message[key];
|
|
2341
|
-
if (inBrowser) {
|
|
2342
|
-
const end = window.performance.now();
|
|
2343
|
-
const emitter = context.__v_emitter;
|
|
2344
|
-
if (emitter && start && format) emitter.emit("message-resolve", {
|
|
2452
|
+
message = getOwn(messages, targetLocale) || create();
|
|
2453
|
+
const timeline = startTimelineMeasure(context, "resolve");
|
|
2454
|
+
if ((format = resolveValue(message, key)) === null) format = getOwn(message, key);
|
|
2455
|
+
if (timeline) {
|
|
2456
|
+
const time = endTimelineMeasure(timeline);
|
|
2457
|
+
if (format) timeline.emitter.emit("message-resolve", {
|
|
2345
2458
|
type: "message-resolve",
|
|
2346
2459
|
key,
|
|
2347
2460
|
message: format,
|
|
2348
|
-
time
|
|
2349
|
-
groupId:
|
|
2461
|
+
time,
|
|
2462
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
2350
2463
|
});
|
|
2351
|
-
if (startTag && endTag && mark && measure) {
|
|
2352
|
-
mark(endTag);
|
|
2353
|
-
measure("intlify message resolve", startTag, endTag);
|
|
2354
|
-
}
|
|
2355
2464
|
}
|
|
2356
|
-
if (
|
|
2465
|
+
if (isMessageFormat(format)) break;
|
|
2357
2466
|
if (!isImplicitFallback(targetLocale, locales)) {
|
|
2358
2467
|
const missingRet = handleMissing(context, key, targetLocale, missingWarn, type);
|
|
2359
2468
|
if (missingRet !== key) format = missingRet;
|
|
@@ -2366,6 +2475,9 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
|
|
|
2366
2475
|
message
|
|
2367
2476
|
];
|
|
2368
2477
|
}
|
|
2478
|
+
function stringifyKey(key) {
|
|
2479
|
+
return isString(key) ? key : isObject(key) ? JSON.stringify(key) : isFunction(key) ? key.name || "function" : "";
|
|
2480
|
+
}
|
|
2369
2481
|
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
2370
2482
|
if (isMessageFunction(format)) {
|
|
2371
2483
|
const msg = format;
|
|
@@ -2379,55 +2491,40 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
|
|
|
2379
2491
|
msg.key = key;
|
|
2380
2492
|
return msg;
|
|
2381
2493
|
}
|
|
2382
|
-
|
|
2383
|
-
let startTag;
|
|
2384
|
-
let endTag;
|
|
2385
|
-
if (inBrowser && mark) {
|
|
2386
|
-
start = window.performance.now();
|
|
2387
|
-
startTag = "intlify-message-compilation-start";
|
|
2388
|
-
endTag = "intlify-message-compilation-end";
|
|
2389
|
-
mark?.(startTag);
|
|
2390
|
-
}
|
|
2494
|
+
const timeline = startTimelineMeasure(context, "compilation");
|
|
2391
2495
|
const msg = context.messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, context.warnHtmlMessage, onError));
|
|
2392
|
-
if (
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
time: end - start,
|
|
2399
|
-
groupId: `translate:${key}`
|
|
2400
|
-
});
|
|
2401
|
-
if (startTag && endTag && mark && measure) {
|
|
2402
|
-
mark(endTag);
|
|
2403
|
-
measure("intlify message compilation", startTag, endTag);
|
|
2404
|
-
}
|
|
2405
|
-
}
|
|
2496
|
+
if (timeline) timeline.emitter.emit("message-compilation", {
|
|
2497
|
+
type: "message-compilation",
|
|
2498
|
+
message: format,
|
|
2499
|
+
time: endTimelineMeasure(timeline),
|
|
2500
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
2501
|
+
});
|
|
2406
2502
|
msg.locale = targetLocale;
|
|
2407
2503
|
msg.key = key;
|
|
2408
2504
|
msg.source = format;
|
|
2409
2505
|
return msg;
|
|
2410
2506
|
}
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
const
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
context.__v_emitter?.emit("message-evaluation", {
|
|
2421
|
-
type: "message-evaluation",
|
|
2422
|
-
value: messaged,
|
|
2423
|
-
time: end - start,
|
|
2424
|
-
groupId: `translate:${msg.key}`
|
|
2425
|
-
});
|
|
2426
|
-
mark(endTag);
|
|
2427
|
-
measure("intlify message evaluation", startTag, endTag);
|
|
2428
|
-
return messaged;
|
|
2507
|
+
function startTimelineMeasure(context, name) {
|
|
2508
|
+
const emitter = context.__v_emitter;
|
|
2509
|
+
if (!inBrowser || !emitter) return null;
|
|
2510
|
+
const start = window.performance.now();
|
|
2511
|
+
mark?.(`intlify-message-${name}-start`);
|
|
2512
|
+
return {
|
|
2513
|
+
emitter,
|
|
2514
|
+
name,
|
|
2515
|
+
start
|
|
2429
2516
|
};
|
|
2430
2517
|
}
|
|
2518
|
+
function endTimelineMeasure({ name, start }) {
|
|
2519
|
+
const time = window.performance.now() - start;
|
|
2520
|
+
if (mark && measure) {
|
|
2521
|
+
const startTag = `intlify-message-${name}-start`;
|
|
2522
|
+
const endTag = `intlify-message-${name}-end`;
|
|
2523
|
+
mark(endTag);
|
|
2524
|
+
measure(`intlify message ${name}`, startTag, endTag);
|
|
2525
|
+
}
|
|
2526
|
+
return time;
|
|
2527
|
+
}
|
|
2431
2528
|
/** @internal */
|
|
2432
2529
|
function parseTranslateArgs(...args) {
|
|
2433
2530
|
const [arg1, arg2, arg3] = args;
|
|
@@ -2459,12 +2556,11 @@ function getCompileContext(context, locale, key, source, warnHtmlMessage, onErro
|
|
|
2459
2556
|
error: err.message,
|
|
2460
2557
|
start: err.location && err.location.start.offset,
|
|
2461
2558
|
end: err.location && err.location.end.offset,
|
|
2462
|
-
groupId:
|
|
2559
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
2463
2560
|
});
|
|
2464
2561
|
const message = `Message compilation error: ${err.message}`;
|
|
2465
2562
|
throw new SyntaxError(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
2466
2563
|
}
|
|
2467
|
-
throw err;
|
|
2468
2564
|
},
|
|
2469
2565
|
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
2470
2566
|
};
|
|
@@ -2503,4 +2599,4 @@ function getMessageContextOptions(context, locale, message, options) {
|
|
|
2503
2599
|
};
|
|
2504
2600
|
}
|
|
2505
2601
|
//#endregion
|
|
2506
|
-
export { AST_NODE_PROPS_KEYS, CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_RESOLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple,
|
|
2602
|
+
export { AST_NODE_PROPS_KEYS, CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_RESOLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getLocale, getLocaleMessage, getWarnMessage, handleMissing, isAlmostSameLocale, isFallbackLocale, isImplicitFallback, isMessageAST, isMessageFormat, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, isWarnOption, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveLocale, resolveValue, resolveWithKeyValue, setLocaleMessage, translate, updateFallbackLocale };
|