@intlify/core 12.0.0-alpha.4 → 12.0.0-alpha.5
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 +323 -260
- package/dist/core.esm-browser.prod.js +590 -556
- package/dist/core.global.js +323 -261
- package/dist/core.global.prod.js +606 -564
- package/dist/core.js +1 -1
- package/dist/core.node.js +1 -1
- package/dist/core.runtime.esm-browser.js +296 -239
- package/dist/core.runtime.esm-browser.prod.js +451 -417
- package/dist/core.runtime.global.js +296 -240
- package/dist/core.runtime.global.prod.js +452 -416
- package/dist/core.runtime.js +1 -1
- package/dist/core.runtime.node.js +1 -1
- package/package.json +3 -3
|
@@ -1,24 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @intlify/core v12.0.0-alpha.
|
|
2
|
+
* @intlify/core v12.0.0-alpha.5
|
|
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
|
|
@@ -363,7 +414,7 @@ let compileCache = create();
|
|
|
363
414
|
function clearCompileCache() {
|
|
364
415
|
compileCache = create();
|
|
365
416
|
}
|
|
366
|
-
/*
|
|
417
|
+
/* #__NO_SIDE_EFFECTS__ */
|
|
367
418
|
function compile(message, context) {
|
|
368
419
|
{
|
|
369
420
|
if (!isMessageAST(message)) {
|
|
@@ -384,12 +435,11 @@ const CoreErrorCodes = {
|
|
|
384
435
|
INVALID_ARGUMENT: 17,
|
|
385
436
|
INVALID_DATE_ARGUMENT: 18,
|
|
386
437
|
INVALID_ISO_DATE_ARGUMENT: 19,
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
NOT_SUPPORT_LOCALE_TYPE: 23
|
|
438
|
+
NOT_SUPPORT_LOCALE_PROMISE_VALUE: 20,
|
|
439
|
+
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 21,
|
|
440
|
+
NOT_SUPPORT_LOCALE_TYPE: 22
|
|
391
441
|
};
|
|
392
|
-
const CORE_ERROR_CODES_EXTEND_POINT =
|
|
442
|
+
const CORE_ERROR_CODES_EXTEND_POINT = 23;
|
|
393
443
|
function createCoreError(code) {
|
|
394
444
|
return createCompileError(code, null, { messages: errorMessages });
|
|
395
445
|
}
|
|
@@ -398,7 +448,6 @@ const errorMessages = {
|
|
|
398
448
|
[CoreErrorCodes.INVALID_ARGUMENT]: "Invalid arguments",
|
|
399
449
|
[CoreErrorCodes.INVALID_DATE_ARGUMENT]: "The date provided is an invalid Date object.Make sure your Date represents a valid date.",
|
|
400
450
|
[CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: "The argument provided is not a valid ISO date string",
|
|
401
|
-
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: "Not support non-string message",
|
|
402
451
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE]: "cannot support promise value",
|
|
403
452
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: "cannot support async function",
|
|
404
453
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: "cannot support locale type"
|
|
@@ -413,13 +462,14 @@ let _resolveLocale;
|
|
|
413
462
|
/** @internal */
|
|
414
463
|
function resolveLocale(locale) {
|
|
415
464
|
if (isString(locale)) return locale;
|
|
416
|
-
else if (isFunction(locale))
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
465
|
+
else if (isFunction(locale)) {
|
|
466
|
+
if (locale.resolvedOnce && _resolveLocale != null) return _resolveLocale;
|
|
467
|
+
else if (locale.constructor.name === "Function") {
|
|
468
|
+
const resolve = locale();
|
|
469
|
+
if (isPromise(resolve)) throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
|
|
470
|
+
return _resolveLocale = resolve;
|
|
471
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
|
|
472
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
|
|
423
473
|
}
|
|
424
474
|
/**
|
|
425
475
|
* Fallback with simple implemenation
|
|
@@ -427,7 +477,7 @@ function resolveLocale(locale) {
|
|
|
427
477
|
* @remarks
|
|
428
478
|
* A fallback locale function implemented with a simple fallback algorithm.
|
|
429
479
|
*
|
|
430
|
-
* Basically,
|
|
480
|
+
* 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.
|
|
431
481
|
*
|
|
432
482
|
* @param ctx - A {@link CoreContext | context}
|
|
433
483
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
@@ -438,7 +488,7 @@ function resolveLocale(locale) {
|
|
|
438
488
|
* @VueI18nGeneral
|
|
439
489
|
*/
|
|
440
490
|
function fallbackWithSimple(_ctx, fallback, start) {
|
|
441
|
-
return [
|
|
491
|
+
return [.../* @__PURE__ */ new Set([start, ...isArray(fallback) ? fallback : isObject(fallback) ? Object.keys(fallback).filter((locale) => locale !== "default") : isString(fallback) ? [fallback] : [start]])];
|
|
442
492
|
}
|
|
443
493
|
/**
|
|
444
494
|
* Fallback with locale chain
|
|
@@ -446,6 +496,9 @@ function fallbackWithSimple(_ctx, fallback, start) {
|
|
|
446
496
|
* @remarks
|
|
447
497
|
* A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
|
|
448
498
|
*
|
|
499
|
+
* Chains are cached per fallback identity. If you mutate an object or array fallback in place,
|
|
500
|
+
* pass a new object or array instead so that the chain is recomputed.
|
|
501
|
+
*
|
|
449
502
|
* @param ctx - A {@link CoreContext | context}
|
|
450
503
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
451
504
|
* @param start - A starting {@link Locale | locale}
|
|
@@ -459,17 +512,31 @@ function fallbackWithSimple(_ctx, fallback, start) {
|
|
|
459
512
|
function fallbackWithLocaleChain(ctx, fallback, start) {
|
|
460
513
|
const startLocale = isString(start) ? start : DEFAULT_LOCALE;
|
|
461
514
|
const context = ctx;
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
context.__localeChainCache.
|
|
515
|
+
let chains;
|
|
516
|
+
if (isObject(fallback)) {
|
|
517
|
+
if (!context.__localeChainObjectCache) context.__localeChainObjectCache = /* @__PURE__ */ new WeakMap();
|
|
518
|
+
chains = context.__localeChainObjectCache.get(fallback);
|
|
519
|
+
if (!chains) {
|
|
520
|
+
chains = /* @__PURE__ */ new Map();
|
|
521
|
+
context.__localeChainObjectCache.set(fallback, chains);
|
|
522
|
+
}
|
|
523
|
+
} else {
|
|
524
|
+
if (!context.__localeChainCache) context.__localeChainCache = /* @__PURE__ */ new Map();
|
|
525
|
+
chains = context.__localeChainCache.get(fallback);
|
|
526
|
+
if (!chains) {
|
|
527
|
+
chains = /* @__PURE__ */ new Map();
|
|
528
|
+
context.__localeChainCache.set(fallback, chains);
|
|
529
|
+
}
|
|
472
530
|
}
|
|
531
|
+
const cached = chains.get(startLocale);
|
|
532
|
+
if (cached) return cached;
|
|
533
|
+
const chain = [];
|
|
534
|
+
let block = [start];
|
|
535
|
+
while (isArray(block)) block = appendBlockToChain(chain, block, fallback);
|
|
536
|
+
const defaults = isArray(fallback) || !isPlainObject(fallback) ? fallback : fallback["default"] ? fallback["default"] : null;
|
|
537
|
+
block = isString(defaults) ? [defaults] : defaults;
|
|
538
|
+
if (isArray(block)) appendBlockToChain(chain, block, false);
|
|
539
|
+
chains.set(startLocale, chain);
|
|
473
540
|
return chain;
|
|
474
541
|
}
|
|
475
542
|
function appendBlockToChain(chain, block, blocks) {
|
|
@@ -761,7 +828,7 @@ function getWarnMessage(code, ...args) {
|
|
|
761
828
|
* Intlify core-base version
|
|
762
829
|
* @internal
|
|
763
830
|
*/
|
|
764
|
-
const VERSION = "12.0.0-alpha.
|
|
831
|
+
const VERSION = "12.0.0-alpha.5";
|
|
765
832
|
const NOT_RESOLVED = -1;
|
|
766
833
|
const DEFAULT_LOCALE = "en-US";
|
|
767
834
|
const MISSING_RESOLVE_VALUE = "";
|
|
@@ -821,11 +888,6 @@ function registerLocaleFallbacker(fallbacker) {
|
|
|
821
888
|
warnOnce(`registerLocaleFallbacker is deprecated. Use 'localeFallbacker' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
822
889
|
_fallbacker = fallbacker;
|
|
823
890
|
}
|
|
824
|
-
let _fallbackContext = null;
|
|
825
|
-
const setFallbackContext = (context) => {
|
|
826
|
-
_fallbackContext = context;
|
|
827
|
-
};
|
|
828
|
-
const getFallbackContext = () => _fallbackContext;
|
|
829
891
|
let _cid = 0;
|
|
830
892
|
function createCoreContext(options = {}) {
|
|
831
893
|
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
@@ -891,7 +953,7 @@ const createResources = (locale) => ({ [locale]: create() });
|
|
|
891
953
|
* @version 12.0.0
|
|
892
954
|
*/
|
|
893
955
|
function getLocaleMessage(ctx, locale) {
|
|
894
|
-
const src = ctx.messages
|
|
956
|
+
const src = getOwn(ctx.messages, locale);
|
|
895
957
|
if (src == null) return;
|
|
896
958
|
const dest = create();
|
|
897
959
|
deepCopy(src, dest);
|
|
@@ -901,6 +963,7 @@ function getLocaleMessage(ctx, locale) {
|
|
|
901
963
|
* @version 12.0.0
|
|
902
964
|
*/
|
|
903
965
|
function setLocaleMessage(ctx, locale, messages) {
|
|
966
|
+
if (isObjectPrototypeKey(locale)) return;
|
|
904
967
|
ctx.messages[locale] = messages;
|
|
905
968
|
}
|
|
906
969
|
/** @internal */
|
|
@@ -920,7 +983,7 @@ function handleMissing(context, key, locale, missingWarn, type) {
|
|
|
920
983
|
locale,
|
|
921
984
|
key,
|
|
922
985
|
type,
|
|
923
|
-
groupId:
|
|
986
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
924
987
|
});
|
|
925
988
|
}
|
|
926
989
|
if (missing !== null) {
|
|
@@ -934,10 +997,15 @@ function handleMissing(context, key, locale, missingWarn, type) {
|
|
|
934
997
|
return key;
|
|
935
998
|
}
|
|
936
999
|
}
|
|
1000
|
+
function invalidateLocaleChainCache(ctx, fallback) {
|
|
1001
|
+
const context = ctx;
|
|
1002
|
+
if (isObject(fallback)) context.__localeChainObjectCache?.delete(fallback);
|
|
1003
|
+
else context.__localeChainCache?.delete(fallback);
|
|
1004
|
+
}
|
|
937
1005
|
/** @internal */
|
|
938
1006
|
function updateFallbackLocale(ctx, locale, fallback) {
|
|
939
|
-
|
|
940
|
-
|
|
1007
|
+
invalidateLocaleChainCache(ctx, fallback);
|
|
1008
|
+
if (ctx.fallbackContext) invalidateLocaleChainCache(ctx.fallbackContext, fallback);
|
|
941
1009
|
ctx.localeFallbacker(ctx, fallback, locale);
|
|
942
1010
|
}
|
|
943
1011
|
/** @internal */
|
|
@@ -953,6 +1021,59 @@ function isImplicitFallback(targetLocale, locales) {
|
|
|
953
1021
|
return false;
|
|
954
1022
|
}
|
|
955
1023
|
//#endregion
|
|
1024
|
+
//#region packages/core-base/src/formatter.ts
|
|
1025
|
+
function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type) {
|
|
1026
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
1027
|
+
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
1028
|
+
let from = locale;
|
|
1029
|
+
for (let i = 0; i < locales.length; i++) {
|
|
1030
|
+
const targetLocale = locales[i];
|
|
1031
|
+
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(type === "datetime format" ? CoreWarnCodes.FALLBACK_TO_DATE_FORMAT : CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1032
|
+
key,
|
|
1033
|
+
target: targetLocale
|
|
1034
|
+
}));
|
|
1035
|
+
if (locale !== targetLocale) {
|
|
1036
|
+
const emitter = context.__v_emitter;
|
|
1037
|
+
if (emitter) emitter.emit("fallback", {
|
|
1038
|
+
type,
|
|
1039
|
+
key,
|
|
1040
|
+
from,
|
|
1041
|
+
to: targetLocale,
|
|
1042
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
const format = (formats[targetLocale] || {})[key];
|
|
1046
|
+
if (isPlainObject(format) && isString(targetLocale)) return targetLocale;
|
|
1047
|
+
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1048
|
+
from = targetLocale;
|
|
1049
|
+
}
|
|
1050
|
+
return null;
|
|
1051
|
+
}
|
|
1052
|
+
function getFormatterCacheKey(locale, key, overrides) {
|
|
1053
|
+
let id = `${locale}__${key}`;
|
|
1054
|
+
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
1055
|
+
return id;
|
|
1056
|
+
}
|
|
1057
|
+
function clearFormatCache(formatters, locale, format) {
|
|
1058
|
+
for (const key in format) {
|
|
1059
|
+
const prefix = `${locale}__${key}`;
|
|
1060
|
+
for (const id of formatters.keys()) if (id === prefix || id.startsWith(`${prefix}__`)) formatters.delete(id);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
function parseFormatArgs(args, options, initialOverrides, optionsKeys) {
|
|
1064
|
+
const [, arg2, arg3, arg4] = args;
|
|
1065
|
+
let overrides = initialOverrides;
|
|
1066
|
+
if (isString(arg2)) options.key = arg2;
|
|
1067
|
+
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1068
|
+
if (optionsKeys.includes(key)) overrides[key] = arg2[key];
|
|
1069
|
+
else options[key] = arg2[key];
|
|
1070
|
+
});
|
|
1071
|
+
if (isString(arg3)) options.locale = arg3;
|
|
1072
|
+
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1073
|
+
if (isPlainObject(arg4)) overrides = arg4;
|
|
1074
|
+
return overrides;
|
|
1075
|
+
}
|
|
1076
|
+
//#endregion
|
|
956
1077
|
//#region packages/core-base/src/intl.ts
|
|
957
1078
|
const intlDefined = typeof Intl !== "undefined";
|
|
958
1079
|
const Availabilities = {
|
|
@@ -962,7 +1083,7 @@ const Availabilities = {
|
|
|
962
1083
|
//#endregion
|
|
963
1084
|
//#region packages/core-base/src/datetime.ts
|
|
964
1085
|
function datetime(context, ...args) {
|
|
965
|
-
const { datetimeFormats, unresolving,
|
|
1086
|
+
const { datetimeFormats, unresolving, onWarn } = context;
|
|
966
1087
|
const { __datetimeFormatters } = context;
|
|
967
1088
|
if (!Availabilities.dateTimeFormat) {
|
|
968
1089
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
@@ -977,39 +1098,14 @@ function datetime(context, ...args) {
|
|
|
977
1098
|
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
978
1099
|
const part = !!options.part;
|
|
979
1100
|
const locale = getLocale(context, options);
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
let targetLocale;
|
|
984
|
-
let format = null;
|
|
985
|
-
let from = locale;
|
|
986
|
-
let to = null;
|
|
987
|
-
const type = "datetime format";
|
|
988
|
-
for (let i = 0; i < locales.length; i++) {
|
|
989
|
-
targetLocale = to = locales[i];
|
|
990
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
991
|
-
key,
|
|
992
|
-
target: targetLocale
|
|
993
|
-
}));
|
|
994
|
-
if (locale !== targetLocale) {
|
|
995
|
-
const emitter = context.__v_emitter;
|
|
996
|
-
if (emitter) emitter.emit("fallback", {
|
|
997
|
-
type,
|
|
998
|
-
key,
|
|
999
|
-
from,
|
|
1000
|
-
to,
|
|
1001
|
-
groupId: `${type}:${key}`
|
|
1002
|
-
});
|
|
1003
|
-
}
|
|
1004
|
-
datetimeFormat = datetimeFormats[targetLocale] || {};
|
|
1005
|
-
format = datetimeFormat[key];
|
|
1006
|
-
if (isPlainObject(format)) break;
|
|
1007
|
-
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1008
|
-
from = to;
|
|
1101
|
+
if (!isString(key) || key === "") {
|
|
1102
|
+
const formatter = new Intl.DateTimeFormat(locale.replace(/!/g, ""), overrides);
|
|
1103
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1009
1104
|
}
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1105
|
+
const targetLocale = resolveFormatLocale(context, key, locale, datetimeFormats, missingWarn, fallbackWarn, "datetime format");
|
|
1106
|
+
if (!isString(targetLocale)) return unresolving ? -1 : key;
|
|
1107
|
+
const format = datetimeFormats[targetLocale][key];
|
|
1108
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
1013
1109
|
let formatter = __datetimeFormatters.get(id);
|
|
1014
1110
|
if (!formatter) {
|
|
1015
1111
|
formatter = new Intl.DateTimeFormat(targetLocale, assign({}, format, overrides));
|
|
@@ -1042,12 +1138,12 @@ const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
|
1042
1138
|
];
|
|
1043
1139
|
/** @internal */
|
|
1044
1140
|
function parseDateTimeArgs(...args) {
|
|
1045
|
-
const [arg1
|
|
1141
|
+
const [arg1] = args;
|
|
1046
1142
|
const options = create();
|
|
1047
|
-
|
|
1143
|
+
const initialOverrides = create();
|
|
1048
1144
|
let value;
|
|
1049
1145
|
if (isString(arg1)) {
|
|
1050
|
-
const matches = arg1.match(
|
|
1146
|
+
const matches = arg1.match(/^(\d{4}-\d{2}-\d{2})(?:(T|\s)(.+))?$/);
|
|
1051
1147
|
if (!matches) throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
1052
1148
|
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();
|
|
1053
1149
|
value = new Date(dateTime);
|
|
@@ -1061,14 +1157,7 @@ function parseDateTimeArgs(...args) {
|
|
|
1061
1157
|
value = arg1;
|
|
1062
1158
|
} else if (isNumber(arg1)) value = arg1;
|
|
1063
1159
|
else throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1064
|
-
|
|
1065
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1066
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
1067
|
-
else options[key] = arg2[key];
|
|
1068
|
-
});
|
|
1069
|
-
if (isString(arg3)) options.locale = arg3;
|
|
1070
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1071
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
1160
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, DATETIME_FORMAT_OPTIONS_KEYS);
|
|
1072
1161
|
return [
|
|
1073
1162
|
options.key || "",
|
|
1074
1163
|
value,
|
|
@@ -1078,17 +1167,12 @@ function parseDateTimeArgs(...args) {
|
|
|
1078
1167
|
}
|
|
1079
1168
|
/** @internal */
|
|
1080
1169
|
function clearDateTimeFormat(ctx, locale, format) {
|
|
1081
|
-
|
|
1082
|
-
for (const key in format) {
|
|
1083
|
-
const id = `${locale}__${key}`;
|
|
1084
|
-
if (!context.__datetimeFormatters.has(id)) continue;
|
|
1085
|
-
context.__datetimeFormatters.delete(id);
|
|
1086
|
-
}
|
|
1170
|
+
clearFormatCache(ctx.__datetimeFormatters, locale, format);
|
|
1087
1171
|
}
|
|
1088
1172
|
//#endregion
|
|
1089
1173
|
//#region packages/core-base/src/number.ts
|
|
1090
1174
|
function number(context, ...args) {
|
|
1091
|
-
const { numberFormats, unresolving,
|
|
1175
|
+
const { numberFormats, unresolving, onWarn } = context;
|
|
1092
1176
|
const { __numberFormatters } = context;
|
|
1093
1177
|
if (!Availabilities.numberFormat) {
|
|
1094
1178
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
@@ -1103,39 +1187,14 @@ function number(context, ...args) {
|
|
|
1103
1187
|
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1104
1188
|
const part = !!options.part;
|
|
1105
1189
|
const locale = getLocale(context, options);
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
let targetLocale;
|
|
1110
|
-
let format = null;
|
|
1111
|
-
let from = locale;
|
|
1112
|
-
let to = null;
|
|
1113
|
-
const type = "number format";
|
|
1114
|
-
for (let i = 0; i < locales.length; i++) {
|
|
1115
|
-
targetLocale = to = locales[i];
|
|
1116
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1117
|
-
key,
|
|
1118
|
-
target: targetLocale
|
|
1119
|
-
}));
|
|
1120
|
-
if (locale !== targetLocale) {
|
|
1121
|
-
const emitter = context.__v_emitter;
|
|
1122
|
-
if (emitter) emitter.emit("fallback", {
|
|
1123
|
-
type,
|
|
1124
|
-
key,
|
|
1125
|
-
from,
|
|
1126
|
-
to,
|
|
1127
|
-
groupId: `${type}:${key}`
|
|
1128
|
-
});
|
|
1129
|
-
}
|
|
1130
|
-
numberFormat = numberFormats[targetLocale] || {};
|
|
1131
|
-
format = numberFormat[key];
|
|
1132
|
-
if (isPlainObject(format)) break;
|
|
1133
|
-
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1134
|
-
from = to;
|
|
1190
|
+
if (!isString(key) || key === "") {
|
|
1191
|
+
const formatter = new Intl.NumberFormat(locale.replace(/!/g, ""), overrides);
|
|
1192
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1135
1193
|
}
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1194
|
+
const targetLocale = resolveFormatLocale(context, key, locale, numberFormats, missingWarn, fallbackWarn, "number format");
|
|
1195
|
+
if (!isString(targetLocale)) return unresolving ? -1 : key;
|
|
1196
|
+
const format = numberFormats[targetLocale][key];
|
|
1197
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
1139
1198
|
let formatter = __numberFormatters.get(id);
|
|
1140
1199
|
if (!formatter) {
|
|
1141
1200
|
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
@@ -1168,19 +1227,12 @@ const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
|
1168
1227
|
];
|
|
1169
1228
|
/** @internal */
|
|
1170
1229
|
function parseNumberArgs(...args) {
|
|
1171
|
-
const [arg1
|
|
1230
|
+
const [arg1] = args;
|
|
1172
1231
|
const options = create();
|
|
1173
|
-
|
|
1232
|
+
const initialOverrides = create();
|
|
1174
1233
|
if (!isNumber(arg1)) throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1175
1234
|
const value = arg1;
|
|
1176
|
-
|
|
1177
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1178
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
1179
|
-
else options[key] = arg2[key];
|
|
1180
|
-
});
|
|
1181
|
-
if (isString(arg3)) options.locale = arg3;
|
|
1182
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1183
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
1235
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, NUMBER_FORMAT_OPTIONS_KEYS);
|
|
1184
1236
|
return [
|
|
1185
1237
|
options.key || "",
|
|
1186
1238
|
value,
|
|
@@ -1190,12 +1242,7 @@ function parseNumberArgs(...args) {
|
|
|
1190
1242
|
}
|
|
1191
1243
|
/** @internal */
|
|
1192
1244
|
function clearNumberFormat(ctx, locale, format) {
|
|
1193
|
-
|
|
1194
|
-
for (const key in format) {
|
|
1195
|
-
const id = `${locale}__${key}`;
|
|
1196
|
-
if (!context.__numberFormatters.has(id)) continue;
|
|
1197
|
-
context.__numberFormatters.delete(id);
|
|
1198
|
-
}
|
|
1245
|
+
clearFormatCache(ctx.__numberFormatters, locale, format);
|
|
1199
1246
|
}
|
|
1200
1247
|
//#endregion
|
|
1201
1248
|
//#region packages/core-base/src/runtime.ts
|
|
@@ -1300,6 +1347,8 @@ function createMessageContext(options = {}) {
|
|
|
1300
1347
|
//#region packages/core-base/src/translate.ts
|
|
1301
1348
|
const NOOP_MESSAGE_FUNCTION = () => "";
|
|
1302
1349
|
const isMessageFunction = (val) => isFunction(val);
|
|
1350
|
+
/** @internal */
|
|
1351
|
+
const isMessageFormat = (val) => isString(val) || isMessageAST(val) || isMessageFunction(val);
|
|
1303
1352
|
function translate(context, ...args) {
|
|
1304
1353
|
const [key, options] = parseTranslateArgs(...args);
|
|
1305
1354
|
const locale = getLocale(context, options);
|
|
@@ -1312,16 +1361,16 @@ function translate(context, ...args) {
|
|
|
1312
1361
|
context.messages[locale] || create()
|
|
1313
1362
|
];
|
|
1314
1363
|
let cacheBaseKey = key;
|
|
1315
|
-
if (!resolvedMessage && !(
|
|
1364
|
+
if (!resolvedMessage && !isMessageFormat(format)) {
|
|
1316
1365
|
const defaultMsgOrKey = isString(options.default) ? options.default : isBoolean(options.default) || context.fallbackFormat ? !context.messageCompiler ? () => key : key : null;
|
|
1317
1366
|
if (context.fallbackFormat || defaultMsgOrKey != null && (isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey))) {
|
|
1318
1367
|
format = defaultMsgOrKey;
|
|
1319
1368
|
cacheBaseKey = format;
|
|
1320
1369
|
}
|
|
1321
1370
|
}
|
|
1322
|
-
if (!resolvedMessage && (!(
|
|
1371
|
+
if (!resolvedMessage && (!isMessageFormat(format) || !isString(targetLocale))) return context.unresolving ? -1 : key;
|
|
1323
1372
|
if (isString(format) && context.messageCompiler == null) {
|
|
1324
|
-
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 '${
|
|
1373
|
+
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 '${stringifyKey(key)}'.`);
|
|
1325
1374
|
return key;
|
|
1326
1375
|
}
|
|
1327
1376
|
let occurred = false;
|
|
@@ -1338,9 +1387,15 @@ function translate(context, ...args) {
|
|
|
1338
1387
|
}
|
|
1339
1388
|
function escapeParams(options) {
|
|
1340
1389
|
if (isArray(options.list)) options.list = options.list.map((item) => isString(item) ? escapeHtml(item) : item);
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1390
|
+
if (isObject(options.named)) {
|
|
1391
|
+
const source = options.named;
|
|
1392
|
+
const named = create();
|
|
1393
|
+
for (const key of Object.keys(source)) {
|
|
1394
|
+
const value = source[key];
|
|
1395
|
+
named[key] = isString(value) ? escapeHtml(value) : value;
|
|
1396
|
+
}
|
|
1397
|
+
options.named = named;
|
|
1398
|
+
}
|
|
1344
1399
|
}
|
|
1345
1400
|
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
1346
1401
|
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
@@ -1363,7 +1418,7 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
|
|
|
1363
1418
|
key,
|
|
1364
1419
|
from,
|
|
1365
1420
|
to: targetLocale,
|
|
1366
|
-
groupId:
|
|
1421
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1367
1422
|
});
|
|
1368
1423
|
}
|
|
1369
1424
|
message = messages[targetLocale] || create();
|
|
@@ -1385,14 +1440,14 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
|
|
|
1385
1440
|
key,
|
|
1386
1441
|
message: format,
|
|
1387
1442
|
time: end - start,
|
|
1388
|
-
groupId:
|
|
1443
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1389
1444
|
});
|
|
1390
1445
|
if (startTag && endTag && mark && measure) {
|
|
1391
1446
|
mark(endTag);
|
|
1392
1447
|
measure("intlify message resolve", startTag, endTag);
|
|
1393
1448
|
}
|
|
1394
1449
|
}
|
|
1395
|
-
if (
|
|
1450
|
+
if (isMessageFormat(format)) break;
|
|
1396
1451
|
if (!isImplicitFallback(targetLocale, locales)) {
|
|
1397
1452
|
const missingRet = handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1398
1453
|
if (missingRet !== key) format = missingRet;
|
|
@@ -1405,6 +1460,9 @@ function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn
|
|
|
1405
1460
|
message
|
|
1406
1461
|
];
|
|
1407
1462
|
}
|
|
1463
|
+
function stringifyKey(key) {
|
|
1464
|
+
return isString(key) ? key : isObject(key) ? JSON.stringify(key) : isFunction(key) ? key.name || "function" : "";
|
|
1465
|
+
}
|
|
1408
1466
|
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
1409
1467
|
if (isMessageFunction(format)) {
|
|
1410
1468
|
const msg = format;
|
|
@@ -1435,7 +1493,7 @@ function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey,
|
|
|
1435
1493
|
type: "message-compilation",
|
|
1436
1494
|
message: format,
|
|
1437
1495
|
time: end - start,
|
|
1438
|
-
groupId:
|
|
1496
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
1439
1497
|
});
|
|
1440
1498
|
if (startTag && endTag && mark && measure) {
|
|
1441
1499
|
mark(endTag);
|
|
@@ -1460,7 +1518,7 @@ if (inBrowser) {
|
|
|
1460
1518
|
type: "message-evaluation",
|
|
1461
1519
|
value: messaged,
|
|
1462
1520
|
time: end - start,
|
|
1463
|
-
groupId:
|
|
1521
|
+
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
1464
1522
|
});
|
|
1465
1523
|
mark(endTag);
|
|
1466
1524
|
measure("intlify message evaluation", startTag, endTag);
|
|
@@ -1498,12 +1556,11 @@ function getCompileContext(context, locale, key, source, warnHtmlMessage, onErro
|
|
|
1498
1556
|
error: err.message,
|
|
1499
1557
|
start: err.location && err.location.start.offset,
|
|
1500
1558
|
end: err.location && err.location.end.offset,
|
|
1501
|
-
groupId:
|
|
1559
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
1502
1560
|
});
|
|
1503
1561
|
const message = `Message compilation error: ${err.message}`;
|
|
1504
1562
|
throw new SyntaxError(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
1505
1563
|
}
|
|
1506
|
-
throw err;
|
|
1507
1564
|
},
|
|
1508
1565
|
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
1509
1566
|
};
|
|
@@ -1547,4 +1604,4 @@ registerMessageCompiler(compile);
|
|
|
1547
1604
|
registerMessageResolver(resolveValue);
|
|
1548
1605
|
registerLocaleFallbacker(fallbackWithLocaleChain);
|
|
1549
1606
|
//#endregion
|
|
1550
|
-
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,
|
|
1607
|
+
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, isImplicitFallback, isMessageAST, isMessageFormat, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveLocale, resolveValue, resolveWithKeyValue, setLocaleMessage, translate, updateFallbackLocale };
|