@intlify/core 12.0.0-alpha.4 → 12.0.0-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.esm-browser.js +473 -377
- package/dist/core.esm-browser.prod.js +640 -568
- package/dist/core.global.js +475 -377
- package/dist/core.global.prod.js +659 -579
- package/dist/core.js +1 -1
- package/dist/core.node.js +1 -1
- package/dist/core.runtime.esm-browser.js +448 -363
- package/dist/core.runtime.esm-browser.prod.js +504 -433
- package/dist/core.runtime.global.js +450 -363
- package/dist/core.runtime.global.prod.js +503 -429
- package/dist/core.runtime.js +1 -5
- package/dist/core.runtime.node.js +1 -5
- package/package.json +4 -4
package/dist/core.global.js
CHANGED
|
@@ -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
|
|
@@ -476,10 +527,13 @@ var IntlifyCore = (function(exports) {
|
|
|
476
527
|
const ctx = context();
|
|
477
528
|
pos.column += offset;
|
|
478
529
|
pos.offset += offset;
|
|
479
|
-
if (onError)
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
530
|
+
if (onError) {
|
|
531
|
+
const err = createCompileError(code, location ? createLocation(ctx.startLoc, pos) : null, {
|
|
532
|
+
domain: ERROR_DOMAIN$1,
|
|
533
|
+
args
|
|
534
|
+
});
|
|
535
|
+
onError(err);
|
|
536
|
+
}
|
|
483
537
|
}
|
|
484
538
|
function getToken(context, type, value) {
|
|
485
539
|
context.endLoc = currentPosition();
|
|
@@ -612,7 +666,7 @@ var IntlifyCore = (function(exports) {
|
|
|
612
666
|
}
|
|
613
667
|
function takeChar(scnr, fn) {
|
|
614
668
|
const ch = scnr.currentChar();
|
|
615
|
-
if (ch === EOF) return
|
|
669
|
+
if (ch === EOF) return;
|
|
616
670
|
if (fn(ch)) {
|
|
617
671
|
scnr.next();
|
|
618
672
|
return ch;
|
|
@@ -669,15 +723,16 @@ var IntlifyCore = (function(exports) {
|
|
|
669
723
|
scnr.next();
|
|
670
724
|
}
|
|
671
725
|
} else if (ch === "{" || ch === "}" || ch === "@" || ch === "|" || !ch) break;
|
|
672
|
-
else if (ch === " " || ch === "\n")
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
726
|
+
else if (ch === " " || ch === "\n") {
|
|
727
|
+
if (isTextStart(scnr)) {
|
|
728
|
+
buf += ch;
|
|
729
|
+
scnr.next();
|
|
730
|
+
} else if (isPluralStart(scnr)) break;
|
|
731
|
+
else {
|
|
732
|
+
buf += ch;
|
|
733
|
+
scnr.next();
|
|
734
|
+
}
|
|
735
|
+
} else {
|
|
681
736
|
buf += ch;
|
|
682
737
|
scnr.next();
|
|
683
738
|
}
|
|
@@ -925,7 +980,6 @@ var IntlifyCore = (function(exports) {
|
|
|
925
980
|
return token;
|
|
926
981
|
}
|
|
927
982
|
if (isTextStart(scnr)) return getToken(context, 0, readText(scnr));
|
|
928
|
-
break;
|
|
929
983
|
}
|
|
930
984
|
return token;
|
|
931
985
|
}
|
|
@@ -973,10 +1027,13 @@ var IntlifyCore = (function(exports) {
|
|
|
973
1027
|
const end = tokenzer.currentPosition();
|
|
974
1028
|
end.offset += offset;
|
|
975
1029
|
end.column += offset;
|
|
976
|
-
if (onError)
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
1030
|
+
if (onError) {
|
|
1031
|
+
const err = createCompileError(code, location ? createLocation(start, end) : null, {
|
|
1032
|
+
domain: ERROR_DOMAIN,
|
|
1033
|
+
args
|
|
1034
|
+
});
|
|
1035
|
+
onError(err);
|
|
1036
|
+
}
|
|
980
1037
|
}
|
|
981
1038
|
function startNode(type, offset, loc) {
|
|
982
1039
|
const node = { type };
|
|
@@ -1313,7 +1370,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1313
1370
|
code: ""
|
|
1314
1371
|
};
|
|
1315
1372
|
}
|
|
1316
|
-
/*
|
|
1373
|
+
/* #__NO_SIDE_EFFECTS__ */
|
|
1317
1374
|
function compile(message, context) {
|
|
1318
1375
|
if (isString(message)) {
|
|
1319
1376
|
checkHtmlMessage(message, isBoolean(context.warnHtmlMessage) ? context.warnHtmlMessage : true);
|
|
@@ -1330,7 +1387,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1330
1387
|
return !detectError ? compileCache[cacheKey] = msg : msg;
|
|
1331
1388
|
} else {
|
|
1332
1389
|
if (!isMessageAST(message)) {
|
|
1333
|
-
warn(`the message
|
|
1390
|
+
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.`);
|
|
1334
1391
|
return (() => message);
|
|
1335
1392
|
}
|
|
1336
1393
|
const cacheKey = message.cacheKey;
|
|
@@ -1347,12 +1404,11 @@ var IntlifyCore = (function(exports) {
|
|
|
1347
1404
|
INVALID_ARGUMENT: 17,
|
|
1348
1405
|
INVALID_DATE_ARGUMENT: 18,
|
|
1349
1406
|
INVALID_ISO_DATE_ARGUMENT: 19,
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
NOT_SUPPORT_LOCALE_TYPE: 23
|
|
1407
|
+
NOT_SUPPORT_LOCALE_PROMISE_VALUE: 20,
|
|
1408
|
+
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 21,
|
|
1409
|
+
NOT_SUPPORT_LOCALE_TYPE: 22
|
|
1354
1410
|
};
|
|
1355
|
-
const CORE_ERROR_CODES_EXTEND_POINT =
|
|
1411
|
+
const CORE_ERROR_CODES_EXTEND_POINT = 23;
|
|
1356
1412
|
function createCoreError(code) {
|
|
1357
1413
|
return createCompileError(code, null, { messages: errorMessages });
|
|
1358
1414
|
}
|
|
@@ -1361,7 +1417,6 @@ var IntlifyCore = (function(exports) {
|
|
|
1361
1417
|
[CoreErrorCodes.INVALID_ARGUMENT]: "Invalid arguments",
|
|
1362
1418
|
[CoreErrorCodes.INVALID_DATE_ARGUMENT]: "The date provided is an invalid Date object.Make sure your Date represents a valid date.",
|
|
1363
1419
|
[CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: "The argument provided is not a valid ISO date string",
|
|
1364
|
-
[CoreErrorCodes.NOT_SUPPORT_NON_STRING_MESSAGE]: "Not support non-string message",
|
|
1365
1420
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE]: "cannot support promise value",
|
|
1366
1421
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: "cannot support async function",
|
|
1367
1422
|
[CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: "cannot support locale type"
|
|
@@ -1376,13 +1431,14 @@ var IntlifyCore = (function(exports) {
|
|
|
1376
1431
|
/** @internal */
|
|
1377
1432
|
function resolveLocale(locale) {
|
|
1378
1433
|
if (isString(locale)) return locale;
|
|
1379
|
-
else if (isFunction(locale))
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1434
|
+
else if (isFunction(locale)) {
|
|
1435
|
+
if (locale.resolvedOnce && _resolveLocale != null) return _resolveLocale;
|
|
1436
|
+
else if (locale.constructor.name === "Function") {
|
|
1437
|
+
const resolve = locale();
|
|
1438
|
+
if (isPromise(resolve)) throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
|
|
1439
|
+
return _resolveLocale = resolve;
|
|
1440
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
|
|
1441
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
|
|
1386
1442
|
}
|
|
1387
1443
|
/**
|
|
1388
1444
|
* Fallback with simple implemenation
|
|
@@ -1390,7 +1446,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1390
1446
|
* @remarks
|
|
1391
1447
|
* A fallback locale function implemented with a simple fallback algorithm.
|
|
1392
1448
|
*
|
|
1393
|
-
* Basically,
|
|
1449
|
+
* 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.
|
|
1394
1450
|
*
|
|
1395
1451
|
* @param ctx - A {@link CoreContext | context}
|
|
1396
1452
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
@@ -1401,7 +1457,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1401
1457
|
* @VueI18nGeneral
|
|
1402
1458
|
*/
|
|
1403
1459
|
function fallbackWithSimple(_ctx, fallback, start) {
|
|
1404
|
-
return [
|
|
1460
|
+
return [.../* @__PURE__ */ new Set([start, ...isArray(fallback) ? fallback : isObject(fallback) ? Object.keys(fallback).filter((locale) => locale !== "default") : isString(fallback) ? [fallback] : [start]])];
|
|
1405
1461
|
}
|
|
1406
1462
|
/**
|
|
1407
1463
|
* Fallback with locale chain
|
|
@@ -1409,6 +1465,9 @@ var IntlifyCore = (function(exports) {
|
|
|
1409
1465
|
* @remarks
|
|
1410
1466
|
* A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
|
|
1411
1467
|
*
|
|
1468
|
+
* Chains are cached per fallback identity. If you mutate an object or array fallback in place,
|
|
1469
|
+
* pass a new object or array instead so that the chain is recomputed.
|
|
1470
|
+
*
|
|
1412
1471
|
* @param ctx - A {@link CoreContext | context}
|
|
1413
1472
|
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
1414
1473
|
* @param start - A starting {@link Locale | locale}
|
|
@@ -1422,17 +1481,31 @@ var IntlifyCore = (function(exports) {
|
|
|
1422
1481
|
function fallbackWithLocaleChain(ctx, fallback, start) {
|
|
1423
1482
|
const startLocale = isString(start) ? start : DEFAULT_LOCALE;
|
|
1424
1483
|
const context = ctx;
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
context.__localeChainCache.
|
|
1484
|
+
let chains;
|
|
1485
|
+
if (isObject(fallback)) {
|
|
1486
|
+
if (!context.__localeChainObjectCache) context.__localeChainObjectCache = /* @__PURE__ */ new WeakMap();
|
|
1487
|
+
chains = context.__localeChainObjectCache.get(fallback);
|
|
1488
|
+
if (!chains) {
|
|
1489
|
+
chains = /* @__PURE__ */ new Map();
|
|
1490
|
+
context.__localeChainObjectCache.set(fallback, chains);
|
|
1491
|
+
}
|
|
1492
|
+
} else {
|
|
1493
|
+
if (!context.__localeChainCache) context.__localeChainCache = /* @__PURE__ */ new Map();
|
|
1494
|
+
chains = context.__localeChainCache.get(fallback);
|
|
1495
|
+
if (!chains) {
|
|
1496
|
+
chains = /* @__PURE__ */ new Map();
|
|
1497
|
+
context.__localeChainCache.set(fallback, chains);
|
|
1498
|
+
}
|
|
1435
1499
|
}
|
|
1500
|
+
const cached = chains.get(startLocale);
|
|
1501
|
+
if (cached) return cached;
|
|
1502
|
+
const chain = [];
|
|
1503
|
+
let block = [start];
|
|
1504
|
+
while (isArray(block)) block = appendBlockToChain(chain, block, fallback);
|
|
1505
|
+
const defaults = isArray(fallback) || !isPlainObject(fallback) ? fallback : fallback["default"] ? fallback["default"] : null;
|
|
1506
|
+
block = isString(defaults) ? [defaults] : defaults;
|
|
1507
|
+
if (isArray(block)) appendBlockToChain(chain, block, false);
|
|
1508
|
+
chains.set(startLocale, chain);
|
|
1436
1509
|
return chain;
|
|
1437
1510
|
}
|
|
1438
1511
|
function appendBlockToChain(chain, block, blocks) {
|
|
@@ -1645,7 +1718,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1645
1718
|
* @VueI18nGeneral
|
|
1646
1719
|
*/
|
|
1647
1720
|
function resolveWithKeyValue(obj, path) {
|
|
1648
|
-
return isObject(obj) ? obj
|
|
1721
|
+
return isObject(obj) ? getOwn(obj, path) : null;
|
|
1649
1722
|
}
|
|
1650
1723
|
/**
|
|
1651
1724
|
* message resolver
|
|
@@ -1700,12 +1773,15 @@ var IntlifyCore = (function(exports) {
|
|
|
1700
1773
|
FALLBACK_TO_DATE_FORMAT: 6,
|
|
1701
1774
|
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
|
|
1702
1775
|
INVALID_NUMBER_ARGUMENT: 8,
|
|
1703
|
-
INVALID_DATE_ARGUMENT: 9
|
|
1776
|
+
INVALID_DATE_ARGUMENT: 9,
|
|
1777
|
+
IGNORE_UNSAFE_LOCALE: 10,
|
|
1778
|
+
FALLBACK_TO_DEFAULT_PLURAL_RULE: 11,
|
|
1779
|
+
FALLBACK_FROM_INVALID_LOCALE: 12
|
|
1704
1780
|
};
|
|
1705
|
-
const CORE_WARN_CODES_EXTEND_POINT =
|
|
1781
|
+
const CORE_WARN_CODES_EXTEND_POINT = 13;
|
|
1706
1782
|
/** @internal */
|
|
1707
1783
|
const warnMessages = {
|
|
1708
|
-
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale
|
|
1784
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale {resource}.`,
|
|
1709
1785
|
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
1710
1786
|
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
1711
1787
|
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
@@ -1713,7 +1789,10 @@ var IntlifyCore = (function(exports) {
|
|
|
1713
1789
|
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
1714
1790
|
[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.`,
|
|
1715
1791
|
[CoreWarnCodes.INVALID_NUMBER_ARGUMENT]: `Invalid argument for number formatting: expected a number but received '{value}'.`,
|
|
1716
|
-
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'
|
|
1792
|
+
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'.`,
|
|
1793
|
+
[CoreWarnCodes.IGNORE_UNSAFE_LOCALE]: `Ignore '{locale}' locale: it is a property name of Object.prototype`,
|
|
1794
|
+
[CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE]: `Fall back to the default plural rule for '{locale}' locale: Intl.PluralRules does not support it`,
|
|
1795
|
+
[CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE]: `Fall back to '{target}' locale for {type} formatting with '{locale}' locale: it is not a BCP 47 language tag`
|
|
1717
1796
|
};
|
|
1718
1797
|
function getWarnMessage(code, ...args) {
|
|
1719
1798
|
return format$1(warnMessages[code], ...args);
|
|
@@ -1724,7 +1803,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1724
1803
|
* Intlify core-base version
|
|
1725
1804
|
* @internal
|
|
1726
1805
|
*/
|
|
1727
|
-
const VERSION = "12.0.0-alpha.
|
|
1806
|
+
const VERSION = "12.0.0-alpha.6";
|
|
1728
1807
|
const NOT_RESOLVED = -1;
|
|
1729
1808
|
const DEFAULT_LOCALE = "en-US";
|
|
1730
1809
|
const MISSING_RESOLVE_VALUE = "";
|
|
@@ -1784,26 +1863,21 @@ var IntlifyCore = (function(exports) {
|
|
|
1784
1863
|
warnOnce(`registerLocaleFallbacker is deprecated. Use 'localeFallbacker' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
1785
1864
|
_fallbacker = fallbacker;
|
|
1786
1865
|
}
|
|
1787
|
-
let _fallbackContext = null;
|
|
1788
|
-
const setFallbackContext = (context) => {
|
|
1789
|
-
_fallbackContext = context;
|
|
1790
|
-
};
|
|
1791
|
-
const getFallbackContext = () => _fallbackContext;
|
|
1792
1866
|
let _cid = 0;
|
|
1793
1867
|
function createCoreContext(options = {}) {
|
|
1794
1868
|
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
1795
1869
|
const version = isString(options.version) ? options.version : VERSION;
|
|
1796
1870
|
const locale = isString(options.locale) || isFunction(options.locale) ? options.locale : DEFAULT_LOCALE;
|
|
1797
1871
|
const _locale = isFunction(locale) ? DEFAULT_LOCALE : locale;
|
|
1798
|
-
const fallbackLocale =
|
|
1872
|
+
const fallbackLocale = isFallbackLocale(options.fallbackLocale) ? options.fallbackLocale : _locale;
|
|
1799
1873
|
const messages = isPlainObject(options.messages) ? options.messages : createResources(_locale);
|
|
1800
1874
|
const datetimeFormats = isPlainObject(options.datetimeFormats) ? options.datetimeFormats : createResources(_locale);
|
|
1801
1875
|
const numberFormats = isPlainObject(options.numberFormats) ? options.numberFormats : createResources(_locale);
|
|
1802
1876
|
const modifiers = assign(create(), options.modifiers, getDefaultLinkedModifiers());
|
|
1803
1877
|
const pluralRules = options.pluralRules || create();
|
|
1804
1878
|
const missing = isFunction(options.missing) ? options.missing : null;
|
|
1805
|
-
const missingWarn =
|
|
1806
|
-
const fallbackWarn =
|
|
1879
|
+
const missingWarn = isWarnOption(options.missingWarn) ? options.missingWarn : true;
|
|
1880
|
+
const fallbackWarn = isWarnOption(options.fallbackWarn) ? options.fallbackWarn : true;
|
|
1807
1881
|
const fallbackFormat = !!options.fallbackFormat;
|
|
1808
1882
|
const unresolving = !!options.unresolving;
|
|
1809
1883
|
const postTranslation = isFunction(options.postTranslation) ? options.postTranslation : null;
|
|
@@ -1814,9 +1888,6 @@ var IntlifyCore = (function(exports) {
|
|
|
1814
1888
|
const messageResolver = isFunction(options.messageResolver) ? options.messageResolver : _resolver || resolveWithKeyValue;
|
|
1815
1889
|
const localeFallbacker = isFunction(options.localeFallbacker) ? options.localeFallbacker : _fallbacker || fallbackWithSimple;
|
|
1816
1890
|
const fallbackContext = isObject(options.fallbackContext) ? options.fallbackContext : void 0;
|
|
1817
|
-
const internalOptions = options;
|
|
1818
|
-
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters) ? internalOptions.__datetimeFormatters : /* @__PURE__ */ new Map();
|
|
1819
|
-
const __numberFormatters = isObject(internalOptions.__numberFormatters) ? internalOptions.__numberFormatters : /* @__PURE__ */ new Map();
|
|
1820
1891
|
_cid++;
|
|
1821
1892
|
const context = {
|
|
1822
1893
|
version,
|
|
@@ -1843,9 +1914,8 @@ var IntlifyCore = (function(exports) {
|
|
|
1843
1914
|
};
|
|
1844
1915
|
context.datetimeFormats = datetimeFormats;
|
|
1845
1916
|
context.numberFormats = numberFormats;
|
|
1846
|
-
context.__datetimeFormatters =
|
|
1847
|
-
context.__numberFormatters =
|
|
1848
|
-
context.__v_emitter = internalOptions.__v_emitter != null ? internalOptions.__v_emitter : void 0;
|
|
1917
|
+
context.__datetimeFormatters = /* @__PURE__ */ new Map();
|
|
1918
|
+
context.__numberFormatters = /* @__PURE__ */ new Map();
|
|
1849
1919
|
return context;
|
|
1850
1920
|
}
|
|
1851
1921
|
const createResources = (locale) => ({ [locale]: create() });
|
|
@@ -1853,7 +1923,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1853
1923
|
* @version 12.0.0
|
|
1854
1924
|
*/
|
|
1855
1925
|
function getLocaleMessage(ctx, locale) {
|
|
1856
|
-
const src = ctx.messages
|
|
1926
|
+
const src = getOwn(ctx.messages, locale);
|
|
1857
1927
|
if (src == null) return;
|
|
1858
1928
|
const dest = create();
|
|
1859
1929
|
deepCopy(src, dest);
|
|
@@ -1863,9 +1933,21 @@ var IntlifyCore = (function(exports) {
|
|
|
1863
1933
|
* @version 12.0.0
|
|
1864
1934
|
*/
|
|
1865
1935
|
function setLocaleMessage(ctx, locale, messages) {
|
|
1936
|
+
if (isObjectPrototypeKey(locale)) {
|
|
1937
|
+
ctx.onWarn(getWarnMessage(CoreWarnCodes.IGNORE_UNSAFE_LOCALE, { locale }));
|
|
1938
|
+
return;
|
|
1939
|
+
}
|
|
1866
1940
|
ctx.messages[locale] = messages;
|
|
1867
1941
|
}
|
|
1868
1942
|
/** @internal */
|
|
1943
|
+
function isFallbackLocale(val) {
|
|
1944
|
+
return isString(val) || isArray(val) || isPlainObject(val) || val === false;
|
|
1945
|
+
}
|
|
1946
|
+
/** @internal */
|
|
1947
|
+
function isWarnOption(val) {
|
|
1948
|
+
return isBoolean(val) || isRegExp(val);
|
|
1949
|
+
}
|
|
1950
|
+
/** @internal */
|
|
1869
1951
|
function isTranslateFallbackWarn(fallback, key) {
|
|
1870
1952
|
return isRegExp(fallback) ? fallback.test(key) : fallback;
|
|
1871
1953
|
}
|
|
@@ -1882,7 +1964,7 @@ var IntlifyCore = (function(exports) {
|
|
|
1882
1964
|
locale,
|
|
1883
1965
|
key,
|
|
1884
1966
|
type,
|
|
1885
|
-
groupId:
|
|
1967
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1886
1968
|
});
|
|
1887
1969
|
}
|
|
1888
1970
|
if (missing !== null) {
|
|
@@ -1891,15 +1973,26 @@ var IntlifyCore = (function(exports) {
|
|
|
1891
1973
|
} else {
|
|
1892
1974
|
if (isTranslateMissingWarn(missingWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, {
|
|
1893
1975
|
key,
|
|
1894
|
-
locale
|
|
1976
|
+
locale,
|
|
1977
|
+
resource: type === "datetime format" ? "datetime formats" : type === "number format" ? "number formats" : "messages"
|
|
1895
1978
|
}));
|
|
1896
1979
|
return key;
|
|
1897
1980
|
}
|
|
1898
1981
|
}
|
|
1982
|
+
function invalidateLocaleChainCache(ctx, fallback) {
|
|
1983
|
+
const context = ctx;
|
|
1984
|
+
if (isObject(fallback)) context.__localeChainObjectCache?.delete(fallback);
|
|
1985
|
+
else context.__localeChainCache?.delete(fallback);
|
|
1986
|
+
}
|
|
1899
1987
|
/** @internal */
|
|
1900
1988
|
function updateFallbackLocale(ctx, locale, fallback) {
|
|
1901
|
-
|
|
1902
|
-
|
|
1989
|
+
invalidateLocaleChainCache(ctx, fallback);
|
|
1990
|
+
if (ctx.fallbackContext) invalidateLocaleChainCache(ctx.fallbackContext, fallback);
|
|
1991
|
+
{
|
|
1992
|
+
const { __datetimeFormatters, __numberFormatters } = ctx;
|
|
1993
|
+
__datetimeFormatters.clear();
|
|
1994
|
+
__numberFormatters.clear();
|
|
1995
|
+
}
|
|
1903
1996
|
ctx.localeFallbacker(ctx, fallback, locale);
|
|
1904
1997
|
}
|
|
1905
1998
|
/** @internal */
|
|
@@ -1915,41 +2008,14 @@ var IntlifyCore = (function(exports) {
|
|
|
1915
2008
|
return false;
|
|
1916
2009
|
}
|
|
1917
2010
|
//#endregion
|
|
1918
|
-
//#region packages/core-base/src/
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
1922
|
-
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
1923
|
-
};
|
|
1924
|
-
//#endregion
|
|
1925
|
-
//#region packages/core-base/src/datetime.ts
|
|
1926
|
-
function datetime(context, ...args) {
|
|
1927
|
-
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
1928
|
-
const { __datetimeFormatters } = context;
|
|
1929
|
-
if (!Availabilities.dateTimeFormat) {
|
|
1930
|
-
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
1931
|
-
return "";
|
|
1932
|
-
}
|
|
1933
|
-
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
1934
|
-
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
1935
|
-
return "";
|
|
1936
|
-
}
|
|
1937
|
-
const [key, value, options, overrides] = parseDateTimeArgs(...args);
|
|
1938
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
1939
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1940
|
-
const part = !!options.part;
|
|
1941
|
-
const locale = getLocale(context, options);
|
|
2011
|
+
//#region packages/core-base/src/formatter.ts
|
|
2012
|
+
function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type) {
|
|
2013
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
1942
2014
|
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
1943
|
-
if (!isString(key) || key === "") return new Intl.DateTimeFormat(locale.replace(/!/g, ""), overrides).format(value);
|
|
1944
|
-
let datetimeFormat = {};
|
|
1945
|
-
let targetLocale;
|
|
1946
|
-
let format = null;
|
|
1947
2015
|
let from = locale;
|
|
1948
|
-
let to = null;
|
|
1949
|
-
const type = "datetime format";
|
|
1950
2016
|
for (let i = 0; i < locales.length; i++) {
|
|
1951
|
-
targetLocale =
|
|
1952
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
2017
|
+
const targetLocale = locales[i];
|
|
2018
|
+
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(type === "datetime format" ? CoreWarnCodes.FALLBACK_TO_DATE_FORMAT : CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1953
2019
|
key,
|
|
1954
2020
|
target: targetLocale
|
|
1955
2021
|
}));
|
|
@@ -1959,26 +2025,119 @@ var IntlifyCore = (function(exports) {
|
|
|
1959
2025
|
type,
|
|
1960
2026
|
key,
|
|
1961
2027
|
from,
|
|
1962
|
-
to,
|
|
1963
|
-
groupId:
|
|
2028
|
+
to: targetLocale,
|
|
2029
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1964
2030
|
});
|
|
1965
2031
|
}
|
|
1966
|
-
|
|
1967
|
-
format
|
|
1968
|
-
if (isPlainObject(format)) break;
|
|
2032
|
+
const format = getOwn(getOwn(formats, targetLocale) || {}, key);
|
|
2033
|
+
if (isPlainObject(format) && isString(targetLocale)) return targetLocale;
|
|
1969
2034
|
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1970
|
-
from =
|
|
2035
|
+
from = targetLocale;
|
|
1971
2036
|
}
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
2037
|
+
return null;
|
|
2038
|
+
}
|
|
2039
|
+
function isLanguageTag(locale) {
|
|
2040
|
+
try {
|
|
2041
|
+
Intl.getCanonicalLocales(locale);
|
|
2042
|
+
return true;
|
|
2043
|
+
} catch {
|
|
2044
|
+
return false;
|
|
2045
|
+
}
|
|
2046
|
+
}
|
|
2047
|
+
/**
|
|
2048
|
+
* Create an `Intl.DateTimeFormat` or `Intl.NumberFormat` formatter
|
|
2049
|
+
*
|
|
2050
|
+
* @remarks
|
|
2051
|
+
* `Intl` throws a RangeError for a locale that is not a BCP 47 language tag (e.g. `pt_BR`),
|
|
2052
|
+
* so fall back to the first language tag in the locale chain, or the default locale
|
|
2053
|
+
*/
|
|
2054
|
+
function createFormatter(context, IntlFormatter, locale, options, type) {
|
|
2055
|
+
try {
|
|
2056
|
+
return new IntlFormatter(locale, options);
|
|
2057
|
+
} catch (error) {
|
|
2058
|
+
if (isLanguageTag(locale)) throw error;
|
|
2059
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
2060
|
+
const target = localeFallbacker(context, fallbackLocale, locale).find(isLanguageTag) || "en-US";
|
|
2061
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_FROM_INVALID_LOCALE, {
|
|
2062
|
+
locale,
|
|
2063
|
+
target,
|
|
2064
|
+
type: type === "datetime format" ? "datetime" : "number"
|
|
2065
|
+
}));
|
|
2066
|
+
return new IntlFormatter(target, options);
|
|
2067
|
+
}
|
|
2068
|
+
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Format a value with the format of a key, or only with the overrides when no key is given
|
|
2071
|
+
*
|
|
2072
|
+
* @remarks
|
|
2073
|
+
* The common part of `datetime()` and `number()`, after they check and parse the arguments
|
|
2074
|
+
*/
|
|
2075
|
+
function formatValue(context, [key, value, options, overrides], IntlFormatter, formatters, formats, type) {
|
|
2076
|
+
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2077
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2078
|
+
const part = !!options.part;
|
|
2079
|
+
const locale = getLocale(context, options);
|
|
2080
|
+
if (!isString(key) || key === "") {
|
|
2081
|
+
const formatter = createFormatter(context, IntlFormatter, locale.replace(/!/g, ""), overrides, type);
|
|
2082
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2083
|
+
}
|
|
2084
|
+
const targetLocale = resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type);
|
|
2085
|
+
if (!isString(targetLocale)) return context.unresolving ? -1 : key;
|
|
2086
|
+
const format = formats[targetLocale][key];
|
|
2087
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
2088
|
+
let formatter = formatters.get(id);
|
|
1976
2089
|
if (!formatter) {
|
|
1977
|
-
formatter =
|
|
1978
|
-
|
|
2090
|
+
formatter = createFormatter(context, IntlFormatter, targetLocale, assign({}, format, overrides), type);
|
|
2091
|
+
formatters.set(id, formatter);
|
|
1979
2092
|
}
|
|
1980
2093
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1981
2094
|
}
|
|
2095
|
+
function getFormatterCacheKey(locale, key, overrides) {
|
|
2096
|
+
let id = `${locale}__${key}`;
|
|
2097
|
+
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
2098
|
+
return id;
|
|
2099
|
+
}
|
|
2100
|
+
function clearFormatCache(formatters, locale, format) {
|
|
2101
|
+
for (const key in format) {
|
|
2102
|
+
const prefix = `${locale}__${key}`;
|
|
2103
|
+
for (const id of formatters.keys()) if (id === prefix || id.startsWith(`${prefix}__`)) formatters.delete(id);
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
function parseFormatArgs(args, options, initialOverrides, optionsKeys) {
|
|
2107
|
+
const [, arg2, arg3, arg4] = args;
|
|
2108
|
+
let overrides = initialOverrides;
|
|
2109
|
+
if (isString(arg2)) options.key = arg2;
|
|
2110
|
+
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
2111
|
+
if (optionsKeys.includes(key)) overrides[key] = arg2[key];
|
|
2112
|
+
else options[key] = arg2[key];
|
|
2113
|
+
});
|
|
2114
|
+
if (isString(arg3)) options.locale = arg3;
|
|
2115
|
+
else if (isPlainObject(arg3)) overrides = arg3;
|
|
2116
|
+
if (isPlainObject(arg4)) overrides = arg4;
|
|
2117
|
+
return overrides;
|
|
2118
|
+
}
|
|
2119
|
+
//#endregion
|
|
2120
|
+
//#region packages/core-base/src/intl.ts
|
|
2121
|
+
const intlDefined = typeof Intl !== "undefined";
|
|
2122
|
+
const Availabilities = {
|
|
2123
|
+
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
2124
|
+
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
2125
|
+
};
|
|
2126
|
+
//#endregion
|
|
2127
|
+
//#region packages/core-base/src/datetime.ts
|
|
2128
|
+
function datetime(context, ...args) {
|
|
2129
|
+
const { datetimeFormats, onWarn } = context;
|
|
2130
|
+
const { __datetimeFormatters } = context;
|
|
2131
|
+
if (!Availabilities.dateTimeFormat) {
|
|
2132
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
2133
|
+
return "";
|
|
2134
|
+
}
|
|
2135
|
+
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
2136
|
+
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
2137
|
+
return "";
|
|
2138
|
+
}
|
|
2139
|
+
return formatValue(context, parseDateTimeArgs(...args), Intl.DateTimeFormat, __datetimeFormatters, datetimeFormats, "datetime format");
|
|
2140
|
+
}
|
|
1982
2141
|
/** @internal */
|
|
1983
2142
|
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
1984
2143
|
"localeMatcher",
|
|
@@ -2004,12 +2163,12 @@ var IntlifyCore = (function(exports) {
|
|
|
2004
2163
|
];
|
|
2005
2164
|
/** @internal */
|
|
2006
2165
|
function parseDateTimeArgs(...args) {
|
|
2007
|
-
const [arg1
|
|
2166
|
+
const [arg1] = args;
|
|
2008
2167
|
const options = create();
|
|
2009
|
-
|
|
2168
|
+
const initialOverrides = create();
|
|
2010
2169
|
let value;
|
|
2011
2170
|
if (isString(arg1)) {
|
|
2012
|
-
const matches = arg1.match(
|
|
2171
|
+
const matches = arg1.match(/^(\d{4}-\d{2}-\d{2})(?:(T|\s)(.+))?$/);
|
|
2013
2172
|
if (!matches) throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
2014
2173
|
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();
|
|
2015
2174
|
value = new Date(dateTime);
|
|
@@ -2023,14 +2182,7 @@ var IntlifyCore = (function(exports) {
|
|
|
2023
2182
|
value = arg1;
|
|
2024
2183
|
} else if (isNumber(arg1)) value = arg1;
|
|
2025
2184
|
else throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
2026
|
-
|
|
2027
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
2028
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
2029
|
-
else options[key] = arg2[key];
|
|
2030
|
-
});
|
|
2031
|
-
if (isString(arg3)) options.locale = arg3;
|
|
2032
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
2033
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
2185
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, DATETIME_FORMAT_OPTIONS_KEYS);
|
|
2034
2186
|
return [
|
|
2035
2187
|
options.key || "",
|
|
2036
2188
|
value,
|
|
@@ -2040,17 +2192,12 @@ var IntlifyCore = (function(exports) {
|
|
|
2040
2192
|
}
|
|
2041
2193
|
/** @internal */
|
|
2042
2194
|
function clearDateTimeFormat(ctx, locale, format) {
|
|
2043
|
-
|
|
2044
|
-
for (const key in format) {
|
|
2045
|
-
const id = `${locale}__${key}`;
|
|
2046
|
-
if (!context.__datetimeFormatters.has(id)) continue;
|
|
2047
|
-
context.__datetimeFormatters.delete(id);
|
|
2048
|
-
}
|
|
2195
|
+
clearFormatCache(ctx.__datetimeFormatters, locale, format);
|
|
2049
2196
|
}
|
|
2050
2197
|
//#endregion
|
|
2051
2198
|
//#region packages/core-base/src/number.ts
|
|
2052
2199
|
function number(context, ...args) {
|
|
2053
|
-
const { numberFormats,
|
|
2200
|
+
const { numberFormats, onWarn } = context;
|
|
2054
2201
|
const { __numberFormatters } = context;
|
|
2055
2202
|
if (!Availabilities.numberFormat) {
|
|
2056
2203
|
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
@@ -2060,50 +2207,7 @@ var IntlifyCore = (function(exports) {
|
|
|
2060
2207
|
onWarn(getWarnMessage(CoreWarnCodes.INVALID_NUMBER_ARGUMENT, { value: String(args[0]) }));
|
|
2061
2208
|
return "";
|
|
2062
2209
|
}
|
|
2063
|
-
|
|
2064
|
-
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
2065
|
-
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
2066
|
-
const part = !!options.part;
|
|
2067
|
-
const locale = getLocale(context, options);
|
|
2068
|
-
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
2069
|
-
if (!isString(key) || key === "") return new Intl.NumberFormat(locale.replace(/!/g, ""), overrides).format(value);
|
|
2070
|
-
let numberFormat = {};
|
|
2071
|
-
let targetLocale;
|
|
2072
|
-
let format = null;
|
|
2073
|
-
let from = locale;
|
|
2074
|
-
let to = null;
|
|
2075
|
-
const type = "number format";
|
|
2076
|
-
for (let i = 0; i < locales.length; i++) {
|
|
2077
|
-
targetLocale = to = locales[i];
|
|
2078
|
-
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
2079
|
-
key,
|
|
2080
|
-
target: targetLocale
|
|
2081
|
-
}));
|
|
2082
|
-
if (locale !== targetLocale) {
|
|
2083
|
-
const emitter = context.__v_emitter;
|
|
2084
|
-
if (emitter) emitter.emit("fallback", {
|
|
2085
|
-
type,
|
|
2086
|
-
key,
|
|
2087
|
-
from,
|
|
2088
|
-
to,
|
|
2089
|
-
groupId: `${type}:${key}`
|
|
2090
|
-
});
|
|
2091
|
-
}
|
|
2092
|
-
numberFormat = numberFormats[targetLocale] || {};
|
|
2093
|
-
format = numberFormat[key];
|
|
2094
|
-
if (isPlainObject(format)) break;
|
|
2095
|
-
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
2096
|
-
from = to;
|
|
2097
|
-
}
|
|
2098
|
-
if (!isPlainObject(format) || !isString(targetLocale)) return unresolving ? -1 : key;
|
|
2099
|
-
let id = `${targetLocale}__${key}`;
|
|
2100
|
-
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
2101
|
-
let formatter = __numberFormatters.get(id);
|
|
2102
|
-
if (!formatter) {
|
|
2103
|
-
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
2104
|
-
__numberFormatters.set(id, formatter);
|
|
2105
|
-
}
|
|
2106
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
2210
|
+
return formatValue(context, parseNumberArgs(...args), Intl.NumberFormat, __numberFormatters, numberFormats, "number format");
|
|
2107
2211
|
}
|
|
2108
2212
|
/** @internal */
|
|
2109
2213
|
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
@@ -2130,19 +2234,12 @@ var IntlifyCore = (function(exports) {
|
|
|
2130
2234
|
];
|
|
2131
2235
|
/** @internal */
|
|
2132
2236
|
function parseNumberArgs(...args) {
|
|
2133
|
-
const [arg1
|
|
2237
|
+
const [arg1] = args;
|
|
2134
2238
|
const options = create();
|
|
2135
|
-
|
|
2239
|
+
const initialOverrides = create();
|
|
2136
2240
|
if (!isNumber(arg1)) throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
2137
2241
|
const value = arg1;
|
|
2138
|
-
|
|
2139
|
-
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
2140
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) overrides[key] = arg2[key];
|
|
2141
|
-
else options[key] = arg2[key];
|
|
2142
|
-
});
|
|
2143
|
-
if (isString(arg3)) options.locale = arg3;
|
|
2144
|
-
else if (isPlainObject(arg3)) overrides = arg3;
|
|
2145
|
-
if (isPlainObject(arg4)) overrides = arg4;
|
|
2242
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, NUMBER_FORMAT_OPTIONS_KEYS);
|
|
2146
2243
|
return [
|
|
2147
2244
|
options.key || "",
|
|
2148
2245
|
value,
|
|
@@ -2152,12 +2249,7 @@ var IntlifyCore = (function(exports) {
|
|
|
2152
2249
|
}
|
|
2153
2250
|
/** @internal */
|
|
2154
2251
|
function clearNumberFormat(ctx, locale, format) {
|
|
2155
|
-
|
|
2156
|
-
for (const key in format) {
|
|
2157
|
-
const id = `${locale}__${key}`;
|
|
2158
|
-
if (!context.__numberFormatters.has(id)) continue;
|
|
2159
|
-
context.__numberFormatters.delete(id);
|
|
2160
|
-
}
|
|
2252
|
+
clearFormatCache(ctx.__numberFormatters, locale, format);
|
|
2161
2253
|
}
|
|
2162
2254
|
//#endregion
|
|
2163
2255
|
//#region packages/core-base/src/runtime.ts
|
|
@@ -2180,14 +2272,25 @@ var IntlifyCore = (function(exports) {
|
|
|
2180
2272
|
"other"
|
|
2181
2273
|
];
|
|
2182
2274
|
const intlPluralRulesCache = /* @__PURE__ */ new Map();
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2275
|
+
const INTL_PLURAL_RULES_CACHE_SIZE = 1e3;
|
|
2276
|
+
function createIntlPluralRules(locale) {
|
|
2277
|
+
try {
|
|
2278
|
+
if (Intl.PluralRules.supportedLocalesOf(locale).length === 0) return null;
|
|
2186
2279
|
const rules = new Intl.PluralRules(locale);
|
|
2187
|
-
|
|
2280
|
+
return {
|
|
2188
2281
|
rules,
|
|
2189
2282
|
categories: rules.resolvedOptions().pluralCategories.slice().sort((a, b) => PLURAL_CATEGORY_ORDER.indexOf(a) - PLURAL_CATEGORY_ORDER.indexOf(b))
|
|
2190
2283
|
};
|
|
2284
|
+
} catch {
|
|
2285
|
+
return null;
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
function getIntlPluralRules(locale) {
|
|
2289
|
+
let cached = intlPluralRulesCache.get(locale);
|
|
2290
|
+
if (cached === void 0) {
|
|
2291
|
+
cached = createIntlPluralRules(locale);
|
|
2292
|
+
if (cached === null) warn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DEFAULT_PLURAL_RULE, { locale }));
|
|
2293
|
+
if (intlPluralRulesCache.size >= INTL_PLURAL_RULES_CACHE_SIZE) intlPluralRulesCache.delete(intlPluralRulesCache.keys().next().value);
|
|
2191
2294
|
intlPluralRulesCache.set(locale, cached);
|
|
2192
2295
|
}
|
|
2193
2296
|
return cached;
|
|
@@ -2200,10 +2303,11 @@ var IntlifyCore = (function(exports) {
|
|
|
2200
2303
|
const locale = options.locale;
|
|
2201
2304
|
const pluralIndex = getPluralIndex(options);
|
|
2202
2305
|
let resolvedPluralRule;
|
|
2203
|
-
if (isString(locale) && isFunction(options.pluralRules
|
|
2306
|
+
if (isString(locale) && options.pluralRules && isFunction(getOwn(options.pluralRules, locale))) resolvedPluralRule = options.pluralRules[locale];
|
|
2204
2307
|
else if (isString(locale) && typeof Intl !== "undefined" && typeof Intl.PluralRules !== "undefined") resolvedPluralRule = (choice, choicesLength) => {
|
|
2205
|
-
const
|
|
2206
|
-
if (choicesLength > categories.length) return pluralDefault(choice, choicesLength);
|
|
2308
|
+
const intlPluralRules = getIntlPluralRules(locale);
|
|
2309
|
+
if (intlPluralRules === null || choicesLength > intlPluralRules.categories.length) return pluralDefault(choice, choicesLength);
|
|
2310
|
+
const { rules, categories } = intlPluralRules;
|
|
2207
2311
|
const category = rules.select(Math.abs(choice));
|
|
2208
2312
|
const index = categories.indexOf(category);
|
|
2209
2313
|
return Math.min(index === -1 ? choicesLength - 1 : index, choicesLength - 1);
|
|
@@ -2214,16 +2318,16 @@ var IntlifyCore = (function(exports) {
|
|
|
2214
2318
|
const _list = options.list || [];
|
|
2215
2319
|
const list = (index) => _list[index];
|
|
2216
2320
|
const _named = options.named || create();
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
_named
|
|
2220
|
-
|
|
2221
|
-
|
|
2321
|
+
const hasPluralIndex = isNumber(options.pluralIndex);
|
|
2322
|
+
const named = (key) => {
|
|
2323
|
+
const value = isObjectPrototypeKey(key) ? getOwn(_named, key) : _named[key];
|
|
2324
|
+
return hasPluralIndex && value == null && (key === "count" || key === "n") ? options.pluralIndex : value;
|
|
2325
|
+
};
|
|
2222
2326
|
function message(key, useLinked) {
|
|
2223
2327
|
const msg = isFunction(options.messages) ? options.messages(key, !!useLinked) : isObject(options.messages) ? options.messages[key] : false;
|
|
2224
2328
|
return !msg ? options.parent ? options.parent.message(key) : DEFAULT_MESSAGE : msg;
|
|
2225
2329
|
}
|
|
2226
|
-
const _modifier = (name) => options.modifiers ? options.modifiers
|
|
2330
|
+
const _modifier = (name) => options.modifiers ? getOwn(options.modifiers, name) : DEFAULT_MODIFIER;
|
|
2227
2331
|
const normalize = isFunction(options.processor?.normalize) ? options.processor.normalize : DEFAULT_NORMALIZE;
|
|
2228
2332
|
const interpolate = isFunction(options.processor?.interpolate) ? options.processor.interpolate : DEFAULT_INTERPOLATE;
|
|
2229
2333
|
const type = isString(options.processor?.type) ? options.processor.type : DEFAULT_MESSAGE_DATA_TYPE;
|
|
@@ -2243,7 +2347,11 @@ var IntlifyCore = (function(exports) {
|
|
|
2243
2347
|
const ret = message(key, true)(ctx);
|
|
2244
2348
|
const resolved = ret === "" || ret === void 0 ? key : ret;
|
|
2245
2349
|
const msg = type === "vnode" && isArray(resolved) && modifier ? resolved[0] : resolved;
|
|
2246
|
-
|
|
2350
|
+
if (!modifier) return msg;
|
|
2351
|
+
const modify = _modifier(modifier);
|
|
2352
|
+
if (isFunction(modify)) return modify(msg, type);
|
|
2353
|
+
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.`);
|
|
2354
|
+
return msg;
|
|
2247
2355
|
};
|
|
2248
2356
|
const ctx = {
|
|
2249
2357
|
["list"]: list,
|
|
@@ -2254,7 +2362,10 @@ var IntlifyCore = (function(exports) {
|
|
|
2254
2362
|
["type"]: type,
|
|
2255
2363
|
["interpolate"]: interpolate,
|
|
2256
2364
|
["normalize"]: normalize,
|
|
2257
|
-
["values"]: assign(create(), _list, _named
|
|
2365
|
+
["values"]: assign(create(), _list, _named, hasPluralIndex ? {
|
|
2366
|
+
count: named("count"),
|
|
2367
|
+
n: named("n")
|
|
2368
|
+
} : {})
|
|
2258
2369
|
};
|
|
2259
2370
|
return ctx;
|
|
2260
2371
|
}
|
|
@@ -2262,6 +2373,8 @@ var IntlifyCore = (function(exports) {
|
|
|
2262
2373
|
//#region packages/core-base/src/translate.ts
|
|
2263
2374
|
const NOOP_MESSAGE_FUNCTION = () => "";
|
|
2264
2375
|
const isMessageFunction = (val) => isFunction(val);
|
|
2376
|
+
/** @internal */
|
|
2377
|
+
const isMessageFormat = (val) => isString(val) || isMessageAST(val) || isMessageFunction(val);
|
|
2265
2378
|
function translate(context, ...args) {
|
|
2266
2379
|
const [key, options] = parseTranslateArgs(...args);
|
|
2267
2380
|
const locale = getLocale(context, options);
|
|
@@ -2271,21 +2384,18 @@ var IntlifyCore = (function(exports) {
|
|
|
2271
2384
|
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) : [
|
|
2272
2385
|
key,
|
|
2273
2386
|
locale,
|
|
2274
|
-
context.messages
|
|
2387
|
+
getOwn(context.messages, locale) || create()
|
|
2275
2388
|
];
|
|
2276
2389
|
let cacheBaseKey = key;
|
|
2277
|
-
if (!resolvedMessage && !(
|
|
2390
|
+
if (!resolvedMessage && !isMessageFormat(format)) {
|
|
2278
2391
|
const defaultMsgOrKey = isString(options.default) ? options.default : isBoolean(options.default) || context.fallbackFormat ? !context.messageCompiler ? () => key : key : null;
|
|
2279
2392
|
if (context.fallbackFormat || defaultMsgOrKey != null && (isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey))) {
|
|
2280
2393
|
format = defaultMsgOrKey;
|
|
2281
2394
|
cacheBaseKey = format;
|
|
2282
2395
|
}
|
|
2283
2396
|
}
|
|
2284
|
-
if (!resolvedMessage && (!(
|
|
2285
|
-
if (isString(format) && context.messageCompiler == null) {
|
|
2286
|
-
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" : ""}'.`);
|
|
2287
|
-
return key;
|
|
2288
|
-
}
|
|
2397
|
+
if (!resolvedMessage && (!isMessageFormat(format) || !isString(targetLocale))) return context.unresolving ? -1 : key;
|
|
2398
|
+
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.`);
|
|
2289
2399
|
let occurred = false;
|
|
2290
2400
|
const onError = () => {
|
|
2291
2401
|
occurred = true;
|
|
@@ -2293,16 +2403,29 @@ var IntlifyCore = (function(exports) {
|
|
|
2293
2403
|
const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
|
|
2294
2404
|
if (occurred) return format;
|
|
2295
2405
|
const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
|
|
2296
|
-
const
|
|
2406
|
+
const timeline = startTimelineMeasure(context, "evaluation");
|
|
2407
|
+
const messaged = msg(msgContext);
|
|
2408
|
+
if (timeline) timeline.emitter.emit("message-evaluation", {
|
|
2409
|
+
type: "message-evaluation",
|
|
2410
|
+
value: messaged,
|
|
2411
|
+
time: endTimelineMeasure(timeline),
|
|
2412
|
+
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
2413
|
+
});
|
|
2297
2414
|
let ret = context.postTranslation ? context.postTranslation(messaged, key) : messaged;
|
|
2298
2415
|
if (escapeParameter && isString(ret)) ret = sanitizeTranslatedHtml(ret);
|
|
2299
2416
|
return ret;
|
|
2300
2417
|
}
|
|
2301
2418
|
function escapeParams(options) {
|
|
2302
2419
|
if (isArray(options.list)) options.list = options.list.map((item) => isString(item) ? escapeHtml(item) : item);
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2420
|
+
if (isObject(options.named)) {
|
|
2421
|
+
const source = options.named;
|
|
2422
|
+
const named = create();
|
|
2423
|
+
for (const key of Object.keys(source)) {
|
|
2424
|
+
const value = source[key];
|
|
2425
|
+
named[key] = isString(value) ? escapeHtml(value) : value;
|
|
2426
|
+
}
|
|
2427
|
+
options.named = named;
|
|
2428
|
+
}
|
|
2306
2429
|
}
|
|
2307
2430
|
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
2308
2431
|
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
@@ -2325,36 +2448,23 @@ var IntlifyCore = (function(exports) {
|
|
|
2325
2448
|
key,
|
|
2326
2449
|
from,
|
|
2327
2450
|
to: targetLocale,
|
|
2328
|
-
groupId:
|
|
2451
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
2329
2452
|
});
|
|
2330
2453
|
}
|
|
2331
|
-
message = messages
|
|
2332
|
-
|
|
2333
|
-
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
startTag = "intlify-message-resolve-start";
|
|
2338
|
-
endTag = "intlify-message-resolve-end";
|
|
2339
|
-
mark?.(startTag);
|
|
2340
|
-
}
|
|
2341
|
-
if ((format = resolveValue(message, key)) === null) format = message[key];
|
|
2342
|
-
if (inBrowser) {
|
|
2343
|
-
const end = window.performance.now();
|
|
2344
|
-
const emitter = context.__v_emitter;
|
|
2345
|
-
if (emitter && start && format) emitter.emit("message-resolve", {
|
|
2454
|
+
message = getOwn(messages, targetLocale) || create();
|
|
2455
|
+
const timeline = startTimelineMeasure(context, "resolve");
|
|
2456
|
+
if ((format = resolveValue(message, key)) === null) format = getOwn(message, key);
|
|
2457
|
+
if (timeline) {
|
|
2458
|
+
const time = endTimelineMeasure(timeline);
|
|
2459
|
+
if (format) timeline.emitter.emit("message-resolve", {
|
|
2346
2460
|
type: "message-resolve",
|
|
2347
2461
|
key,
|
|
2348
2462
|
message: format,
|
|
2349
|
-
time
|
|
2350
|
-
groupId:
|
|
2463
|
+
time,
|
|
2464
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
2351
2465
|
});
|
|
2352
|
-
if (startTag && endTag && mark && measure) {
|
|
2353
|
-
mark(endTag);
|
|
2354
|
-
measure("intlify message resolve", startTag, endTag);
|
|
2355
|
-
}
|
|
2356
2466
|
}
|
|
2357
|
-
if (
|
|
2467
|
+
if (isMessageFormat(format)) break;
|
|
2358
2468
|
if (!isImplicitFallback(targetLocale, locales)) {
|
|
2359
2469
|
const missingRet = handleMissing(context, key, targetLocale, missingWarn, type);
|
|
2360
2470
|
if (missingRet !== key) format = missingRet;
|
|
@@ -2367,6 +2477,9 @@ var IntlifyCore = (function(exports) {
|
|
|
2367
2477
|
message
|
|
2368
2478
|
];
|
|
2369
2479
|
}
|
|
2480
|
+
function stringifyKey(key) {
|
|
2481
|
+
return isString(key) ? key : isObject(key) ? JSON.stringify(key) : isFunction(key) ? key.name || "function" : "";
|
|
2482
|
+
}
|
|
2370
2483
|
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
2371
2484
|
if (isMessageFunction(format)) {
|
|
2372
2485
|
const msg = format;
|
|
@@ -2380,55 +2493,40 @@ var IntlifyCore = (function(exports) {
|
|
|
2380
2493
|
msg.key = key;
|
|
2381
2494
|
return msg;
|
|
2382
2495
|
}
|
|
2383
|
-
|
|
2384
|
-
let startTag;
|
|
2385
|
-
let endTag;
|
|
2386
|
-
if (inBrowser && mark) {
|
|
2387
|
-
start = window.performance.now();
|
|
2388
|
-
startTag = "intlify-message-compilation-start";
|
|
2389
|
-
endTag = "intlify-message-compilation-end";
|
|
2390
|
-
mark?.(startTag);
|
|
2391
|
-
}
|
|
2496
|
+
const timeline = startTimelineMeasure(context, "compilation");
|
|
2392
2497
|
const msg = context.messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, context.warnHtmlMessage, onError));
|
|
2393
|
-
if (
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
time: end - start,
|
|
2400
|
-
groupId: `translate:${key}`
|
|
2401
|
-
});
|
|
2402
|
-
if (startTag && endTag && mark && measure) {
|
|
2403
|
-
mark(endTag);
|
|
2404
|
-
measure("intlify message compilation", startTag, endTag);
|
|
2405
|
-
}
|
|
2406
|
-
}
|
|
2498
|
+
if (timeline) timeline.emitter.emit("message-compilation", {
|
|
2499
|
+
type: "message-compilation",
|
|
2500
|
+
message: format,
|
|
2501
|
+
time: endTimelineMeasure(timeline),
|
|
2502
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
2503
|
+
});
|
|
2407
2504
|
msg.locale = targetLocale;
|
|
2408
2505
|
msg.key = key;
|
|
2409
2506
|
msg.source = format;
|
|
2410
2507
|
return msg;
|
|
2411
2508
|
}
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
const
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
context.__v_emitter?.emit("message-evaluation", {
|
|
2422
|
-
type: "message-evaluation",
|
|
2423
|
-
value: messaged,
|
|
2424
|
-
time: end - start,
|
|
2425
|
-
groupId: `translate:${msg.key}`
|
|
2426
|
-
});
|
|
2427
|
-
mark(endTag);
|
|
2428
|
-
measure("intlify message evaluation", startTag, endTag);
|
|
2429
|
-
return messaged;
|
|
2509
|
+
function startTimelineMeasure(context, name) {
|
|
2510
|
+
const emitter = context.__v_emitter;
|
|
2511
|
+
if (!inBrowser || !emitter) return null;
|
|
2512
|
+
const start = window.performance.now();
|
|
2513
|
+
mark?.(`intlify-message-${name}-start`);
|
|
2514
|
+
return {
|
|
2515
|
+
emitter,
|
|
2516
|
+
name,
|
|
2517
|
+
start
|
|
2430
2518
|
};
|
|
2431
2519
|
}
|
|
2520
|
+
function endTimelineMeasure({ name, start }) {
|
|
2521
|
+
const time = window.performance.now() - start;
|
|
2522
|
+
if (mark && measure) {
|
|
2523
|
+
const startTag = `intlify-message-${name}-start`;
|
|
2524
|
+
const endTag = `intlify-message-${name}-end`;
|
|
2525
|
+
mark(endTag);
|
|
2526
|
+
measure(`intlify message ${name}`, startTag, endTag);
|
|
2527
|
+
}
|
|
2528
|
+
return time;
|
|
2529
|
+
}
|
|
2432
2530
|
/** @internal */
|
|
2433
2531
|
function parseTranslateArgs(...args) {
|
|
2434
2532
|
const [arg1, arg2, arg3] = args;
|
|
@@ -2460,12 +2558,11 @@ var IntlifyCore = (function(exports) {
|
|
|
2460
2558
|
error: err.message,
|
|
2461
2559
|
start: err.location && err.location.start.offset,
|
|
2462
2560
|
end: err.location && err.location.end.offset,
|
|
2463
|
-
groupId:
|
|
2561
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
2464
2562
|
});
|
|
2465
2563
|
const message = `Message compilation error: ${err.message}`;
|
|
2466
2564
|
throw new SyntaxError(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
2467
2565
|
}
|
|
2468
|
-
throw err;
|
|
2469
2566
|
},
|
|
2470
2567
|
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
2471
2568
|
};
|
|
@@ -2526,17 +2623,19 @@ var IntlifyCore = (function(exports) {
|
|
|
2526
2623
|
exports.datetime = datetime;
|
|
2527
2624
|
exports.fallbackWithLocaleChain = fallbackWithLocaleChain;
|
|
2528
2625
|
exports.fallbackWithSimple = fallbackWithSimple;
|
|
2529
|
-
exports.getFallbackContext = getFallbackContext;
|
|
2530
2626
|
exports.getLocale = getLocale;
|
|
2531
2627
|
exports.getLocaleMessage = getLocaleMessage;
|
|
2532
2628
|
exports.getWarnMessage = getWarnMessage;
|
|
2533
2629
|
exports.handleMissing = handleMissing;
|
|
2534
2630
|
exports.isAlmostSameLocale = isAlmostSameLocale;
|
|
2631
|
+
exports.isFallbackLocale = isFallbackLocale;
|
|
2535
2632
|
exports.isImplicitFallback = isImplicitFallback;
|
|
2536
2633
|
exports.isMessageAST = isMessageAST;
|
|
2634
|
+
exports.isMessageFormat = isMessageFormat;
|
|
2537
2635
|
exports.isMessageFunction = isMessageFunction;
|
|
2538
2636
|
exports.isTranslateFallbackWarn = isTranslateFallbackWarn;
|
|
2539
2637
|
exports.isTranslateMissingWarn = isTranslateMissingWarn;
|
|
2638
|
+
exports.isWarnOption = isWarnOption;
|
|
2540
2639
|
exports.number = number;
|
|
2541
2640
|
exports.parse = parse;
|
|
2542
2641
|
exports.parseDateTimeArgs = parseDateTimeArgs;
|
|
@@ -2548,7 +2647,6 @@ var IntlifyCore = (function(exports) {
|
|
|
2548
2647
|
exports.resolveLocale = resolveLocale;
|
|
2549
2648
|
exports.resolveValue = resolveValue;
|
|
2550
2649
|
exports.resolveWithKeyValue = resolveWithKeyValue;
|
|
2551
|
-
exports.setFallbackContext = setFallbackContext;
|
|
2552
2650
|
exports.setLocaleMessage = setLocaleMessage;
|
|
2553
2651
|
exports.translate = translate;
|
|
2554
2652
|
exports.updateFallbackLocale = updateFallbackLocale;
|