@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
|
@@ -1,26 +1,10 @@
|
|
|
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
6
|
var IntlifyCore = (function(exports) {
|
|
7
7
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
8
|
-
//#region packages/shared/src/warn.ts
|
|
9
|
-
function warn(msg, err) {
|
|
10
|
-
if (typeof console !== "undefined") {
|
|
11
|
-
console.warn(`[intlify] ` + msg);
|
|
12
|
-
/* istanbul ignore if */
|
|
13
|
-
if (err) console.warn(err.stack);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
const hasWarned = {};
|
|
17
|
-
function warnOnce(msg) {
|
|
18
|
-
if (!hasWarned[msg]) {
|
|
19
|
-
hasWarned[msg] = true;
|
|
20
|
-
warn(msg);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
//#endregion
|
|
24
8
|
//#region packages/shared/src/utils.ts
|
|
25
9
|
/**
|
|
26
10
|
* Original Utilities
|
|
@@ -39,6 +23,7 @@ var IntlifyCore = (function(exports) {
|
|
|
39
23
|
perf.measure(name, startTag, endTag);
|
|
40
24
|
perf.clearMarks(startTag);
|
|
41
25
|
perf.clearMarks(endTag);
|
|
26
|
+
perf.clearMeasures(name);
|
|
42
27
|
};
|
|
43
28
|
}
|
|
44
29
|
}
|
|
@@ -57,75 +42,36 @@ var IntlifyCore = (function(exports) {
|
|
|
57
42
|
});
|
|
58
43
|
const friendlyJSONstringify = (json) => JSON.stringify(json).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/\u0027/g, "\\u0027");
|
|
59
44
|
const isNumber = (val) => typeof val === "number" && isFinite(val);
|
|
45
|
+
function toDevtoolsGroupId(type, key) {
|
|
46
|
+
return isString(key) || isNumber(key) ? `${type}:${key}` : type;
|
|
47
|
+
}
|
|
60
48
|
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
61
49
|
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
62
50
|
const isKeylessObject = (val) => Object.keys(val).length === 0;
|
|
63
51
|
const assign = Object.assign;
|
|
64
52
|
const _create = Object.create;
|
|
65
53
|
const create = (obj = null) => _create(obj);
|
|
66
|
-
function escapeHtml(rawText) {
|
|
67
|
-
return rawText.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/").replace(/=/g, "=");
|
|
68
|
-
}
|
|
69
|
-
function escapeAttributeValue(value) {
|
|
70
|
-
return value.replace(/&(?![a-z0-9#]{2,6};)/gi, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
71
|
-
}
|
|
72
|
-
const javascriptSchemePattern = /^\s*javascript\s*(?::|�*58;?|�*3a;?|:?)/i;
|
|
73
|
-
const urlAttributePattern = /^(?:href|src|action|formaction)$/i;
|
|
74
|
-
function hasJavascriptScheme(value) {
|
|
75
|
-
return javascriptSchemePattern.test(value);
|
|
76
|
-
}
|
|
77
|
-
function sanitizeStyleValue(value) {
|
|
78
|
-
const urlPattern = /url\s*\(/gi;
|
|
79
|
-
let sanitized = "";
|
|
80
|
-
let cursor = 0;
|
|
81
|
-
let match;
|
|
82
|
-
while ((match = urlPattern.exec(value)) !== null) {
|
|
83
|
-
const urlStart = match.index;
|
|
84
|
-
const openParenIndex = urlPattern.lastIndex - 1;
|
|
85
|
-
let index = openParenIndex + 1;
|
|
86
|
-
let depth = 1;
|
|
87
|
-
let quote = null;
|
|
88
|
-
for (; index < value.length; index++) {
|
|
89
|
-
const char = value[index];
|
|
90
|
-
if (quote) {
|
|
91
|
-
if (char === quote) quote = null;
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
if (char === "\"" || char === "'") quote = char;
|
|
95
|
-
else if (char === "(") depth++;
|
|
96
|
-
else if (char === ")") {
|
|
97
|
-
depth--;
|
|
98
|
-
if (depth === 0) break;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (depth !== 0) break;
|
|
102
|
-
const rawUrlValue = value.slice(openParenIndex + 1, index).trim();
|
|
103
|
-
const unquotedUrlValue = rawUrlValue.startsWith("\"") && rawUrlValue.endsWith("\"") || rawUrlValue.startsWith("'") && rawUrlValue.endsWith("'") ? rawUrlValue.slice(1, -1).trim() : rawUrlValue;
|
|
104
|
-
sanitized += value.slice(cursor, urlStart);
|
|
105
|
-
sanitized += hasJavascriptScheme(unquotedUrlValue) ? "url(about:blank)" : value.slice(urlStart, index + 1);
|
|
106
|
-
cursor = index + 1;
|
|
107
|
-
}
|
|
108
|
-
return sanitized + value.slice(cursor);
|
|
109
|
-
}
|
|
110
|
-
function sanitizeAttributeValue(attrName, value) {
|
|
111
|
-
if (urlAttributePattern.test(attrName) && hasJavascriptScheme(value)) return "about:blank";
|
|
112
|
-
return escapeAttributeValue(attrName.toLowerCase() === "style" ? sanitizeStyleValue(value) : value);
|
|
113
|
-
}
|
|
114
|
-
function sanitizeTranslatedHtml(html) {
|
|
115
|
-
html = html.replace(/([\w:-]+)\s*=\s*"([^"]*)"/g, (_, attrName, attrValue) => `${attrName}="${sanitizeAttributeValue(attrName, attrValue)}"`);
|
|
116
|
-
html = html.replace(/([\w:-]+)\s*=\s*'([^']*)'/g, (_, attrName, attrValue) => `${attrName}='${sanitizeAttributeValue(attrName, attrValue)}'`);
|
|
117
|
-
if (/\s*on\w+\s*=\s*["']?[^"'>]+["']?/i.test(html)) {
|
|
118
|
-
warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
|
|
119
|
-
html = html.replace(/(\s+)on(\w+\s*=)/gi, "$1on$2");
|
|
120
|
-
}
|
|
121
|
-
html = html.replace(/(\s+(?:href|src|action|formaction)\s*=\s*)([^\s"'=<>`]+)/gi, (match, prefix, attrValue) => hasJavascriptScheme(attrValue) ? `${prefix}about:blank` : match);
|
|
122
|
-
return html;
|
|
123
|
-
}
|
|
124
54
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
125
55
|
function hasOwn(obj, key) {
|
|
126
56
|
return hasOwnProperty.call(obj, key);
|
|
127
57
|
}
|
|
128
58
|
/**
|
|
59
|
+
* Get `obj[key]` only when `key` is an own property of `obj`, so that a key such as `constructor`
|
|
60
|
+
* does not read a property inherited from `Object.prototype`.
|
|
61
|
+
* The property is read before the check, so that a reactive `obj` tracks `key` even when it is missing.
|
|
62
|
+
*/
|
|
63
|
+
function getOwn(obj, key) {
|
|
64
|
+
const value = obj[key];
|
|
65
|
+
return hasOwn(obj, key) ? value : void 0;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Whether `key` is a property name of `Object.prototype`, such as `__proto__`, `constructor` or `toString`.
|
|
69
|
+
* On a plain object, such a key reads a built-in, and assigning to `__proto__` replaces the prototype.
|
|
70
|
+
*/
|
|
71
|
+
function isObjectPrototypeKey(key) {
|
|
72
|
+
return hasOwn(Object.prototype, key);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
129
75
|
* Useful Utilities By Evan you
|
|
130
76
|
* Modified by kazuya kawaguchi
|
|
131
77
|
* MIT License
|
|
@@ -180,25 +126,130 @@ var IntlifyCore = (function(exports) {
|
|
|
180
126
|
return res.join("\n");
|
|
181
127
|
}
|
|
182
128
|
//#endregion
|
|
129
|
+
//#region packages/shared/src/warn.ts
|
|
130
|
+
function warn(msg, err) {
|
|
131
|
+
if (typeof console !== "undefined") {
|
|
132
|
+
console.warn(`[intlify] ` + msg);
|
|
133
|
+
/* istanbul ignore if */
|
|
134
|
+
if (err) console.warn(err.stack);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const hasWarned = {};
|
|
138
|
+
function warnOnce(msg) {
|
|
139
|
+
if (!hasWarned[msg]) {
|
|
140
|
+
hasWarned[msg] = true;
|
|
141
|
+
warn(msg);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region packages/shared/src/sanitize.ts
|
|
146
|
+
function escapeHtml(rawText) {
|
|
147
|
+
return rawText.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/").replace(/=/g, "=");
|
|
148
|
+
}
|
|
149
|
+
function escapeAttributeValue(value) {
|
|
150
|
+
return value.replace(/&(?![a-z0-9#]{2,6};)/gi, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
151
|
+
}
|
|
152
|
+
const javascriptSchemePattern = /^javascript:/i;
|
|
153
|
+
const urlAttributePattern = /^(?:href|src|action|formaction)$/i;
|
|
154
|
+
const numericCharacterReferencePattern = /&#(?:x([0-9a-f]+)|(\d+));?/gi;
|
|
155
|
+
const namedWhitespaceCharacterReferencePattern = /&(?:Tab|NewLine);/g;
|
|
156
|
+
const colonCharacterReferencePattern = /:?/gi;
|
|
157
|
+
const controlOrWhitespacePattern = /[\u0000-\u0020\u007f-\u009f]/g;
|
|
158
|
+
const eventHandlerPattern = /(?:^|[\s"'<>/])on\w+\s*=\s*["']?[^"'>]+["']?/i;
|
|
159
|
+
const eventHandlerAttributePattern = /(^|[\s"'<>/])on(\w+\s*=)/gi;
|
|
160
|
+
const srcdocPattern = /(?:^|[\s"'<>/])srcdoc\s*=/i;
|
|
161
|
+
const srcdocAttributePattern = /(^|[\s"'<>/])s(rcdoc\s*=)/gi;
|
|
162
|
+
const unquotedUrlAttributePattern = /(^|[\s"'<>/])((?:href|src|action|formaction)\s*=\s*)([^\s"'=<>`]+)/gi;
|
|
163
|
+
function decodeNumericCharacterReference(match, hex, decimal) {
|
|
164
|
+
const digits = hex || decimal;
|
|
165
|
+
if (!digits) return match;
|
|
166
|
+
const codePoint = Number.parseInt(digits, hex ? 16 : 10);
|
|
167
|
+
return codePoint <= 127 ? String.fromCharCode(codePoint) : match;
|
|
168
|
+
}
|
|
169
|
+
function hasJavascriptScheme(value) {
|
|
170
|
+
const normalized = value.replace(numericCharacterReferencePattern, decodeNumericCharacterReference).replace(namedWhitespaceCharacterReferencePattern, "").replace(colonCharacterReferencePattern, ":").replace(controlOrWhitespacePattern, "");
|
|
171
|
+
return javascriptSchemePattern.test(normalized);
|
|
172
|
+
}
|
|
173
|
+
function sanitizeStyleValue(value) {
|
|
174
|
+
const urlPattern = /url\s*\(/gi;
|
|
175
|
+
let sanitized = "";
|
|
176
|
+
let cursor = 0;
|
|
177
|
+
let match;
|
|
178
|
+
while ((match = urlPattern.exec(value)) !== null) {
|
|
179
|
+
const urlStart = match.index;
|
|
180
|
+
const openParenIndex = urlPattern.lastIndex - 1;
|
|
181
|
+
let index = openParenIndex + 1;
|
|
182
|
+
let depth = 1;
|
|
183
|
+
let quote = null;
|
|
184
|
+
for (; index < value.length; index++) {
|
|
185
|
+
const char = value[index];
|
|
186
|
+
if (quote) {
|
|
187
|
+
if (char === quote) quote = null;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (char === "\"" || char === "'") quote = char;
|
|
191
|
+
else if (char === "(") depth++;
|
|
192
|
+
else if (char === ")") {
|
|
193
|
+
depth--;
|
|
194
|
+
if (depth === 0) break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (depth !== 0) break;
|
|
198
|
+
const rawUrlValue = value.slice(openParenIndex + 1, index).trim();
|
|
199
|
+
const unquotedUrlValue = rawUrlValue.startsWith("\"") && rawUrlValue.endsWith("\"") || rawUrlValue.startsWith("'") && rawUrlValue.endsWith("'") ? rawUrlValue.slice(1, -1).trim() : rawUrlValue;
|
|
200
|
+
sanitized += value.slice(cursor, urlStart);
|
|
201
|
+
sanitized += hasJavascriptScheme(unquotedUrlValue) ? "url(about:blank)" : value.slice(urlStart, index + 1);
|
|
202
|
+
cursor = index + 1;
|
|
203
|
+
}
|
|
204
|
+
return sanitized + value.slice(cursor);
|
|
205
|
+
}
|
|
206
|
+
function sanitizeAttributeValue(attrName, value) {
|
|
207
|
+
if (urlAttributePattern.test(attrName) && hasJavascriptScheme(value)) return "about:blank";
|
|
208
|
+
return escapeAttributeValue(attrName.toLowerCase() === "style" ? sanitizeStyleValue(value) : value);
|
|
209
|
+
}
|
|
210
|
+
function sanitizeTranslatedHtml(html) {
|
|
211
|
+
html = html.replace(/([\w:-]+)\s*=\s*"([^"]*)"/g, (_, attrName, attrValue) => `${attrName}="${sanitizeAttributeValue(attrName, attrValue)}"`);
|
|
212
|
+
html = html.replace(/([\w:-]+)\s*=\s*'([^']*)'/g, (_, attrName, attrValue) => `${attrName}='${sanitizeAttributeValue(attrName, attrValue)}'`);
|
|
213
|
+
if (eventHandlerPattern.test(html)) {
|
|
214
|
+
warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
|
|
215
|
+
html = html.replace(eventHandlerAttributePattern, "$1on$2");
|
|
216
|
+
}
|
|
217
|
+
if (srcdocPattern.test(html)) {
|
|
218
|
+
warn("Potentially dangerous srcdoc attribute detected in translation. Consider removing srcdoc from your translation messages.");
|
|
219
|
+
html = html.replace(srcdocAttributePattern, "$1s$2");
|
|
220
|
+
}
|
|
221
|
+
html = html.replace(unquotedUrlAttributePattern, (match, boundary, prefix, attrValue) => hasJavascriptScheme(attrValue) ? `${boundary}${prefix}about:blank` : match);
|
|
222
|
+
return html;
|
|
223
|
+
}
|
|
224
|
+
//#endregion
|
|
183
225
|
//#region packages/shared/src/messages.ts
|
|
184
226
|
const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
|
|
185
227
|
function deepCopy(src, des) {
|
|
186
|
-
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) throw new Error("Invalid value");
|
|
187
|
-
const stack = [
|
|
188
|
-
src,
|
|
189
|
-
des
|
|
190
|
-
}];
|
|
228
|
+
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des) || des === Object.prototype) throw new Error("Invalid value");
|
|
229
|
+
const stack = [src, des];
|
|
191
230
|
while (stack.length) {
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
231
|
+
const des = stack.pop();
|
|
232
|
+
const src = stack.pop();
|
|
233
|
+
const keys = Object.keys(src);
|
|
234
|
+
for (let i = 0; i < keys.length; i++) {
|
|
235
|
+
const key = keys[i];
|
|
236
|
+
if (key === "__proto__") continue;
|
|
237
|
+
const value = src[key];
|
|
238
|
+
if (!isObject(value)) {
|
|
239
|
+
des[key] = value;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (isArray(value)) {
|
|
243
|
+
const copied = [];
|
|
244
|
+
copied.length = value.length;
|
|
245
|
+
des[key] = copied;
|
|
246
|
+
stack.push(value, copied);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
let desValue = des[key];
|
|
250
|
+
if (!isObject(desValue) || isArray(desValue)) desValue = des[key] = create();
|
|
251
|
+
stack.push(value, desValue);
|
|
252
|
+
}
|
|
202
253
|
}
|
|
203
254
|
}
|
|
204
255
|
//#endregion
|
|
@@ -365,11 +416,11 @@ var IntlifyCore = (function(exports) {
|
|
|
365
416
|
function clearCompileCache() {
|
|
366
417
|
compileCache = create();
|
|
367
418
|
}
|
|
368
|
-
/*
|
|
419
|
+
/* #__NO_SIDE_EFFECTS__ */
|
|
369
420
|
function compile(message, context) {
|
|
370
421
|
{
|
|
371
422
|
if (!isMessageAST(message)) {
|
|
372
|
-
warn(`the message
|
|
423
|
+
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.`);
|
|
373
424
|
return (() => message);
|
|
374
425
|
}
|
|
375
426
|
const cacheKey = message.cacheKey;
|
|
@@ -386,12 +437,11 @@ var IntlifyCore = (function(exports) {
|
|
|
386
437
|
INVALID_ARGUMENT: 17,
|
|
387
438
|
INVALID_DATE_ARGUMENT: 18,
|
|
388
439
|
INVALID_ISO_DATE_ARGUMENT: 19,
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
NOT_SUPPORT_LOCALE_TYPE: 23
|
|
440
|
+
NOT_SUPPORT_LOCALE_PROMISE_VALUE: 20,
|
|
441
|
+
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 21,
|
|
442
|
+
NOT_SUPPORT_LOCALE_TYPE: 22
|
|
393
443
|
};
|
|
394
|
-
const CORE_ERROR_CODES_EXTEND_POINT =
|
|
444
|
+
const CORE_ERROR_CODES_EXTEND_POINT = 23;
|
|
395
445
|
function createCoreError(code) {
|
|
396
446
|
return createCompileError(code, null, { messages: errorMessages });
|
|
397
447
|
}
|
|
@@ -400,7 +450,6 @@ var IntlifyCore = (function(exports) {
|
|
|
400
450
|
[CoreErrorCodes.INVALID_ARGUMENT]: "Invalid arguments",
|
|
401
451
|
[CoreErrorCodes.INVALID_DATE_ARGUMENT]: "The date provided is an invalid Date object.Make sure your Date represents a valid date.",
|
|
402
452
|
[CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: "The argument provided is not a valid ISO date string",
|
|
403
|
-
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: "Not support non-string message",
|
|
404
453
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE]: "cannot support promise value",
|
|
405
454
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: "cannot support async function",
|
|
406
455
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: "cannot support locale type"
|
|
@@ -415,13 +464,14 @@ var IntlifyCore = (function(exports) {
|
|
|
415
464
|
/** @internal */
|
|
416
465
|
function resolveLocale(locale) {
|
|
417
466
|
if (isString(locale)) return locale;
|
|
418
|
-
else if (isFunction(locale))
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
467
|
+
else if (isFunction(locale)) {
|
|
468
|
+
if (locale.resolvedOnce && _resolveLocale != null) return _resolveLocale;
|
|
469
|
+
else if (locale.constructor.name === "Function") {
|
|
470
|
+
const resolve = locale();
|
|
471
|
+
if (isPromise(resolve)) throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
|
|
472
|
+
return _resolveLocale = resolve;
|
|
473
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
|
|
474
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
|
|
425
475
|
}
|
|
426
476
|
/**
|
|
427
477
|
* Fallback with simple implemenation
|
|
@@ -429,7 +479,7 @@ var IntlifyCore = (function(exports) {
|
|
|
429
479
|
* @remarks
|
|
430
480
|
* A fallback locale function implemented with a simple fallback algorithm.
|
|
431
481
|
*
|
|
432
|
-
* Basically,
|
|
482
|
+
* 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.
|
|
433
483
|
*
|
|
434
484
|
* @param ctx - A {@link CoreContext | context}
|
|
435
485
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
@@ -440,7 +490,7 @@ var IntlifyCore = (function(exports) {
|
|
|
440
490
|
* @VueI18nGeneral
|
|
441
491
|
*/
|
|
442
492
|
function fallbackWithSimple(_ctx, fallback, start) {
|
|
443
|
-
return [
|
|
493
|
+
return [.../* @__PURE__ */ new Set([start, ...isArray(fallback) ? fallback : isObject(fallback) ? Object.keys(fallback).filter((locale) => locale !== "default") : isString(fallback) ? [fallback] : [start]])];
|
|
444
494
|
}
|
|
445
495
|
/**
|
|
446
496
|
* Fallback with locale chain
|
|
@@ -448,6 +498,9 @@ var IntlifyCore = (function(exports) {
|
|
|
448
498
|
* @remarks
|
|
449
499
|
* A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
|
|
450
500
|
*
|
|
501
|
+
* Chains are cached per fallback identity. If you mutate an object or array fallback in place,
|
|
502
|
+
* pass a new object or array instead so that the chain is recomputed.
|
|
503
|
+
*
|
|
451
504
|
* @param ctx - A {@link CoreContext | context}
|
|
452
505
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
453
506
|
* @param start - A starting {@link Locale | locale}
|
|
@@ -461,17 +514,31 @@ var IntlifyCore = (function(exports) {
|
|
|
461
514
|
function fallbackWithLocaleChain(ctx, fallback, start) {
|
|
462
515
|
const startLocale = isString(start) ? start : DEFAULT_LOCALE;
|
|
463
516
|
const context = ctx;
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
context.__localeChainCache.
|
|
517
|
+
let chains;
|
|
518
|
+
if (isObject(fallback)) {
|
|
519
|
+
if (!context.__localeChainObjectCache) context.__localeChainObjectCache = /* @__PURE__ */ new WeakMap();
|
|
520
|
+
chains = context.__localeChainObjectCache.get(fallback);
|
|
521
|
+
if (!chains) {
|
|
522
|
+
chains = /* @__PURE__ */ new Map();
|
|
523
|
+
context.__localeChainObjectCache.set(fallback, chains);
|
|
524
|
+
}
|
|
525
|
+
} else {
|
|
526
|
+
if (!context.__localeChainCache) context.__localeChainCache = /* @__PURE__ */ new Map();
|
|
527
|
+
chains = context.__localeChainCache.get(fallback);
|
|
528
|
+
if (!chains) {
|
|
529
|
+
chains = /* @__PURE__ */ new Map();
|
|
530
|
+
context.__localeChainCache.set(fallback, chains);
|
|
531
|
+
}
|
|
474
532
|
}
|
|
533
|
+
const cached = chains.get(startLocale);
|
|
534
|
+
if (cached) return cached;
|
|
535
|
+
const chain = [];
|
|
536
|
+
let block = [start];
|
|
537
|
+
while (isArray(block)) block = appendBlockToChain(chain, block, fallback);
|
|
538
|
+
const defaults = isArray(fallback) || !isPlainObject(fallback) ? fallback : fallback["default"] ? fallback["default"] : null;
|
|
539
|
+
block = isString(defaults) ? [defaults] : defaults;
|
|
540
|
+
if (isArray(block)) appendBlockToChain(chain, block, false);
|
|
541
|
+
chains.set(startLocale, chain);
|
|
475
542
|
return chain;
|
|
476
543
|
}
|
|
477
544
|
function appendBlockToChain(chain, block, blocks) {
|
|
@@ -684,7 +751,7 @@ var IntlifyCore = (function(exports) {
|
|
|
684
751
|
* @VueI18nGeneral
|
|
685
752
|
*/
|
|
686
753
|
function resolveWithKeyValue(obj, path) {
|
|
687
|
-
return isObject(obj) ? obj
|
|
754
|
+
return isObject(obj) ? getOwn(obj, path) : null;
|
|
688
755
|
}
|
|
689
756
|
/**
|
|
690
757
|
* message resolver
|
|
@@ -739,12 +806,15 @@ var IntlifyCore = (function(exports) {
|
|
|
739
806
|
FALLBACK_TO_DATE_FORMAT: 6,
|
|
740
807
|
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
|
|
741
808
|
INVALID_NUMBER_ARGUMENT: 8,
|
|
742
|
-
INVALID_DATE_ARGUMENT: 9
|
|
809
|
+
INVALID_DATE_ARGUMENT: 9,
|
|
810
|
+
IGNORE_UNSAFE_LOCALE: 10,
|
|
811
|
+
FALLBACK_TO_DEFAULT_PLURAL_RULE: 11,
|
|
812
|
+
FALLBACK_FROM_INVALID_LOCALE: 12
|
|
743
813
|
};
|
|
744
|
-
const CORE_WARN_CODES_EXTEND_POINT =
|
|
814
|
+
const CORE_WARN_CODES_EXTEND_POINT = 13;
|
|
745
815
|
/** @internal */
|
|
746
816
|
const warnMessages = {
|
|
747
|
-
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale
|
|
817
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale {resource}.`,
|
|
748
818
|
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
749
819
|
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
750
820
|
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
@@ -752,7 +822,10 @@ var IntlifyCore = (function(exports) {
|
|
|
752
822
|
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
753
823
|
[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.`,
|
|
754
824
|
[CoreWarnCodes.INVALID_NUMBER_ARGUMENT]: `Invalid argument for number formatting: expected a number but received '{value}'.`,
|
|
755
|
-
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'
|
|
825
|
+
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'.`,
|
|
826
|
+
[CoreWarnCodes.IGNORE_UNSAFE_LOCALE]: `Ignore '{locale}' locale: it is a property name of Object.prototype`,
|
|
827
|
+
[CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE]: `Fall back to the default plural rule for '{locale}' locale: Intl.PluralRules does not support it`,
|
|
828
|
+
[CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE]: `Fall back to '{target}' locale for {type} formatting with '{locale}' locale: it is not a BCP 47 language tag`
|
|
756
829
|
};
|
|
757
830
|
function getWarnMessage(code, ...args) {
|
|
758
831
|
return format$1(warnMessages[code], ...args);
|
|
@@ -763,7 +836,7 @@ var IntlifyCore = (function(exports) {
|
|
|
763
836
|
* Intlify core-base version
|
|
764
837
|
* @internal
|
|
765
838
|
*/
|
|
766
|
-
const VERSION = "12.0.0-alpha.
|
|
839
|
+
const VERSION = "12.0.0-alpha.6";
|
|
767
840
|
const NOT_RESOLVED = -1;
|
|
768
841
|
const DEFAULT_LOCALE = "en-US";
|
|
769
842
|
const MISSING_RESOLVE_VALUE = "";
|
|
@@ -823,26 +896,21 @@ var IntlifyCore = (function(exports) {
|
|
|
823
896
|
warnOnce(`registerLocaleFallbacker is deprecated. Use 'localeFallbacker' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
824
897
|
_fallbacker = fallbacker;
|
|
825
898
|
}
|
|
826
|
-
let _fallbackContext = null;
|
|
827
|
-
const setFallbackContext = (context) => {
|
|
828
|
-
_fallbackContext = context;
|
|
829
|
-
};
|
|
830
|
-
const getFallbackContext = () => _fallbackContext;
|
|
831
899
|
let _cid = 0;
|
|
832
900
|
function createCoreContext(options = {}) {
|
|
833
901
|
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
834
902
|
const version = isString(options.version) ? options.version : VERSION;
|
|
835
903
|
const locale = isString(options.locale) || isFunction(options.locale) ? options.locale : DEFAULT_LOCALE;
|
|
836
904
|
const _locale = isFunction(locale) ? DEFAULT_LOCALE : locale;
|
|
837
|
-
const fallbackLocale =
|
|
905
|
+
const fallbackLocale = isFallbackLocale(options.fallbackLocale) ? options.fallbackLocale : _locale;
|
|
838
906
|
const messages = isPlainObject(options.messages) ? options.messages : createResources(_locale);
|
|
839
907
|
const datetimeFormats = isPlainObject(options.datetimeFormats) ? options.datetimeFormats : createResources(_locale);
|
|
840
908
|
const numberFormats = isPlainObject(options.numberFormats) ? options.numberFormats : createResources(_locale);
|
|
841
909
|
const modifiers = assign(create(), options.modifiers, getDefaultLinkedModifiers());
|
|
842
910
|
const pluralRules = options.pluralRules || create();
|
|
843
911
|
const missing = isFunction(options.missing) ? options.missing : null;
|
|
844
|
-
const missingWarn =
|
|
845
|
-
const fallbackWarn =
|
|
912
|
+
const missingWarn = isWarnOption(options.missingWarn) ? options.missingWarn : true;
|
|
913
|
+
const fallbackWarn = isWarnOption(options.fallbackWarn) ? options.fallbackWarn : true;
|
|
846
914
|
const fallbackFormat = !!options.fallbackFormat;
|
|
847
915
|
const unresolving = !!options.unresolving;
|
|
848
916
|
const postTranslation = isFunction(options.postTranslation) ? options.postTranslation : null;
|
|
@@ -853,9 +921,6 @@ var IntlifyCore = (function(exports) {
|
|
|
853
921
|
const messageResolver = isFunction(options.messageResolver) ? options.messageResolver : _resolver || resolveWithKeyValue;
|
|
854
922
|
const localeFallbacker = isFunction(options.localeFallbacker) ? options.localeFallbacker : _fallbacker || fallbackWithSimple;
|
|
855
923
|
const fallbackContext = isObject(options.fallbackContext) ? options.fallbackContext : void 0;
|
|
856
|
-
const internalOptions = options;
|
|
857
|
-
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters) ? internalOptions.__datetimeFormatters : /* @__PURE__ */ new Map();
|
|
858
|
-
const __numberFormatters = isObject(internalOptions.__numberFormatters) ? internalOptions.__numberFormatters : /* @__PURE__ */ new Map();
|
|
859
924
|
_cid++;
|
|
860
925
|
const context = {
|
|
861
926
|
version,
|
|
@@ -882,9 +947,8 @@ var IntlifyCore = (function(exports) {
|
|
|
882
947
|
};
|
|
883
948
|
context.datetimeFormats = datetimeFormats;
|
|
884
949
|
context.numberFormats = numberFormats;
|
|
885
|
-
context.__datetimeFormatters =
|
|
886
|
-
context.__numberFormatters =
|
|
887
|
-
context.__v_emitter = internalOptions.__v_emitter != null ? internalOptions.__v_emitter : void 0;
|
|
950
|
+
context.__datetimeFormatters = /* @__PURE__ */ new Map();
|
|
951
|
+
context.__numberFormatters = /* @__PURE__ */ new Map();
|
|
888
952
|
return context;
|
|
889
953
|
}
|
|
890
954
|
const createResources = (locale) => ({ [locale]: create() });
|
|
@@ -892,7 +956,7 @@ var IntlifyCore = (function(exports) {
|
|
|
892
956
|
* @version 12.0.0
|
|
893
957
|
*/
|
|
894
958
|
function getLocaleMessage(ctx, locale) {
|
|
895
|
-
const src = ctx.messages
|
|
959
|
+
const src = getOwn(ctx.messages, locale);
|
|
896
960
|
if (src == null) return;
|
|
897
961
|
const dest = create();
|
|
898
962
|
deepCopy(src, dest);
|
|
@@ -902,9 +966,21 @@ var IntlifyCore = (function(exports) {
|
|
|
902
966
|
* @version 12.0.0
|
|
903
967
|
*/
|
|
904
968
|
function setLocaleMessage(ctx, locale, messages) {
|
|
969
|
+
if (isObjectPrototypeKey(locale)) {
|
|
970
|
+
ctx.onWarn(getWarnMessage(CoreWarnCodes.IGNORE_UNSAFE_LOCALE, { locale }));
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
905
973
|
ctx.messages[locale] = messages;
|
|
906
974
|
}
|
|
907
975
|
/** @internal */
|
|
976
|
+
function isFallbackLocale(val) {
|
|
977
|
+
return isString(val) || isArray(val) || isPlainObject(val) || val === false;
|
|
978
|
+
}
|
|
979
|
+
/** @internal */
|
|
980
|
+
function isWarnOption(val) {
|
|
981
|
+
return isBoolean(val) || isRegExp(val);
|
|
982
|
+
}
|
|
983
|
+
/** @internal */
|
|
908
984
|
function isTranslateFallbackWarn(fallback, key) {
|
|
909
985
|
return isRegExp(fallback) ? fallback.test(key) : fallback;
|
|
910
986
|
}
|
|
@@ -921,7 +997,7 @@ var IntlifyCore = (function(exports) {
|
|
|
921
997
|
locale,
|
|
922
998
|
key,
|
|
923
999
|
type,
|
|
924
|
-
groupId:
|
|
1000
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
925
1001
|
});
|
|
926
1002
|
}
|
|
927
1003
|
if (missing !== null) {
|
|
@@ -930,15 +1006,26 @@ var IntlifyCore = (function(exports) {
|
|
|
930
1006
|
} else {
|
|
931
1007
|
if (isTranslateMissingWarn(missingWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, {
|
|
932
1008
|
key,
|
|
933
|
-
locale
|
|
1009
|
+
locale,
|
|
1010
|
+
resource: type === "datetime format" ? "datetime formats" : type === "number format" ? "number formats" : "messages"
|
|
934
1011
|
}));
|
|
935
1012
|
return key;
|
|
936
1013
|
}
|
|
937
1014
|
}
|
|
1015
|
+
function invalidateLocaleChainCache(ctx, fallback) {
|
|
1016
|
+
const context = ctx;
|
|
1017
|
+
if (isObject(fallback)) context.__localeChainObjectCache?.delete(fallback);
|
|
1018
|
+
else context.__localeChainCache?.delete(fallback);
|
|
1019
|
+
}
|
|
938
1020
|
/** @internal */
|
|
939
1021
|
function updateFallbackLocale(ctx, locale, fallback) {
|
|
940
|
-
|
|
941
|
-
|
|
1022
|
+
invalidateLocaleChainCache(ctx, fallback);
|
|
1023
|
+
if (ctx.fallbackContext) invalidateLocaleChainCache(ctx.fallbackContext, fallback);
|
|
1024
|
+
{
|
|
1025
|
+
const { __datetimeFormatters, __numberFormatters } = ctx;
|
|
1026
|
+
__datetimeFormatters.clear();
|
|
1027
|
+
__numberFormatters.clear();
|
|
1028
|
+
}
|
|
942
1029
|
ctx.localeFallbacker(ctx, fallback, locale);
|
|
943
1030
|
}
|
|
944
1031
|
/** @internal */
|
|
@@ -954,41 +1041,14 @@ var IntlifyCore = (function(exports) {
|
|
|
954
1041
|
return false;
|
|
955
1042
|
}
|
|
956
1043
|
//#endregion
|
|
957
|
-
//#region packages/core-base/src/
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
961
|
-
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
962
|
-
};
|
|
963
|
-
//#endregion
|
|
964
|
-
//#region packages/core-base/src/datetime.ts
|
|
965
|
-
function datetime(context, ...args) {
|
|
966
|
-
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
967
|
-
const { __datetimeFormatters } = context;
|
|
968
|
-
if (!Availabilities.dateTimeFormat) {
|
|
969
|
-
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
970
|
-
return "";
|
|
971
|
-
}
|
|
972
|
-
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
973
|
-
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
974
|
-
return "";
|
|
975
|
-
}
|
|
976
|
-
const [key, value, options, overrides] = parseDateTimeArgs(...args);
|
|
977
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
978
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
979
|
-
const part = !!options.part;
|
|
980
|
-
const locale = getLocale(context, options);
|
|
1044
|
+
//#region packages/core-base/src/formatter.ts
|
|
1045
|
+
function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type) {
|
|
1046
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
981
1047
|
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
982
|
-
if (!isString(key) || key === "") return new Intl.DateTimeFormat(locale.replace(/!/g, ""), overrides).format(value);
|
|
983
|
-
let datetimeFormat = {};
|
|
984
|
-
let targetLocale;
|
|
985
|
-
let format = null;
|
|
986
1048
|
let from = locale;
|
|
987
|
-
let to = null;
|
|
988
|
-
const type = "datetime format";
|
|
989
1049
|
for (let i = 0; i < locales.length; i++) {
|
|
990
|
-
targetLocale =
|
|
991
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
1050
|
+
const targetLocale = locales[i];
|
|
1051
|
+
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(type === "datetime format" ? CoreWarnCodes.FALLBACK_TO_DATE_FORMAT : CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
992
1052
|
key,
|
|
993
1053
|
target: targetLocale
|
|
994
1054
|
}));
|
|
@@ -998,26 +1058,119 @@ var IntlifyCore = (function(exports) {
|
|
|
998
1058
|
type,
|
|
999
1059
|
key,
|
|
1000
1060
|
from,
|
|
1001
|
-
to,
|
|
1002
|
-
groupId:
|
|
1061
|
+
to: targetLocale,
|
|
1062
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1003
1063
|
});
|
|
1004
1064
|
}
|
|
1005
|
-
|
|
1006
|
-
format
|
|
1007
|
-
if (isPlainObject(format)) break;
|
|
1065
|
+
const format = getOwn(getOwn(formats, targetLocale) || {}, key);
|
|
1066
|
+
if (isPlainObject(format) && isString(targetLocale)) return targetLocale;
|
|
1008
1067
|
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1009
|
-
from =
|
|
1068
|
+
from = targetLocale;
|
|
1010
1069
|
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1070
|
+
return null;
|
|
1071
|
+
}
|
|
1072
|
+
function isLanguageTag(locale) {
|
|
1073
|
+
try {
|
|
1074
|
+
Intl.getCanonicalLocales(locale);
|
|
1075
|
+
return true;
|
|
1076
|
+
} catch {
|
|
1077
|
+
return false;
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* Create an `Intl.DateTimeFormat` or `Intl.NumberFormat` formatter
|
|
1082
|
+
*
|
|
1083
|
+
* @remarks
|
|
1084
|
+
* `Intl` throws a RangeError for a locale that is not a BCP 47 language tag (e.g. `pt_BR`),
|
|
1085
|
+
* so fall back to the first language tag in the locale chain, or the default locale
|
|
1086
|
+
*/
|
|
1087
|
+
function createFormatter(context, IntlFormatter, locale, options, type) {
|
|
1088
|
+
try {
|
|
1089
|
+
return new IntlFormatter(locale, options);
|
|
1090
|
+
} catch (error) {
|
|
1091
|
+
if (isLanguageTag(locale)) throw error;
|
|
1092
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
1093
|
+
const target = localeFallbacker(context, fallbackLocale, locale).find(isLanguageTag) || "en-US";
|
|
1094
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE, {
|
|
1095
|
+
locale,
|
|
1096
|
+
target,
|
|
1097
|
+
type: type === "datetime format" ? "datetime" : "number"
|
|
1098
|
+
}));
|
|
1099
|
+
return new IntlFormatter(target, options);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
/**
|
|
1103
|
+
* Format a value with the format of a key, or only with the overrides when no key is given
|
|
1104
|
+
*
|
|
1105
|
+
* @remarks
|
|
1106
|
+
* The common part of `datetime()` and `number()`, after they check and parse the arguments
|
|
1107
|
+
*/
|
|
1108
|
+
function formatValue(context, [key, value, options, overrides], IntlFormatter, formatters, formats, type) {
|
|
1109
|
+
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
1110
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1111
|
+
const part = !!options.part;
|
|
1112
|
+
const locale = getLocale(context, options);
|
|
1113
|
+
if (!isString(key) || key === "") {
|
|
1114
|
+
const formatter = createFormatter(context, IntlFormatter, locale.replace(/!/g, ""), overrides, type);
|
|
1115
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1116
|
+
}
|
|
1117
|
+
const targetLocale = resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type);
|
|
1118
|
+
if (!isString(targetLocale)) return context.unresolving ? -1 : key;
|
|
1119
|
+
const format = formats[targetLocale][key];
|
|
1120
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
1121
|
+
let formatter = formatters.get(id);
|
|
1015
1122
|
if (!formatter) {
|
|
1016
|
-
formatter =
|
|
1017
|
-
|
|
1123
|
+
formatter = createFormatter(context, IntlFormatter, targetLocale, assign({}, format, overrides), type);
|
|
1124
|
+
formatters.set(id, formatter);
|
|
1018
1125
|
}
|
|
1019
1126
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1020
1127
|
}
|
|
1128
|
+
function getFormatterCacheKey(locale, key, overrides) {
|
|
1129
|
+
let id = `${locale}__${key}`;
|
|
1130
|
+
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
1131
|
+
return id;
|
|
1132
|
+
}
|
|
1133
|
+
function clearFormatCache(formatters, locale, format) {
|
|
1134
|
+
for (const key in format) {
|
|
1135
|
+
const prefix = `${locale}__${key}`;
|
|
1136
|
+
for (const id of formatters.keys()) if (id === prefix || id.startsWith(`${prefix}__`)) formatters.delete(id);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
function parseFormatArgs(args, options, initialOverrides, optionsKeys) {
|
|
1140
|
+
const [, arg2, arg3, arg4] = args;
|
|
1141
|
+
let overrides = initialOverrides;
|
|
1142
|
+
if (isString(arg2)) options.key = arg2;
|
|
1143
|
+
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1144
|
+
if (optionsKeys.includes(key)) overrides[key] = arg2[key];
|
|
1145
|
+
else options[key] = arg2[key];
|
|
1146
|
+
});
|
|
1147
|
+
if (isString(arg3)) options.locale = arg3;
|
|
1148
|
+
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1149
|
+
if (isPlainObject(arg4)) overrides = arg4;
|
|
1150
|
+
return overrides;
|
|
1151
|
+
}
|
|
1152
|
+
//#endregion
|
|
1153
|
+
//#region packages/core-base/src/intl.ts
|
|
1154
|
+
const intlDefined = typeof Intl !== "undefined";
|
|
1155
|
+
const Availabilities = {
|
|
1156
|
+
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
1157
|
+
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
1158
|
+
};
|
|
1159
|
+
//#endregion
|
|
1160
|
+
//#region packages/core-base/src/datetime.ts
|
|
1161
|
+
function datetime(context, ...args) {
|
|
1162
|
+
const { datetimeFormats, onWarn } = context;
|
|
1163
|
+
const { __datetimeFormatters } = context;
|
|
1164
|
+
if (!Availabilities.dateTimeFormat) {
|
|
1165
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
1166
|
+
return "";
|
|
1167
|
+
}
|
|
1168
|
+
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
1169
|
+
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
1170
|
+
return "";
|
|
1171
|
+
}
|
|
1172
|
+
return formatValue(context, parseDateTimeArgs(...args), Intl.DateTimeFormat, __datetimeFormatters, datetimeFormats, "datetime format");
|
|
1173
|
+
}
|
|
1021
1174
|
/** @internal */
|
|
1022
1175
|
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
1023
1176
|
"localeMatcher",
|
|
@@ -1043,12 +1196,12 @@ var IntlifyCore = (function(exports) {
|
|
|
1043
1196
|
];
|
|
1044
1197
|
/** @internal */
|
|
1045
1198
|
function parseDateTimeArgs(...args) {
|
|
1046
|
-
const [arg1
|
|
1199
|
+
const [arg1] = args;
|
|
1047
1200
|
const options = create();
|
|
1048
|
-
|
|
1201
|
+
const initialOverrides = create();
|
|
1049
1202
|
let value;
|
|
1050
1203
|
if (isString(arg1)) {
|
|
1051
|
-
const matches = arg1.match(
|
|
1204
|
+
const matches = arg1.match(/^(\d{4}-\d{2}-\d{2})(?:(T|\s)(.+))?$/);
|
|
1052
1205
|
if (!matches) throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
1053
1206
|
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();
|
|
1054
1207
|
value = new Date(dateTime);
|
|
@@ -1062,14 +1215,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1062
1215
|
value = arg1;
|
|
1063
1216
|
} else if (isNumber(arg1)) value = arg1;
|
|
1064
1217
|
else throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1065
|
-
|
|
1066
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1067
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
1068
|
-
else options[key] = arg2[key];
|
|
1069
|
-
});
|
|
1070
|
-
if (isString(arg3)) options.locale = arg3;
|
|
1071
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1072
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
1218
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, DATETIME_FORMAT_OPTIONS_KEYS);
|
|
1073
1219
|
return [
|
|
1074
1220
|
options.key || "",
|
|
1075
1221
|
value,
|
|
@@ -1079,17 +1225,12 @@ var IntlifyCore = (function(exports) {
|
|
|
1079
1225
|
}
|
|
1080
1226
|
/** @internal */
|
|
1081
1227
|
function clearDateTimeFormat(ctx, locale, format) {
|
|
1082
|
-
|
|
1083
|
-
for (const key in format) {
|
|
1084
|
-
const id = `${locale}__${key}`;
|
|
1085
|
-
if (!context.__datetimeFormatters.has(id)) continue;
|
|
1086
|
-
context.__datetimeFormatters.delete(id);
|
|
1087
|
-
}
|
|
1228
|
+
clearFormatCache(ctx.__datetimeFormatters, locale, format);
|
|
1088
1229
|
}
|
|
1089
1230
|
//#endregion
|
|
1090
1231
|
//#region packages/core-base/src/number.ts
|
|
1091
1232
|
function number(context, ...args) {
|
|
1092
|
-
const { numberFormats,
|
|
1233
|
+
const { numberFormats, onWarn } = context;
|
|
1093
1234
|
const { __numberFormatters } = context;
|
|
1094
1235
|
if (!Availabilities.numberFormat) {
|
|
1095
1236
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
@@ -1099,50 +1240,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1099
1240
|
onWarn(getWarnMessage(CoreWarnCodes.INVALID_NUMBER_ARGUMENT, { value: String(args[0]) }));
|
|
1100
1241
|
return "";
|
|
1101
1242
|
}
|
|
1102
|
-
|
|
1103
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
1104
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1105
|
-
const part = !!options.part;
|
|
1106
|
-
const locale = getLocale(context, options);
|
|
1107
|
-
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
1108
|
-
if (!isString(key) || key === "") return new Intl.NumberFormat(locale.replace(/!/g, ""), overrides).format(value);
|
|
1109
|
-
let numberFormat = {};
|
|
1110
|
-
let targetLocale;
|
|
1111
|
-
let format = null;
|
|
1112
|
-
let from = locale;
|
|
1113
|
-
let to = null;
|
|
1114
|
-
const type = "number format";
|
|
1115
|
-
for (let i = 0; i < locales.length; i++) {
|
|
1116
|
-
targetLocale = to = locales[i];
|
|
1117
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1118
|
-
key,
|
|
1119
|
-
target: targetLocale
|
|
1120
|
-
}));
|
|
1121
|
-
if (locale !== targetLocale) {
|
|
1122
|
-
const emitter = context.__v_emitter;
|
|
1123
|
-
if (emitter) emitter.emit("fallback", {
|
|
1124
|
-
type,
|
|
1125
|
-
key,
|
|
1126
|
-
from,
|
|
1127
|
-
to,
|
|
1128
|
-
groupId: `${type}:${key}`
|
|
1129
|
-
});
|
|
1130
|
-
}
|
|
1131
|
-
numberFormat = numberFormats[targetLocale] || {};
|
|
1132
|
-
format = numberFormat[key];
|
|
1133
|
-
if (isPlainObject(format)) break;
|
|
1134
|
-
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1135
|
-
from = to;
|
|
1136
|
-
}
|
|
1137
|
-
if (!isPlainObject(format) || !isString(targetLocale)) return unresolving ? -1 : key;
|
|
1138
|
-
let id = `${targetLocale}__${key}`;
|
|
1139
|
-
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
1140
|
-
let formatter = __numberFormatters.get(id);
|
|
1141
|
-
if (!formatter) {
|
|
1142
|
-
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
1143
|
-
__numberFormatters.set(id, formatter);
|
|
1144
|
-
}
|
|
1145
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1243
|
+
return formatValue(context, parseNumberArgs(...args), Intl.NumberFormat, __numberFormatters, numberFormats, "number format");
|
|
1146
1244
|
}
|
|
1147
1245
|
/** @internal */
|
|
1148
1246
|
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
@@ -1169,19 +1267,12 @@ var IntlifyCore = (function(exports) {
|
|
|
1169
1267
|
];
|
|
1170
1268
|
/** @internal */
|
|
1171
1269
|
function parseNumberArgs(...args) {
|
|
1172
|
-
const [arg1
|
|
1270
|
+
const [arg1] = args;
|
|
1173
1271
|
const options = create();
|
|
1174
|
-
|
|
1272
|
+
const initialOverrides = create();
|
|
1175
1273
|
if (!isNumber(arg1)) throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1176
1274
|
const value = arg1;
|
|
1177
|
-
|
|
1178
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1179
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
1180
|
-
else options[key] = arg2[key];
|
|
1181
|
-
});
|
|
1182
|
-
if (isString(arg3)) options.locale = arg3;
|
|
1183
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1184
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
1275
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, NUMBER_FORMAT_OPTIONS_KEYS);
|
|
1185
1276
|
return [
|
|
1186
1277
|
options.key || "",
|
|
1187
1278
|
value,
|
|
@@ -1191,12 +1282,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1191
1282
|
}
|
|
1192
1283
|
/** @internal */
|
|
1193
1284
|
function clearNumberFormat(ctx, locale, format) {
|
|
1194
|
-
|
|
1195
|
-
for (const key in format) {
|
|
1196
|
-
const id = `${locale}__${key}`;
|
|
1197
|
-
if (!context.__numberFormatters.has(id)) continue;
|
|
1198
|
-
context.__numberFormatters.delete(id);
|
|
1199
|
-
}
|
|
1285
|
+
clearFormatCache(ctx.__numberFormatters, locale, format);
|
|
1200
1286
|
}
|
|
1201
1287
|
//#endregion
|
|
1202
1288
|
//#region packages/core-base/src/runtime.ts
|
|
@@ -1219,14 +1305,25 @@ var IntlifyCore = (function(exports) {
|
|
|
1219
1305
|
"other"
|
|
1220
1306
|
];
|
|
1221
1307
|
const intlPluralRulesCache = /* @__PURE__ */ new Map();
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1308
|
+
const INTL_PLURAL_RULES_CACHE_SIZE = 1e3;
|
|
1309
|
+
function createIntlPluralRules(locale) {
|
|
1310
|
+
try {
|
|
1311
|
+
if (Intl.PluralRules.supportedLocalesOf(locale).length === 0) return null;
|
|
1225
1312
|
const rules = new Intl.PluralRules(locale);
|
|
1226
|
-
|
|
1313
|
+
return {
|
|
1227
1314
|
rules,
|
|
1228
1315
|
categories: rules.resolvedOptions().pluralCategories.slice().sort((a, b) => PLURAL_CATEGORY_ORDER.indexOf(a) - PLURAL_CATEGORY_ORDER.indexOf(b))
|
|
1229
1316
|
};
|
|
1317
|
+
} catch {
|
|
1318
|
+
return null;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
function getIntlPluralRules(locale) {
|
|
1322
|
+
let cached = intlPluralRulesCache.get(locale);
|
|
1323
|
+
if (cached === void 0) {
|
|
1324
|
+
cached = createIntlPluralRules(locale);
|
|
1325
|
+
if (cached === null) warn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE, { locale }));
|
|
1326
|
+
if (intlPluralRulesCache.size >= INTL_PLURAL_RULES_CACHE_SIZE) intlPluralRulesCache.delete(intlPluralRulesCache.keys().next().value);
|
|
1230
1327
|
intlPluralRulesCache.set(locale, cached);
|
|
1231
1328
|
}
|
|
1232
1329
|
return cached;
|
|
@@ -1239,10 +1336,11 @@ var IntlifyCore = (function(exports) {
|
|
|
1239
1336
|
const locale = options.locale;
|
|
1240
1337
|
const pluralIndex = getPluralIndex(options);
|
|
1241
1338
|
let resolvedPluralRule;
|
|
1242
|
-
if (isString(locale) && isFunction(options.pluralRules
|
|
1339
|
+
if (isString(locale) && options.pluralRules && isFunction(getOwn(options.pluralRules, locale))) resolvedPluralRule = options.pluralRules[locale];
|
|
1243
1340
|
else if (isString(locale) && typeof Intl !== "undefined" && typeof Intl.PluralRules !== "undefined") resolvedPluralRule = (choice, choicesLength) => {
|
|
1244
|
-
const
|
|
1245
|
-
if (choicesLength > categories.length) return pluralDefault(choice, choicesLength);
|
|
1341
|
+
const intlPluralRules = getIntlPluralRules(locale);
|
|
1342
|
+
if (intlPluralRules === null || choicesLength > intlPluralRules.categories.length) return pluralDefault(choice, choicesLength);
|
|
1343
|
+
const { rules, categories } = intlPluralRules;
|
|
1246
1344
|
const category = rules.select(Math.abs(choice));
|
|
1247
1345
|
const index = categories.indexOf(category);
|
|
1248
1346
|
return Math.min(index === -1 ? choicesLength - 1 : index, choicesLength - 1);
|
|
@@ -1253,16 +1351,16 @@ var IntlifyCore = (function(exports) {
|
|
|
1253
1351
|
const _list = options.list || [];
|
|
1254
1352
|
const list = (index) => _list[index];
|
|
1255
1353
|
const _named = options.named || create();
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
_named
|
|
1259
|
-
|
|
1260
|
-
|
|
1354
|
+
const hasPluralIndex = isNumber(options.pluralIndex);
|
|
1355
|
+
const named = (key) => {
|
|
1356
|
+
const value = isObjectPrototypeKey(key) ? getOwn(_named, key) : _named[key];
|
|
1357
|
+
return hasPluralIndex && value == null && (key === "count" || key === "n") ? options.pluralIndex : value;
|
|
1358
|
+
};
|
|
1261
1359
|
function message(key, useLinked) {
|
|
1262
1360
|
const msg = isFunction(options.messages) ? options.messages(key, !!useLinked) : isObject(options.messages) ? options.messages[key] : false;
|
|
1263
1361
|
return !msg ? options.parent ? options.parent.message(key) : DEFAULT_MESSAGE : msg;
|
|
1264
1362
|
}
|
|
1265
|
-
const _modifier = (name) => options.modifiers ? options.modifiers
|
|
1363
|
+
const _modifier = (name) => options.modifiers ? getOwn(options.modifiers, name) : DEFAULT_MODIFIER;
|
|
1266
1364
|
const normalize = isFunction(options.processor?.normalize) ? options.processor.normalize : DEFAULT_NORMALIZE;
|
|
1267
1365
|
const interpolate = isFunction(options.processor?.interpolate) ? options.processor.interpolate : DEFAULT_INTERPOLATE;
|
|
1268
1366
|
const type = isString(options.processor?.type) ? options.processor.type : DEFAULT_MESSAGE_DATA_TYPE;
|
|
@@ -1282,7 +1380,11 @@ var IntlifyCore = (function(exports) {
|
|
|
1282
1380
|
const ret = message(key, true)(ctx);
|
|
1283
1381
|
const resolved = ret === "" || ret === void 0 ? key : ret;
|
|
1284
1382
|
const msg = type === "vnode" && isArray(resolved) && modifier ? resolved[0] : resolved;
|
|
1285
|
-
|
|
1383
|
+
if (!modifier) return msg;
|
|
1384
|
+
const modify = _modifier(modifier);
|
|
1385
|
+
if (isFunction(modify)) return modify(msg, type);
|
|
1386
|
+
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.`);
|
|
1387
|
+
return msg;
|
|
1286
1388
|
};
|
|
1287
1389
|
const ctx = {
|
|
1288
1390
|
["list"]: list,
|
|
@@ -1293,7 +1395,10 @@ var IntlifyCore = (function(exports) {
|
|
|
1293
1395
|
["type"]: type,
|
|
1294
1396
|
["interpolate"]: interpolate,
|
|
1295
1397
|
["normalize"]: normalize,
|
|
1296
|
-
["values"]: assign(create(), _list, _named
|
|
1398
|
+
["values"]: assign(create(), _list, _named, hasPluralIndex ? {
|
|
1399
|
+
count: named("count"),
|
|
1400
|
+
n: named("n")
|
|
1401
|
+
} : {})
|
|
1297
1402
|
};
|
|
1298
1403
|
return ctx;
|
|
1299
1404
|
}
|
|
@@ -1301,6 +1406,8 @@ var IntlifyCore = (function(exports) {
|
|
|
1301
1406
|
//#region packages/core-base/src/translate.ts
|
|
1302
1407
|
const NOOP_MESSAGE_FUNCTION = () => "";
|
|
1303
1408
|
const isMessageFunction = (val) => isFunction(val);
|
|
1409
|
+
/** @internal */
|
|
1410
|
+
const isMessageFormat = (val) => isString(val) || isMessageAST(val) || isMessageFunction(val);
|
|
1304
1411
|
function translate(context, ...args) {
|
|
1305
1412
|
const [key, options] = parseTranslateArgs(...args);
|
|
1306
1413
|
const locale = getLocale(context, options);
|
|
@@ -1310,21 +1417,18 @@ var IntlifyCore = (function(exports) {
|
|
|
1310
1417
|
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) : [
|
|
1311
1418
|
key,
|
|
1312
1419
|
locale,
|
|
1313
|
-
context.messages
|
|
1420
|
+
getOwn(context.messages, locale) || create()
|
|
1314
1421
|
];
|
|
1315
1422
|
let cacheBaseKey = key;
|
|
1316
|
-
if (!resolvedMessage && !(
|
|
1423
|
+
if (!resolvedMessage && !isMessageFormat(format)) {
|
|
1317
1424
|
const defaultMsgOrKey = isString(options.default) ? options.default : isBoolean(options.default) || context.fallbackFormat ? !context.messageCompiler ? () => key : key : null;
|
|
1318
1425
|
if (context.fallbackFormat || defaultMsgOrKey != null && (isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey))) {
|
|
1319
1426
|
format = defaultMsgOrKey;
|
|
1320
1427
|
cacheBaseKey = format;
|
|
1321
1428
|
}
|
|
1322
1429
|
}
|
|
1323
|
-
if (!resolvedMessage && (!(
|
|
1324
|
-
if (isString(format) && context.messageCompiler == null) {
|
|
1325
|
-
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" : ""}'.`);
|
|
1326
|
-
return key;
|
|
1327
|
-
}
|
|
1430
|
+
if (!resolvedMessage && (!isMessageFormat(format) || !isString(targetLocale))) return context.unresolving ? -1 : key;
|
|
1431
|
+
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.`);
|
|
1328
1432
|
let occurred = false;
|
|
1329
1433
|
const onError = () => {
|
|
1330
1434
|
occurred = true;
|
|
@@ -1332,16 +1436,29 @@ var IntlifyCore = (function(exports) {
|
|
|
1332
1436
|
const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
|
|
1333
1437
|
if (occurred) return format;
|
|
1334
1438
|
const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
|
|
1335
|
-
const
|
|
1439
|
+
const timeline = startTimelineMeasure(context, "evaluation");
|
|
1440
|
+
const messaged = msg(msgContext);
|
|
1441
|
+
if (timeline) timeline.emitter.emit("message-evaluation", {
|
|
1442
|
+
type: "message-evaluation",
|
|
1443
|
+
value: messaged,
|
|
1444
|
+
time: endTimelineMeasure(timeline),
|
|
1445
|
+
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
1446
|
+
});
|
|
1336
1447
|
let ret = context.postTranslation ? context.postTranslation(messaged, key) : messaged;
|
|
1337
1448
|
if (escapeParameter && isString(ret)) ret = sanitizeTranslatedHtml(ret);
|
|
1338
1449
|
return ret;
|
|
1339
1450
|
}
|
|
1340
1451
|
function escapeParams(options) {
|
|
1341
1452
|
if (isArray(options.list)) options.list = options.list.map((item) => isString(item) ? escapeHtml(item) : item);
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1453
|
+
if (isObject(options.named)) {
|
|
1454
|
+
const source = options.named;
|
|
1455
|
+
const named = create();
|
|
1456
|
+
for (const key of Object.keys(source)) {
|
|
1457
|
+
const value = source[key];
|
|
1458
|
+
named[key] = isString(value) ? escapeHtml(value) : value;
|
|
1459
|
+
}
|
|
1460
|
+
options.named = named;
|
|
1461
|
+
}
|
|
1345
1462
|
}
|
|
1346
1463
|
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
1347
1464
|
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
@@ -1364,36 +1481,23 @@ var IntlifyCore = (function(exports) {
|
|
|
1364
1481
|
key,
|
|
1365
1482
|
from,
|
|
1366
1483
|
to: targetLocale,
|
|
1367
|
-
groupId:
|
|
1484
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1368
1485
|
});
|
|
1369
1486
|
}
|
|
1370
|
-
message = messages
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
startTag = "intlify-message-resolve-start";
|
|
1377
|
-
endTag = "intlify-message-resolve-end";
|
|
1378
|
-
mark?.(startTag);
|
|
1379
|
-
}
|
|
1380
|
-
if ((format = resolveValue(message, key)) === null) format = message[key];
|
|
1381
|
-
if (inBrowser) {
|
|
1382
|
-
const end = window.performance.now();
|
|
1383
|
-
const emitter = context.__v_emitter;
|
|
1384
|
-
if (emitter && start && format) emitter.emit("message-resolve", {
|
|
1487
|
+
message = getOwn(messages, targetLocale) || create();
|
|
1488
|
+
const timeline = startTimelineMeasure(context, "resolve");
|
|
1489
|
+
if ((format = resolveValue(message, key)) === null) format = getOwn(message, key);
|
|
1490
|
+
if (timeline) {
|
|
1491
|
+
const time = endTimelineMeasure(timeline);
|
|
1492
|
+
if (format) timeline.emitter.emit("message-resolve", {
|
|
1385
1493
|
type: "message-resolve",
|
|
1386
1494
|
key,
|
|
1387
1495
|
message: format,
|
|
1388
|
-
time
|
|
1389
|
-
groupId:
|
|
1496
|
+
time,
|
|
1497
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1390
1498
|
});
|
|
1391
|
-
if (startTag && endTag && mark && measure) {
|
|
1392
|
-
mark(endTag);
|
|
1393
|
-
measure("intlify message resolve", startTag, endTag);
|
|
1394
|
-
}
|
|
1395
1499
|
}
|
|
1396
|
-
if (
|
|
1500
|
+
if (isMessageFormat(format)) break;
|
|
1397
1501
|
if (!isImplicitFallback(targetLocale, locales)) {
|
|
1398
1502
|
const missingRet = handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1399
1503
|
if (missingRet !== key) format = missingRet;
|
|
@@ -1406,6 +1510,9 @@ var IntlifyCore = (function(exports) {
|
|
|
1406
1510
|
message
|
|
1407
1511
|
];
|
|
1408
1512
|
}
|
|
1513
|
+
function stringifyKey(key) {
|
|
1514
|
+
return isString(key) ? key : isObject(key) ? JSON.stringify(key) : isFunction(key) ? key.name || "function" : "";
|
|
1515
|
+
}
|
|
1409
1516
|
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
1410
1517
|
if (isMessageFunction(format)) {
|
|
1411
1518
|
const msg = format;
|
|
@@ -1419,55 +1526,40 @@ var IntlifyCore = (function(exports) {
|
|
|
1419
1526
|
msg.key = key;
|
|
1420
1527
|
return msg;
|
|
1421
1528
|
}
|
|
1422
|
-
|
|
1423
|
-
let startTag;
|
|
1424
|
-
let endTag;
|
|
1425
|
-
if (inBrowser && mark) {
|
|
1426
|
-
start = window.performance.now();
|
|
1427
|
-
startTag = "intlify-message-compilation-start";
|
|
1428
|
-
endTag = "intlify-message-compilation-end";
|
|
1429
|
-
mark?.(startTag);
|
|
1430
|
-
}
|
|
1529
|
+
const timeline = startTimelineMeasure(context, "compilation");
|
|
1431
1530
|
const msg = context.messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, context.warnHtmlMessage, onError));
|
|
1432
|
-
if (
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
time: end - start,
|
|
1439
|
-
groupId: `translate:${key}`
|
|
1440
|
-
});
|
|
1441
|
-
if (startTag && endTag && mark && measure) {
|
|
1442
|
-
mark(endTag);
|
|
1443
|
-
measure("intlify message compilation", startTag, endTag);
|
|
1444
|
-
}
|
|
1445
|
-
}
|
|
1531
|
+
if (timeline) timeline.emitter.emit("message-compilation", {
|
|
1532
|
+
type: "message-compilation",
|
|
1533
|
+
message: format,
|
|
1534
|
+
time: endTimelineMeasure(timeline),
|
|
1535
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
1536
|
+
});
|
|
1446
1537
|
msg.locale = targetLocale;
|
|
1447
1538
|
msg.key = key;
|
|
1448
1539
|
msg.source = format;
|
|
1449
1540
|
return msg;
|
|
1450
1541
|
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
const
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
context.__v_emitter?.emit("message-evaluation", {
|
|
1461
|
-
type: "message-evaluation",
|
|
1462
|
-
value: messaged,
|
|
1463
|
-
time: end - start,
|
|
1464
|
-
groupId: `translate:${msg.key}`
|
|
1465
|
-
});
|
|
1466
|
-
mark(endTag);
|
|
1467
|
-
measure("intlify message evaluation", startTag, endTag);
|
|
1468
|
-
return messaged;
|
|
1542
|
+
function startTimelineMeasure(context, name) {
|
|
1543
|
+
const emitter = context.__v_emitter;
|
|
1544
|
+
if (!inBrowser || !emitter) return null;
|
|
1545
|
+
const start = window.performance.now();
|
|
1546
|
+
mark?.(`intlify-message-${name}-start`);
|
|
1547
|
+
return {
|
|
1548
|
+
emitter,
|
|
1549
|
+
name,
|
|
1550
|
+
start
|
|
1469
1551
|
};
|
|
1470
1552
|
}
|
|
1553
|
+
function endTimelineMeasure({ name, start }) {
|
|
1554
|
+
const time = window.performance.now() - start;
|
|
1555
|
+
if (mark && measure) {
|
|
1556
|
+
const startTag = `intlify-message-${name}-start`;
|
|
1557
|
+
const endTag = `intlify-message-${name}-end`;
|
|
1558
|
+
mark(endTag);
|
|
1559
|
+
measure(`intlify message ${name}`, startTag, endTag);
|
|
1560
|
+
}
|
|
1561
|
+
return time;
|
|
1562
|
+
}
|
|
1471
1563
|
/** @internal */
|
|
1472
1564
|
function parseTranslateArgs(...args) {
|
|
1473
1565
|
const [arg1, arg2, arg3] = args;
|
|
@@ -1499,12 +1591,11 @@ var IntlifyCore = (function(exports) {
|
|
|
1499
1591
|
error: err.message,
|
|
1500
1592
|
start: err.location && err.location.start.offset,
|
|
1501
1593
|
end: err.location && err.location.end.offset,
|
|
1502
|
-
groupId:
|
|
1594
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
1503
1595
|
});
|
|
1504
1596
|
const message = `Message compilation error: ${err.message}`;
|
|
1505
1597
|
throw new SyntaxError(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
1506
1598
|
}
|
|
1507
|
-
throw err;
|
|
1508
1599
|
},
|
|
1509
1600
|
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
1510
1601
|
};
|
|
@@ -1543,11 +1634,6 @@ var IntlifyCore = (function(exports) {
|
|
|
1543
1634
|
};
|
|
1544
1635
|
}
|
|
1545
1636
|
//#endregion
|
|
1546
|
-
//#region packages/core/src/runtime.ts
|
|
1547
|
-
registerMessageCompiler(compile);
|
|
1548
|
-
registerMessageResolver(resolveValue);
|
|
1549
|
-
registerLocaleFallbacker(fallbackWithLocaleChain);
|
|
1550
|
-
//#endregion
|
|
1551
1637
|
exports.AST_NODE_PROPS_KEYS = AST_NODE_PROPS_KEYS;
|
|
1552
1638
|
exports.CORE_ERROR_CODES_EXTEND_POINT = CORE_ERROR_CODES_EXTEND_POINT;
|
|
1553
1639
|
exports.CORE_WARN_CODES_EXTEND_POINT = CORE_WARN_CODES_EXTEND_POINT;
|
|
@@ -1570,17 +1656,19 @@ var IntlifyCore = (function(exports) {
|
|
|
1570
1656
|
exports.datetime = datetime;
|
|
1571
1657
|
exports.fallbackWithLocaleChain = fallbackWithLocaleChain;
|
|
1572
1658
|
exports.fallbackWithSimple = fallbackWithSimple;
|
|
1573
|
-
exports.getFallbackContext = getFallbackContext;
|
|
1574
1659
|
exports.getLocale = getLocale;
|
|
1575
1660
|
exports.getLocaleMessage = getLocaleMessage;
|
|
1576
1661
|
exports.getWarnMessage = getWarnMessage;
|
|
1577
1662
|
exports.handleMissing = handleMissing;
|
|
1578
1663
|
exports.isAlmostSameLocale = isAlmostSameLocale;
|
|
1664
|
+
exports.isFallbackLocale = isFallbackLocale;
|
|
1579
1665
|
exports.isImplicitFallback = isImplicitFallback;
|
|
1580
1666
|
exports.isMessageAST = isMessageAST;
|
|
1667
|
+
exports.isMessageFormat = isMessageFormat;
|
|
1581
1668
|
exports.isMessageFunction = isMessageFunction;
|
|
1582
1669
|
exports.isTranslateFallbackWarn = isTranslateFallbackWarn;
|
|
1583
1670
|
exports.isTranslateMissingWarn = isTranslateMissingWarn;
|
|
1671
|
+
exports.isWarnOption = isWarnOption;
|
|
1584
1672
|
exports.number = number;
|
|
1585
1673
|
exports.parse = parse;
|
|
1586
1674
|
exports.parseDateTimeArgs = parseDateTimeArgs;
|
|
@@ -1592,7 +1680,6 @@ var IntlifyCore = (function(exports) {
|
|
|
1592
1680
|
exports.resolveLocale = resolveLocale;
|
|
1593
1681
|
exports.resolveValue = resolveValue;
|
|
1594
1682
|
exports.resolveWithKeyValue = resolveWithKeyValue;
|
|
1595
|
-
exports.setFallbackContext = setFallbackContext;
|
|
1596
1683
|
exports.setLocaleMessage = setLocaleMessage;
|
|
1597
1684
|
exports.translate = translate;
|
|
1598
1685
|
exports.updateFallbackLocale = updateFallbackLocale;
|