@intlify/core 12.0.0-alpha.3 → 12.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/core.d.ts +1 -4
- package/dist/core.esm-browser.js +2327 -3053
- package/dist/core.esm-browser.prod.js +1402 -6
- package/dist/core.esm-bundler.js +1 -1
- package/dist/core.global.js +2617 -3344
- package/dist/core.global.prod.js +1424 -6
- package/dist/core.js +5 -30
- package/dist/core.node.js +5 -30
- package/dist/core.runtime.esm-browser.js +1387 -1854
- package/dist/core.runtime.esm-browser.prod.js +822 -6
- package/dist/core.runtime.esm-bundler.js +1 -1
- package/dist/core.runtime.global.js +1655 -2123
- package/dist/core.runtime.global.prod.js +824 -6
- package/dist/core.runtime.js +17 -25
- package/dist/core.runtime.node.js +17 -25
- package/package.json +40 -40
|
@@ -1,2074 +1,1607 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* core v12.0.0-alpha.3
|
|
3
|
-
* (c) 2016-present kazuya kawaguchi and contributors
|
|
4
|
-
* Released under the MIT License.
|
|
5
|
-
*/
|
|
6
1
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
* @intlify/core v12.0.0-alpha.5
|
|
3
|
+
* (c) 2016-present kazuya kawaguchi and contributors
|
|
4
|
+
* @license MIT
|
|
5
|
+
**/
|
|
6
|
+
//#region packages/shared/src/utils.ts
|
|
7
|
+
/**
|
|
8
|
+
* Original Utilities
|
|
9
|
+
* written by kazuya kawaguchi
|
|
10
|
+
*/
|
|
11
|
+
const inBrowser = typeof window !== "undefined";
|
|
11
12
|
let mark;
|
|
12
13
|
let measure;
|
|
13
14
|
{
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
const RE_ARGS = /\{([0-9a-zA-Z]+)\}/g;
|
|
32
|
-
/* eslint-disable */
|
|
15
|
+
const perf = inBrowser && window.performance;
|
|
16
|
+
if (perf && perf.mark && perf.measure && perf.clearMarks && perf.clearMeasures) {
|
|
17
|
+
mark = (tag) => {
|
|
18
|
+
perf.mark(tag);
|
|
19
|
+
};
|
|
20
|
+
measure = (name, startTag, endTag) => {
|
|
21
|
+
perf.measure(name, startTag, endTag);
|
|
22
|
+
perf.clearMarks(startTag);
|
|
23
|
+
perf.clearMarks(endTag);
|
|
24
|
+
perf.clearMeasures(name);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const RE_ARGS = /\{([0-9a-z]+)\}/gi;
|
|
33
29
|
function format$1(message, ...args) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const friendlyJSONstringify = (json) => JSON.stringify(json)
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const isDate = (val) => toTypeString(val) ===
|
|
51
|
-
const isRegExp = (val) => toTypeString(val) ===
|
|
52
|
-
const
|
|
30
|
+
if (args.length === 1 && isObject(args[0])) args = args[0];
|
|
31
|
+
if (!args || !args.hasOwnProperty) args = {};
|
|
32
|
+
return message.replace(RE_ARGS, (_match, identifier) => {
|
|
33
|
+
return args.hasOwnProperty(identifier) ? args[identifier] : "";
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const generateFormatCacheKey = (locale, key, source) => friendlyJSONstringify({
|
|
37
|
+
l: locale,
|
|
38
|
+
k: key,
|
|
39
|
+
s: source
|
|
40
|
+
});
|
|
41
|
+
const friendlyJSONstringify = (json) => JSON.stringify(json).replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029").replace(/\u0027/g, "\\u0027");
|
|
42
|
+
const isNumber = (val) => typeof val === "number" && isFinite(val);
|
|
43
|
+
function toDevtoolsGroupId(type, key) {
|
|
44
|
+
return isString(key) || isNumber(key) ? `${type}:${key}` : type;
|
|
45
|
+
}
|
|
46
|
+
const isDate = (val) => toTypeString(val) === "[object Date]";
|
|
47
|
+
const isRegExp = (val) => toTypeString(val) === "[object RegExp]";
|
|
48
|
+
const isKeylessObject = (val) => Object.keys(val).length === 0;
|
|
53
49
|
const assign = Object.assign;
|
|
54
50
|
const _create = Object.create;
|
|
55
51
|
const create = (obj = null) => _create(obj);
|
|
56
|
-
function escapeHtml(rawText) {
|
|
57
|
-
return rawText
|
|
58
|
-
.replace(/</g, '<')
|
|
59
|
-
.replace(/>/g, '>')
|
|
60
|
-
.replace(/"/g, '"')
|
|
61
|
-
.replace(/'/g, ''');
|
|
62
|
-
}
|
|
63
52
|
const hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
64
53
|
function hasOwn(obj, key) {
|
|
65
|
-
|
|
54
|
+
return hasOwnProperty.call(obj, key);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get `obj[key]` only when `key` is an own property of `obj`, so that a key such as `constructor`
|
|
58
|
+
* does not read a property inherited from `Object.prototype`.
|
|
59
|
+
* The property is read before the check, so that a reactive `obj` tracks `key` even when it is missing.
|
|
60
|
+
*/
|
|
61
|
+
function getOwn(obj, key) {
|
|
62
|
+
const value = obj[key];
|
|
63
|
+
return hasOwn(obj, key) ? value : void 0;
|
|
66
64
|
}
|
|
67
|
-
/* eslint-enable */
|
|
68
65
|
/**
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
66
|
+
* Whether `key` is a property name of `Object.prototype`, such as `__proto__`, `constructor` or `toString`.
|
|
67
|
+
* On a plain object, such a key reads a built-in, and assigning to `__proto__` replaces the prototype.
|
|
68
|
+
*/
|
|
69
|
+
function isObjectPrototypeKey(key) {
|
|
70
|
+
return hasOwn(Object.prototype, key);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Useful Utilities By Evan you
|
|
74
|
+
* Modified by kazuya kawaguchi
|
|
75
|
+
* MIT License
|
|
76
|
+
* https://github.com/vuejs/vue-next/blob/master/packages/shared/src/index.ts
|
|
77
|
+
* https://github.com/vuejs/vue-next/blob/master/packages/shared/src/codeframe.ts
|
|
78
|
+
*/
|
|
75
79
|
const isArray = Array.isArray;
|
|
76
|
-
const isFunction = (val) => typeof val ===
|
|
77
|
-
const isString = (val) => typeof val ===
|
|
78
|
-
const isBoolean = (val) => typeof val ===
|
|
79
|
-
|
|
80
|
-
const isObject = (val) => val !== null && typeof val === 'object';
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
+
const isFunction = (val) => typeof val === "function";
|
|
81
|
+
const isString = (val) => typeof val === "string";
|
|
82
|
+
const isBoolean = (val) => typeof val === "boolean";
|
|
83
|
+
const isObject = (val) => val !== null && typeof val === "object";
|
|
82
84
|
const isPromise = (val) => {
|
|
83
|
-
|
|
85
|
+
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
|
|
84
86
|
};
|
|
85
87
|
const objectToString = Object.prototype.toString;
|
|
86
88
|
const toTypeString = (value) => objectToString.call(value);
|
|
87
|
-
const isPlainObject = (val) => toTypeString(val) ===
|
|
88
|
-
// for converting list and named values to displayed strings.
|
|
89
|
+
const isPlainObject = (val) => toTypeString(val) === "[object Object]";
|
|
89
90
|
const toDisplayString = (val) => {
|
|
90
|
-
|
|
91
|
-
? ''
|
|
92
|
-
: isArray(val) || (isPlainObject(val) && val.toString === objectToString)
|
|
93
|
-
? JSON.stringify(val, null, 2)
|
|
94
|
-
: String(val);
|
|
91
|
+
return val == null ? "" : isArray(val) || isPlainObject(val) && val.toString === objectToString ? JSON.stringify(val, null, 2) : String(val);
|
|
95
92
|
};
|
|
96
|
-
function join(items, separator =
|
|
97
|
-
|
|
93
|
+
function join(items, separator = "") {
|
|
94
|
+
return items.reduce((str, item, index) => index === 0 ? str + item : str + separator + item, "");
|
|
98
95
|
}
|
|
99
96
|
const RANGE = 2;
|
|
100
97
|
function generateCodeFrame(source, start = 0, end = source.length) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
}
|
|
132
|
-
|
|
98
|
+
const lines = source.split(/\r?\n/);
|
|
99
|
+
let count = 0;
|
|
100
|
+
const res = [];
|
|
101
|
+
for (let i = 0; i < lines.length; i++) {
|
|
102
|
+
count += lines[i].length + 1;
|
|
103
|
+
if (count >= start) {
|
|
104
|
+
for (let j = i - RANGE; j <= i + RANGE || end > count; j++) {
|
|
105
|
+
if (j < 0 || j >= lines.length) continue;
|
|
106
|
+
const line = j + 1;
|
|
107
|
+
res.push(`${line}${" ".repeat(3 - String(line).length)}| ${lines[j]}`);
|
|
108
|
+
const lineLength = lines[j].length;
|
|
109
|
+
if (j === i) {
|
|
110
|
+
const pad = start - (count - lineLength) + 1;
|
|
111
|
+
const length = Math.max(1, end > count ? lineLength - pad : end - start);
|
|
112
|
+
res.push(` | ` + " ".repeat(pad) + "^".repeat(length));
|
|
113
|
+
} else if (j > i) {
|
|
114
|
+
if (end > count) {
|
|
115
|
+
const length = Math.max(Math.min(end - count, lineLength), 1);
|
|
116
|
+
res.push(` | ` + "^".repeat(length));
|
|
117
|
+
}
|
|
118
|
+
count += lineLength + 1;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return res.join("\n");
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
127
|
+
//#region packages/shared/src/warn.ts
|
|
133
128
|
function warn(msg, err) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
140
|
-
}
|
|
129
|
+
if (typeof console !== "undefined") {
|
|
130
|
+
console.warn(`[intlify] ` + msg);
|
|
131
|
+
/* istanbul ignore if */
|
|
132
|
+
if (err) console.warn(err.stack);
|
|
133
|
+
}
|
|
141
134
|
}
|
|
142
135
|
const hasWarned = {};
|
|
143
136
|
function warnOnce(msg) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
137
|
+
if (!hasWarned[msg]) {
|
|
138
|
+
hasWarned[msg] = true;
|
|
139
|
+
warn(msg);
|
|
140
|
+
}
|
|
148
141
|
}
|
|
149
|
-
|
|
142
|
+
//#endregion
|
|
143
|
+
//#region packages/shared/src/sanitize.ts
|
|
144
|
+
function escapeHtml(rawText) {
|
|
145
|
+
return rawText.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/").replace(/=/g, "=");
|
|
146
|
+
}
|
|
147
|
+
function escapeAttributeValue(value) {
|
|
148
|
+
return value.replace(/&(?![a-z0-9#]{2,6};)/gi, "&").replace(/"/g, """).replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">");
|
|
149
|
+
}
|
|
150
|
+
const javascriptSchemePattern = /^javascript:/i;
|
|
151
|
+
const urlAttributePattern = /^(?:href|src|action|formaction)$/i;
|
|
152
|
+
const numericCharacterReferencePattern = /&#(?:x([0-9a-f]+)|(\d+));?/gi;
|
|
153
|
+
const namedWhitespaceCharacterReferencePattern = /&(?:Tab|NewLine);/g;
|
|
154
|
+
const colonCharacterReferencePattern = /:?/gi;
|
|
155
|
+
const controlOrWhitespacePattern = /[\u0000-\u0020\u007f-\u009f]/g;
|
|
156
|
+
const eventHandlerPattern = /(?:^|[\s"'<>/])on\w+\s*=\s*["']?[^"'>]+["']?/i;
|
|
157
|
+
const eventHandlerAttributePattern = /(^|[\s"'<>/])on(\w+\s*=)/gi;
|
|
158
|
+
const srcdocPattern = /(?:^|[\s"'<>/])srcdoc\s*=/i;
|
|
159
|
+
const srcdocAttributePattern = /(^|[\s"'<>/])s(rcdoc\s*=)/gi;
|
|
160
|
+
const unquotedUrlAttributePattern = /(^|[\s"'<>/])((?:href|src|action|formaction)\s*=\s*)([^\s"'=<>`]+)/gi;
|
|
161
|
+
function decodeNumericCharacterReference(match, hex, decimal) {
|
|
162
|
+
const digits = hex || decimal;
|
|
163
|
+
if (!digits) return match;
|
|
164
|
+
const codePoint = Number.parseInt(digits, hex ? 16 : 10);
|
|
165
|
+
return codePoint <= 127 ? String.fromCharCode(codePoint) : match;
|
|
166
|
+
}
|
|
167
|
+
function hasJavascriptScheme(value) {
|
|
168
|
+
const normalized = value.replace(numericCharacterReferencePattern, decodeNumericCharacterReference).replace(namedWhitespaceCharacterReferencePattern, "").replace(colonCharacterReferencePattern, ":").replace(controlOrWhitespacePattern, "");
|
|
169
|
+
return javascriptSchemePattern.test(normalized);
|
|
170
|
+
}
|
|
171
|
+
function sanitizeStyleValue(value) {
|
|
172
|
+
const urlPattern = /url\s*\(/gi;
|
|
173
|
+
let sanitized = "";
|
|
174
|
+
let cursor = 0;
|
|
175
|
+
let match;
|
|
176
|
+
while ((match = urlPattern.exec(value)) !== null) {
|
|
177
|
+
const urlStart = match.index;
|
|
178
|
+
const openParenIndex = urlPattern.lastIndex - 1;
|
|
179
|
+
let index = openParenIndex + 1;
|
|
180
|
+
let depth = 1;
|
|
181
|
+
let quote = null;
|
|
182
|
+
for (; index < value.length; index++) {
|
|
183
|
+
const char = value[index];
|
|
184
|
+
if (quote) {
|
|
185
|
+
if (char === quote) quote = null;
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (char === "\"" || char === "'") quote = char;
|
|
189
|
+
else if (char === "(") depth++;
|
|
190
|
+
else if (char === ")") {
|
|
191
|
+
depth--;
|
|
192
|
+
if (depth === 0) break;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (depth !== 0) break;
|
|
196
|
+
const rawUrlValue = value.slice(openParenIndex + 1, index).trim();
|
|
197
|
+
const unquotedUrlValue = rawUrlValue.startsWith("\"") && rawUrlValue.endsWith("\"") || rawUrlValue.startsWith("'") && rawUrlValue.endsWith("'") ? rawUrlValue.slice(1, -1).trim() : rawUrlValue;
|
|
198
|
+
sanitized += value.slice(cursor, urlStart);
|
|
199
|
+
sanitized += hasJavascriptScheme(unquotedUrlValue) ? "url(about:blank)" : value.slice(urlStart, index + 1);
|
|
200
|
+
cursor = index + 1;
|
|
201
|
+
}
|
|
202
|
+
return sanitized + value.slice(cursor);
|
|
203
|
+
}
|
|
204
|
+
function sanitizeAttributeValue(attrName, value) {
|
|
205
|
+
if (urlAttributePattern.test(attrName) && hasJavascriptScheme(value)) return "about:blank";
|
|
206
|
+
return escapeAttributeValue(attrName.toLowerCase() === "style" ? sanitizeStyleValue(value) : value);
|
|
207
|
+
}
|
|
208
|
+
function sanitizeTranslatedHtml(html) {
|
|
209
|
+
html = html.replace(/([\w:-]+)\s*=\s*"([^"]*)"/g, (_, attrName, attrValue) => `${attrName}="${sanitizeAttributeValue(attrName, attrValue)}"`);
|
|
210
|
+
html = html.replace(/([\w:-]+)\s*=\s*'([^']*)'/g, (_, attrName, attrValue) => `${attrName}='${sanitizeAttributeValue(attrName, attrValue)}'`);
|
|
211
|
+
if (eventHandlerPattern.test(html)) {
|
|
212
|
+
warn("Potentially dangerous event handlers detected in translation. Consider removing onclick, onerror, etc. from your translation messages.");
|
|
213
|
+
html = html.replace(eventHandlerAttributePattern, "$1on$2");
|
|
214
|
+
}
|
|
215
|
+
if (srcdocPattern.test(html)) {
|
|
216
|
+
warn("Potentially dangerous srcdoc attribute detected in translation. Consider removing srcdoc from your translation messages.");
|
|
217
|
+
html = html.replace(srcdocAttributePattern, "$1s$2");
|
|
218
|
+
}
|
|
219
|
+
html = html.replace(unquotedUrlAttributePattern, (match, boundary, prefix, attrValue) => hasJavascriptScheme(attrValue) ? `${boundary}${prefix}about:blank` : match);
|
|
220
|
+
return html;
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region packages/shared/src/messages.ts
|
|
150
224
|
const isNotObjectOrIsArray = (val) => !isObject(val) || isArray(val);
|
|
151
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
152
225
|
function deepCopy(src, des) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
183
|
-
|
|
226
|
+
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des) || des === Object.prototype) throw new Error("Invalid value");
|
|
227
|
+
const stack = [src, des];
|
|
228
|
+
while (stack.length) {
|
|
229
|
+
const des = stack.pop();
|
|
230
|
+
const src = stack.pop();
|
|
231
|
+
const keys = Object.keys(src);
|
|
232
|
+
for (let i = 0; i < keys.length; i++) {
|
|
233
|
+
const key = keys[i];
|
|
234
|
+
if (key === "__proto__") continue;
|
|
235
|
+
const value = src[key];
|
|
236
|
+
if (!isObject(value)) {
|
|
237
|
+
des[key] = value;
|
|
238
|
+
continue;
|
|
239
|
+
}
|
|
240
|
+
if (isArray(value)) {
|
|
241
|
+
const copied = [];
|
|
242
|
+
copied.length = value.length;
|
|
243
|
+
des[key] = copied;
|
|
244
|
+
stack.push(value, copied);
|
|
245
|
+
continue;
|
|
246
|
+
}
|
|
247
|
+
let desValue = des[key];
|
|
248
|
+
if (!isObject(desValue) || isArray(desValue)) desValue = des[key] = create();
|
|
249
|
+
stack.push(value, desValue);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
//#endregion
|
|
254
|
+
//#region packages/message-compiler/src/errors.ts
|
|
184
255
|
const CompileErrorCodes = {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// generator error codes
|
|
202
|
-
UNHANDLED_CODEGEN_NODE_TYPE: 15,
|
|
203
|
-
// minifier error codes
|
|
204
|
-
UNHANDLED_MINIFIER_NODE_TYPE: 16
|
|
256
|
+
EXPECTED_TOKEN: 1,
|
|
257
|
+
INVALID_TOKEN_IN_PLACEHOLDER: 2,
|
|
258
|
+
UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER: 3,
|
|
259
|
+
UNKNOWN_ESCAPE_SEQUENCE: 4,
|
|
260
|
+
INVALID_UNICODE_ESCAPE_SEQUENCE: 5,
|
|
261
|
+
UNBALANCED_CLOSING_BRACE: 6,
|
|
262
|
+
UNTERMINATED_CLOSING_BRACE: 7,
|
|
263
|
+
EMPTY_PLACEHOLDER: 8,
|
|
264
|
+
NOT_ALLOW_NEST_PLACEHOLDER: 9,
|
|
265
|
+
INVALID_LINKED_FORMAT: 10,
|
|
266
|
+
MUST_HAVE_MESSAGES_IN_PLURAL: 11,
|
|
267
|
+
UNEXPECTED_EMPTY_LINKED_MODIFIER: 12,
|
|
268
|
+
UNEXPECTED_EMPTY_LINKED_KEY: 13,
|
|
269
|
+
UNEXPECTED_LEXICAL_ANALYSIS: 14,
|
|
270
|
+
UNHANDLED_CODEGEN_NODE_TYPE: 15,
|
|
271
|
+
UNHANDLED_MINIFIER_NODE_TYPE: 16
|
|
205
272
|
};
|
|
206
|
-
// Special value for higher-order compilers to pick up the last code
|
|
207
|
-
// to avoid collision of error codes.
|
|
208
|
-
// This should always be kept as the last item.
|
|
209
|
-
const COMPILE_ERROR_CODES_EXTEND_POINT = 17;
|
|
210
273
|
/** @internal */
|
|
211
274
|
const errorMessages$1 = {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
// generator error messages
|
|
229
|
-
[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]: `unhandled codegen node type: '{0}'`,
|
|
230
|
-
// minimizer error messages
|
|
231
|
-
[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]: `unhandled mimifier node type: '{0}'`
|
|
275
|
+
[CompileErrorCodes.EXPECTED_TOKEN]: `Expected token: '{0}'`,
|
|
276
|
+
[CompileErrorCodes.INVALID_TOKEN_IN_PLACEHOLDER]: `Invalid token in placeholder: '{0}'`,
|
|
277
|
+
[CompileErrorCodes.UNTERMINATED_SINGLE_QUOTE_IN_PLACEHOLDER]: `Unterminated single quote in placeholder`,
|
|
278
|
+
[CompileErrorCodes.UNKNOWN_ESCAPE_SEQUENCE]: `Unknown escape sequence: \\{0}`,
|
|
279
|
+
[CompileErrorCodes.INVALID_UNICODE_ESCAPE_SEQUENCE]: `Invalid unicode escape sequence: {0}`,
|
|
280
|
+
[CompileErrorCodes.UNBALANCED_CLOSING_BRACE]: `Unbalanced closing brace`,
|
|
281
|
+
[CompileErrorCodes.UNTERMINATED_CLOSING_BRACE]: `Unterminated closing brace`,
|
|
282
|
+
[CompileErrorCodes.EMPTY_PLACEHOLDER]: `Empty placeholder`,
|
|
283
|
+
[CompileErrorCodes.NOT_ALLOW_NEST_PLACEHOLDER]: `Not allowed nest placeholder`,
|
|
284
|
+
[CompileErrorCodes.INVALID_LINKED_FORMAT]: `Invalid linked format`,
|
|
285
|
+
[CompileErrorCodes.MUST_HAVE_MESSAGES_IN_PLURAL]: `Plural must have messages`,
|
|
286
|
+
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_MODIFIER]: `Unexpected empty linked modifier`,
|
|
287
|
+
[CompileErrorCodes.UNEXPECTED_EMPTY_LINKED_KEY]: `Unexpected empty linked key`,
|
|
288
|
+
[CompileErrorCodes.UNEXPECTED_LEXICAL_ANALYSIS]: `Unexpected lexical analysis in token: '{0}'`,
|
|
289
|
+
[CompileErrorCodes.UNHANDLED_CODEGEN_NODE_TYPE]: `unhandled codegen node type: '{0}'`,
|
|
290
|
+
[CompileErrorCodes.UNHANDLED_MINIFIER_NODE_TYPE]: `unhandled mimifier node type: '{0}'`
|
|
232
291
|
};
|
|
233
292
|
function createCompileError(code, loc, options = {}) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
245
|
-
|
|
293
|
+
const { domain, messages, args } = options;
|
|
294
|
+
const msg = format$1((messages || errorMessages$1)[code] || "", ...args || []);
|
|
295
|
+
const error = new SyntaxError(String(msg));
|
|
296
|
+
error.code = code;
|
|
297
|
+
if (loc) error.location = loc;
|
|
298
|
+
error.domain = domain;
|
|
299
|
+
return error;
|
|
300
|
+
}
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region packages/core-base/src/ast.ts
|
|
246
303
|
function isMessageAST(val) {
|
|
247
|
-
|
|
248
|
-
resolveType(val) === 0 &&
|
|
249
|
-
(hasOwn(val, 'b') || hasOwn(val, 'body')));
|
|
304
|
+
return isObject(val) && resolveType(val) === 0 && (hasOwn(val, "b") || hasOwn(val, "body"));
|
|
250
305
|
}
|
|
251
|
-
const PROPS_BODY = [
|
|
306
|
+
const PROPS_BODY = ["b", "body"];
|
|
252
307
|
function resolveBody(node) {
|
|
253
|
-
|
|
308
|
+
return resolveProps(node, PROPS_BODY);
|
|
254
309
|
}
|
|
255
|
-
const PROPS_CASES = [
|
|
310
|
+
const PROPS_CASES = ["c", "cases"];
|
|
256
311
|
function resolveCases(node) {
|
|
257
|
-
|
|
312
|
+
return resolveProps(node, PROPS_CASES, []);
|
|
258
313
|
}
|
|
259
|
-
const PROPS_STATIC = [
|
|
314
|
+
const PROPS_STATIC = ["s", "static"];
|
|
260
315
|
function resolveStatic(node) {
|
|
261
|
-
|
|
316
|
+
return resolveProps(node, PROPS_STATIC);
|
|
262
317
|
}
|
|
263
|
-
const PROPS_ITEMS = [
|
|
318
|
+
const PROPS_ITEMS = ["i", "items"];
|
|
264
319
|
function resolveItems(node) {
|
|
265
|
-
|
|
320
|
+
return resolveProps(node, PROPS_ITEMS, []);
|
|
266
321
|
}
|
|
267
|
-
const PROPS_TYPE = [
|
|
322
|
+
const PROPS_TYPE = ["t", "type"];
|
|
268
323
|
function resolveType(node) {
|
|
269
|
-
|
|
324
|
+
return resolveProps(node, PROPS_TYPE);
|
|
270
325
|
}
|
|
271
|
-
const PROPS_VALUE = [
|
|
326
|
+
const PROPS_VALUE = ["v", "value"];
|
|
272
327
|
function resolveValue$1(node, type) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
throw createUnhandleNodeError(type);
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
const PROPS_MODIFIER = ['m', 'modifier'];
|
|
328
|
+
const resolved = resolveProps(node, PROPS_VALUE);
|
|
329
|
+
if (resolved != null) return resolved;
|
|
330
|
+
else throw createUnhandleNodeError(type);
|
|
331
|
+
}
|
|
332
|
+
const PROPS_MODIFIER = ["m", "modifier"];
|
|
282
333
|
function resolveLinkedModifier(node) {
|
|
283
|
-
|
|
334
|
+
return resolveProps(node, PROPS_MODIFIER);
|
|
284
335
|
}
|
|
285
|
-
const PROPS_KEY = [
|
|
336
|
+
const PROPS_KEY = ["k", "key"];
|
|
286
337
|
function resolveLinkedKey(node) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
291
|
-
else {
|
|
292
|
-
throw createUnhandleNodeError(6 /* NodeTypes.Linked */);
|
|
293
|
-
}
|
|
338
|
+
const resolved = resolveProps(node, PROPS_KEY);
|
|
339
|
+
if (resolved) return resolved;
|
|
340
|
+
else throw createUnhandleNodeError(6);
|
|
294
341
|
}
|
|
295
342
|
function resolveProps(node, props, defaultValue) {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return node[prop];
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
return defaultValue;
|
|
343
|
+
for (let i = 0; i < props.length; i++) {
|
|
344
|
+
const prop = props[i];
|
|
345
|
+
if (hasOwn(node, prop) && node[prop] != null) return node[prop];
|
|
346
|
+
}
|
|
347
|
+
return defaultValue;
|
|
305
348
|
}
|
|
306
349
|
const AST_NODE_PROPS_KEYS = [
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
350
|
+
...PROPS_BODY,
|
|
351
|
+
...PROPS_CASES,
|
|
352
|
+
...PROPS_STATIC,
|
|
353
|
+
...PROPS_ITEMS,
|
|
354
|
+
...PROPS_KEY,
|
|
355
|
+
...PROPS_MODIFIER,
|
|
356
|
+
...PROPS_VALUE,
|
|
357
|
+
...PROPS_TYPE
|
|
315
358
|
];
|
|
316
359
|
function createUnhandleNodeError(type) {
|
|
317
|
-
|
|
360
|
+
return /* @__PURE__ */ new Error(`unhandled node type: ${type}`);
|
|
318
361
|
}
|
|
319
|
-
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region packages/core-base/src/format.ts
|
|
320
364
|
function format(ast) {
|
|
321
|
-
|
|
322
|
-
return msg;
|
|
365
|
+
return (ctx) => formatParts(ctx, ast);
|
|
323
366
|
}
|
|
324
367
|
function formatParts(ctx, ast) {
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const plural = body;
|
|
332
|
-
const cases = resolveCases(plural);
|
|
333
|
-
return ctx.plural(cases.reduce((messages, c) => [
|
|
334
|
-
...messages,
|
|
335
|
-
formatMessageParts(ctx, c)
|
|
336
|
-
], []));
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
return formatMessageParts(ctx, body);
|
|
340
|
-
}
|
|
368
|
+
const body = resolveBody(ast);
|
|
369
|
+
if (body == null) throw createUnhandleNodeError(0);
|
|
370
|
+
if (resolveType(body) === 1) {
|
|
371
|
+
const cases = resolveCases(body);
|
|
372
|
+
return ctx.plural(cases.reduce((messages, c) => [...isArray(messages) ? messages : [messages], formatMessageParts(ctx, c)], []));
|
|
373
|
+
} else return formatMessageParts(ctx, body);
|
|
341
374
|
}
|
|
342
375
|
function formatMessageParts(ctx, node) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
else {
|
|
350
|
-
const messages = resolveItems(node).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
|
|
351
|
-
return ctx.normalize(messages);
|
|
352
|
-
}
|
|
376
|
+
const static_ = resolveStatic(node);
|
|
377
|
+
if (static_ != null) return ctx.type === "text" ? static_ : ctx.normalize([static_]);
|
|
378
|
+
else {
|
|
379
|
+
const messages = resolveItems(node).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
|
|
380
|
+
return ctx.normalize(messages);
|
|
381
|
+
}
|
|
353
382
|
}
|
|
354
383
|
function formatMessagePart(ctx, node) {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
const linked = node;
|
|
385
|
-
const modifier = resolveLinkedModifier(linked);
|
|
386
|
-
const key = resolveLinkedKey(linked);
|
|
387
|
-
return ctx.linked(formatMessagePart(ctx, key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
|
|
388
|
-
}
|
|
389
|
-
case 7 /* NodeTypes.LinkedKey */: {
|
|
390
|
-
return resolveValue$1(node, type);
|
|
391
|
-
}
|
|
392
|
-
case 8 /* NodeTypes.LinkedModifier */: {
|
|
393
|
-
return resolveValue$1(node, type);
|
|
394
|
-
}
|
|
395
|
-
default:
|
|
396
|
-
throw new Error(`unhandled node on format message part: ${type}`);
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
|
|
384
|
+
const type = resolveType(node);
|
|
385
|
+
switch (type) {
|
|
386
|
+
case 3: return resolveValue$1(node, type);
|
|
387
|
+
case 9: return resolveValue$1(node, type);
|
|
388
|
+
case 4: {
|
|
389
|
+
const named = node;
|
|
390
|
+
if (hasOwn(named, "k") && named.k) return ctx.interpolate(ctx.named(named.k));
|
|
391
|
+
if (hasOwn(named, "key") && named.key) return ctx.interpolate(ctx.named(named.key));
|
|
392
|
+
throw createUnhandleNodeError(type);
|
|
393
|
+
}
|
|
394
|
+
case 5: {
|
|
395
|
+
const list = node;
|
|
396
|
+
if (hasOwn(list, "i") && isNumber(list.i)) return ctx.interpolate(ctx.list(list.i));
|
|
397
|
+
if (hasOwn(list, "index") && isNumber(list.index)) return ctx.interpolate(ctx.list(list.index));
|
|
398
|
+
throw createUnhandleNodeError(type);
|
|
399
|
+
}
|
|
400
|
+
case 6: {
|
|
401
|
+
const linked = node;
|
|
402
|
+
const modifier = resolveLinkedModifier(linked);
|
|
403
|
+
const key = resolveLinkedKey(linked);
|
|
404
|
+
return ctx.linked(formatMessagePart(ctx, key), modifier ? formatMessagePart(ctx, modifier) : void 0, ctx.type);
|
|
405
|
+
}
|
|
406
|
+
case 7: return resolveValue$1(node, type);
|
|
407
|
+
case 8: return resolveValue$1(node, type);
|
|
408
|
+
default: throw new Error(`unhandled node on format message part: ${type}`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
//#endregion
|
|
412
|
+
//#region packages/core-base/src/compilation.ts
|
|
400
413
|
let compileCache = create();
|
|
401
414
|
function clearCompileCache() {
|
|
402
|
-
|
|
415
|
+
compileCache = create();
|
|
403
416
|
}
|
|
404
417
|
/* #__NO_SIDE_EFFECTS__ */
|
|
405
418
|
function compile(message, context) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
else {
|
|
423
|
-
return format(message);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
let devtools = null;
|
|
429
|
-
function setDevToolsHook(hook) {
|
|
430
|
-
devtools = hook;
|
|
431
|
-
}
|
|
432
|
-
function getDevToolsHook() {
|
|
433
|
-
return devtools;
|
|
434
|
-
}
|
|
435
|
-
function initI18nDevTools(i18n, version, meta) {
|
|
436
|
-
// TODO: queue if devtools is undefined
|
|
437
|
-
devtools &&
|
|
438
|
-
devtools.emit('i18n:init', {
|
|
439
|
-
timestamp: Date.now(),
|
|
440
|
-
i18n,
|
|
441
|
-
version,
|
|
442
|
-
meta
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
const translateDevTools =
|
|
446
|
-
/* #__PURE__*/ createDevToolsHook('function:translate');
|
|
447
|
-
function createDevToolsHook(hook) {
|
|
448
|
-
return (payloads) => devtools && devtools.emit(hook, payloads);
|
|
449
|
-
}
|
|
450
|
-
|
|
419
|
+
{
|
|
420
|
+
if (!isMessageAST(message)) {
|
|
421
|
+
warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
422
|
+
return (() => message);
|
|
423
|
+
}
|
|
424
|
+
const cacheKey = message.cacheKey;
|
|
425
|
+
if (cacheKey) {
|
|
426
|
+
const cached = compileCache[cacheKey];
|
|
427
|
+
if (cached) return cached;
|
|
428
|
+
return compileCache[cacheKey] = format(message);
|
|
429
|
+
} else return format(message);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region packages/core-base/src/errors.ts
|
|
451
434
|
const CoreErrorCodes = {
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
NOT_SUPPORT_LOCALE_TYPE: 23
|
|
435
|
+
INVALID_ARGUMENT: 17,
|
|
436
|
+
INVALID_DATE_ARGUMENT: 18,
|
|
437
|
+
INVALID_ISO_DATE_ARGUMENT: 19,
|
|
438
|
+
NOT_SUPPORT_LOCALE_PROMISE_VALUE: 20,
|
|
439
|
+
NOT_SUPPORT_LOCALE_ASYNC_FUNCTION: 21,
|
|
440
|
+
NOT_SUPPORT_LOCALE_TYPE: 22
|
|
459
441
|
};
|
|
460
|
-
const CORE_ERROR_CODES_EXTEND_POINT =
|
|
442
|
+
const CORE_ERROR_CODES_EXTEND_POINT = 23;
|
|
461
443
|
function createCoreError(code) {
|
|
462
|
-
|
|
444
|
+
return createCompileError(code, null, { messages: errorMessages });
|
|
463
445
|
}
|
|
464
446
|
/** @internal */
|
|
465
447
|
const errorMessages = {
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
[CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: 'cannot support async function',
|
|
473
|
-
[CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: 'cannot support locale type'
|
|
448
|
+
[CoreErrorCodes.INVALID_ARGUMENT]: "Invalid arguments",
|
|
449
|
+
[CoreErrorCodes.INVALID_DATE_ARGUMENT]: "The date provided is an invalid Date object.Make sure your Date represents a valid date.",
|
|
450
|
+
[CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT]: "The argument provided is not a valid ISO date string",
|
|
451
|
+
[CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE]: "cannot support promise value",
|
|
452
|
+
[CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION]: "cannot support async function",
|
|
453
|
+
[CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE]: "cannot support locale type"
|
|
474
454
|
};
|
|
475
|
-
|
|
455
|
+
//#endregion
|
|
456
|
+
//#region packages/core-base/src/fallbacker.ts
|
|
476
457
|
/** @internal */
|
|
477
458
|
function getLocale(context, options) {
|
|
478
|
-
|
|
479
|
-
? resolveLocale(options.locale)
|
|
480
|
-
: resolveLocale(context.locale);
|
|
459
|
+
return options.locale != null ? resolveLocale(options.locale) : resolveLocale(context.locale);
|
|
481
460
|
}
|
|
482
461
|
let _resolveLocale;
|
|
483
462
|
/** @internal */
|
|
484
463
|
function resolveLocale(locale) {
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
const resolve = locale();
|
|
495
|
-
if (isPromise(resolve)) {
|
|
496
|
-
throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
|
|
497
|
-
}
|
|
498
|
-
return (_resolveLocale = resolve);
|
|
499
|
-
}
|
|
500
|
-
else {
|
|
501
|
-
throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
else {
|
|
505
|
-
throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
|
|
506
|
-
}
|
|
507
|
-
}
|
|
464
|
+
if (isString(locale)) return locale;
|
|
465
|
+
else if (isFunction(locale)) {
|
|
466
|
+
if (locale.resolvedOnce && _resolveLocale != null) return _resolveLocale;
|
|
467
|
+
else if (locale.constructor.name === "Function") {
|
|
468
|
+
const resolve = locale();
|
|
469
|
+
if (isPromise(resolve)) throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_PROMISE_VALUE);
|
|
470
|
+
return _resolveLocale = resolve;
|
|
471
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_ASYNC_FUNCTION);
|
|
472
|
+
} else throw createCoreError(CoreErrorCodes.NOT_SUPPORT_LOCALE_TYPE);
|
|
508
473
|
}
|
|
509
474
|
/**
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
function fallbackWithSimple(
|
|
526
|
-
|
|
527
|
-
return [...new Set([
|
|
528
|
-
start,
|
|
529
|
-
...(isArray(fallback)
|
|
530
|
-
? fallback
|
|
531
|
-
: isObject(fallback)
|
|
532
|
-
? Object.keys(fallback)
|
|
533
|
-
: isString(fallback)
|
|
534
|
-
? [fallback]
|
|
535
|
-
: [start])
|
|
536
|
-
])];
|
|
475
|
+
* Fallback with simple implemenation
|
|
476
|
+
*
|
|
477
|
+
* @remarks
|
|
478
|
+
* A fallback locale function implemented with a simple fallback algorithm.
|
|
479
|
+
*
|
|
480
|
+
* Basically, the chain consists of `start` plus the specified fallback: for a string or array `fallbackLocale`, the value as specified in the props is used; for a map `fallbackLocale`, the map's **keys** are used and the `default` key is skipped since it is not a locale name.
|
|
481
|
+
*
|
|
482
|
+
* @param ctx - A {@link CoreContext | context}
|
|
483
|
+
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
484
|
+
* @param start - A starting {@link Locale | locale}
|
|
485
|
+
*
|
|
486
|
+
* @returns Fallback locales
|
|
487
|
+
*
|
|
488
|
+
* @VueI18nGeneral
|
|
489
|
+
*/
|
|
490
|
+
function fallbackWithSimple(_ctx, fallback, start) {
|
|
491
|
+
return [.../* @__PURE__ */ new Set([start, ...isArray(fallback) ? fallback : isObject(fallback) ? Object.keys(fallback).filter((locale) => locale !== "default") : isString(fallback) ? [fallback] : [start]])];
|
|
537
492
|
}
|
|
538
493
|
/**
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
494
|
+
* Fallback with locale chain
|
|
495
|
+
*
|
|
496
|
+
* @remarks
|
|
497
|
+
* A fallback locale function implemented with a fallback chain algorithm. It's used in VueI18n as default.
|
|
498
|
+
*
|
|
499
|
+
* Chains are cached per fallback identity. If you mutate an object or array fallback in place,
|
|
500
|
+
* pass a new object or array instead so that the chain is recomputed.
|
|
501
|
+
*
|
|
502
|
+
* @param ctx - A {@link CoreContext | context}
|
|
503
|
+
* @param fallback - A {@link FallbackLocale | fallback locale}
|
|
504
|
+
* @param start - A starting {@link Locale | locale}
|
|
505
|
+
*
|
|
506
|
+
* @returns Fallback locales
|
|
507
|
+
*
|
|
508
|
+
* @VueI18nSee [Fallbacking](../../guide/essentials/fallback)
|
|
509
|
+
*
|
|
510
|
+
* @VueI18nGeneral
|
|
511
|
+
*/
|
|
554
512
|
function fallbackWithLocaleChain(ctx, fallback, start) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
return chain;
|
|
513
|
+
const startLocale = isString(start) ? start : DEFAULT_LOCALE;
|
|
514
|
+
const context = ctx;
|
|
515
|
+
let chains;
|
|
516
|
+
if (isObject(fallback)) {
|
|
517
|
+
if (!context.__localeChainObjectCache) context.__localeChainObjectCache = /* @__PURE__ */ new WeakMap();
|
|
518
|
+
chains = context.__localeChainObjectCache.get(fallback);
|
|
519
|
+
if (!chains) {
|
|
520
|
+
chains = /* @__PURE__ */ new Map();
|
|
521
|
+
context.__localeChainObjectCache.set(fallback, chains);
|
|
522
|
+
}
|
|
523
|
+
} else {
|
|
524
|
+
if (!context.__localeChainCache) context.__localeChainCache = /* @__PURE__ */ new Map();
|
|
525
|
+
chains = context.__localeChainCache.get(fallback);
|
|
526
|
+
if (!chains) {
|
|
527
|
+
chains = /* @__PURE__ */ new Map();
|
|
528
|
+
context.__localeChainCache.set(fallback, chains);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const cached = chains.get(startLocale);
|
|
532
|
+
if (cached) return cached;
|
|
533
|
+
const chain = [];
|
|
534
|
+
let block = [start];
|
|
535
|
+
while (isArray(block)) block = appendBlockToChain(chain, block, fallback);
|
|
536
|
+
const defaults = isArray(fallback) || !isPlainObject(fallback) ? fallback : fallback["default"] ? fallback["default"] : null;
|
|
537
|
+
block = isString(defaults) ? [defaults] : defaults;
|
|
538
|
+
if (isArray(block)) appendBlockToChain(chain, block, false);
|
|
539
|
+
chains.set(startLocale, chain);
|
|
540
|
+
return chain;
|
|
584
541
|
}
|
|
585
542
|
function appendBlockToChain(chain, block, blocks) {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
}
|
|
593
|
-
return follow;
|
|
543
|
+
let follow = true;
|
|
544
|
+
for (let i = 0; i < block.length && isBoolean(follow); i++) {
|
|
545
|
+
const locale = block[i];
|
|
546
|
+
if (isString(locale)) follow = appendLocaleToChain(chain, block[i], blocks);
|
|
547
|
+
}
|
|
548
|
+
return follow;
|
|
594
549
|
}
|
|
595
550
|
function appendLocaleToChain(chain, locale, blocks) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
return follow;
|
|
551
|
+
let follow;
|
|
552
|
+
const tokens = locale.split("-");
|
|
553
|
+
do {
|
|
554
|
+
follow = appendItemToChain(chain, tokens.join("-"), blocks);
|
|
555
|
+
tokens.splice(-1, 1);
|
|
556
|
+
} while (tokens.length && follow === true);
|
|
557
|
+
return follow;
|
|
604
558
|
}
|
|
605
559
|
function appendItemToChain(chain, target, blocks) {
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
621
|
-
return follow;
|
|
622
|
-
}
|
|
623
|
-
|
|
560
|
+
let follow = false;
|
|
561
|
+
if (!chain.includes(target)) {
|
|
562
|
+
follow = true;
|
|
563
|
+
if (target) {
|
|
564
|
+
follow = target[target.length - 1] !== "!";
|
|
565
|
+
const locale = target.replace(/!/g, "");
|
|
566
|
+
chain.push(locale);
|
|
567
|
+
if ((isArray(blocks) || isPlainObject(blocks)) && blocks[locale]) follow = blocks[locale];
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return follow;
|
|
571
|
+
}
|
|
572
|
+
//#endregion
|
|
573
|
+
//#region packages/core-base/src/resolver.ts
|
|
624
574
|
const pathStateMachine = [];
|
|
625
|
-
pathStateMachine[0
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
575
|
+
pathStateMachine[0] = {
|
|
576
|
+
["w"]: [0],
|
|
577
|
+
["i"]: [3, 0],
|
|
578
|
+
["["]: [4],
|
|
579
|
+
["o"]: [7]
|
|
630
580
|
};
|
|
631
|
-
pathStateMachine[1
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
581
|
+
pathStateMachine[1] = {
|
|
582
|
+
["w"]: [1],
|
|
583
|
+
["."]: [2],
|
|
584
|
+
["["]: [4],
|
|
585
|
+
["o"]: [7]
|
|
636
586
|
};
|
|
637
|
-
pathStateMachine[2
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
587
|
+
pathStateMachine[2] = {
|
|
588
|
+
["w"]: [2],
|
|
589
|
+
["i"]: [3, 0],
|
|
590
|
+
["0"]: [3, 0]
|
|
641
591
|
};
|
|
642
|
-
pathStateMachine[3
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
592
|
+
pathStateMachine[3] = {
|
|
593
|
+
["i"]: [3, 0],
|
|
594
|
+
["0"]: [3, 0],
|
|
595
|
+
["w"]: [1, 1],
|
|
596
|
+
["."]: [2, 1],
|
|
597
|
+
["["]: [4, 1],
|
|
598
|
+
["o"]: [7, 1]
|
|
649
599
|
};
|
|
650
|
-
pathStateMachine[4
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
["]" /* PathCharTypes.RIGHT_BRACKET */]: [1 /* States.IN_PATH */, 3 /* Actions.PUSH_SUB_PATH */],
|
|
658
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
659
|
-
["l" /* PathCharTypes.ELSE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */]
|
|
600
|
+
pathStateMachine[4] = {
|
|
601
|
+
["'"]: [5, 0],
|
|
602
|
+
["\""]: [6, 0],
|
|
603
|
+
["["]: [4, 2],
|
|
604
|
+
["]"]: [1, 3],
|
|
605
|
+
["o"]: 8,
|
|
606
|
+
["l"]: [4, 0]
|
|
660
607
|
};
|
|
661
|
-
pathStateMachine[5
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
608
|
+
pathStateMachine[5] = {
|
|
609
|
+
["'"]: [4, 0],
|
|
610
|
+
["o"]: 8,
|
|
611
|
+
["l"]: [5, 0]
|
|
665
612
|
};
|
|
666
|
-
pathStateMachine[6
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
613
|
+
pathStateMachine[6] = {
|
|
614
|
+
["\""]: [4, 0],
|
|
615
|
+
["o"]: 8,
|
|
616
|
+
["l"]: [6, 0]
|
|
670
617
|
};
|
|
671
618
|
/**
|
|
672
|
-
|
|
673
|
-
|
|
619
|
+
* Check if an expression is a literal value.
|
|
620
|
+
*/
|
|
674
621
|
const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
|
|
675
622
|
function isLiteral(exp) {
|
|
676
|
-
|
|
623
|
+
return literalValueRE.test(exp);
|
|
677
624
|
}
|
|
678
625
|
/**
|
|
679
|
-
|
|
680
|
-
|
|
626
|
+
* Strip quotes from a string
|
|
627
|
+
*/
|
|
681
628
|
function stripQuotes(str) {
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str;
|
|
629
|
+
const a = str.charCodeAt(0);
|
|
630
|
+
return a === str.charCodeAt(str.length - 1) && (a === 34 || a === 39) ? str.slice(1, -1) : str;
|
|
685
631
|
}
|
|
686
632
|
/**
|
|
687
|
-
|
|
688
|
-
|
|
633
|
+
* Determine the type of a character in a keypath.
|
|
634
|
+
*/
|
|
689
635
|
function getPathCharType(ch) {
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
case 0xfeff: // Byte Order Mark (BOM)
|
|
710
|
-
case 0x2028: // Line Separator (LS)
|
|
711
|
-
case 0x2029: // Paragraph Separator (PS)
|
|
712
|
-
return "w" /* PathCharTypes.WORKSPACE */;
|
|
713
|
-
}
|
|
714
|
-
return "i" /* PathCharTypes.IDENT */;
|
|
636
|
+
if (ch === void 0 || ch === null) return "o";
|
|
637
|
+
switch (ch.charCodeAt(0)) {
|
|
638
|
+
case 91:
|
|
639
|
+
case 93:
|
|
640
|
+
case 46:
|
|
641
|
+
case 34:
|
|
642
|
+
case 39: return ch;
|
|
643
|
+
case 95:
|
|
644
|
+
case 36:
|
|
645
|
+
case 45: return "i";
|
|
646
|
+
case 9:
|
|
647
|
+
case 10:
|
|
648
|
+
case 13:
|
|
649
|
+
case 160:
|
|
650
|
+
case 65279:
|
|
651
|
+
case 8232:
|
|
652
|
+
case 8233: return "w";
|
|
653
|
+
}
|
|
654
|
+
return "i";
|
|
715
655
|
}
|
|
716
656
|
/**
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
657
|
+
* Format a subPath, return its plain form if it is
|
|
658
|
+
* a literal string or number. Otherwise prepend the
|
|
659
|
+
* dynamic indicator (*).
|
|
660
|
+
*/
|
|
721
661
|
function formatSubPath(path) {
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
return false;
|
|
726
|
-
}
|
|
727
|
-
return isLiteral(trimmed)
|
|
728
|
-
? stripQuotes(trimmed)
|
|
729
|
-
: "*" /* PathCharTypes.ASTARISK */ + trimmed;
|
|
662
|
+
const trimmed = path.trim();
|
|
663
|
+
if (path.charAt(0) === "0" && isNaN(parseInt(path))) return false;
|
|
664
|
+
return isLiteral(trimmed) ? stripQuotes(trimmed) : "*" + trimmed;
|
|
730
665
|
}
|
|
731
666
|
/**
|
|
732
|
-
|
|
733
|
-
|
|
667
|
+
* Parse a string path into an array of segments
|
|
668
|
+
*/
|
|
734
669
|
function parse(path) {
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
type = getPathCharType(c);
|
|
804
|
-
typeMap = pathStateMachine[mode];
|
|
805
|
-
transition = typeMap[type] || typeMap["l" /* PathCharTypes.ELSE */] || 8 /* States.ERROR */;
|
|
806
|
-
// check parse error
|
|
807
|
-
if (transition === 8 /* States.ERROR */) {
|
|
808
|
-
return;
|
|
809
|
-
}
|
|
810
|
-
mode = transition[0];
|
|
811
|
-
if (transition[1] !== undefined) {
|
|
812
|
-
action = actions[transition[1]];
|
|
813
|
-
if (action) {
|
|
814
|
-
newChar = c;
|
|
815
|
-
if (action() === false) {
|
|
816
|
-
return;
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
}
|
|
820
|
-
// check parse finish
|
|
821
|
-
if (mode === 7 /* States.AFTER_PATH */) {
|
|
822
|
-
return keys;
|
|
823
|
-
}
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
// path token cache
|
|
827
|
-
const cache = new Map();
|
|
670
|
+
const keys = [];
|
|
671
|
+
let index = -1;
|
|
672
|
+
let mode = 0;
|
|
673
|
+
let subPathDepth = 0;
|
|
674
|
+
let c;
|
|
675
|
+
let key;
|
|
676
|
+
let newChar;
|
|
677
|
+
let type;
|
|
678
|
+
let transition;
|
|
679
|
+
let action;
|
|
680
|
+
let typeMap;
|
|
681
|
+
const actions = [];
|
|
682
|
+
actions[0] = () => {
|
|
683
|
+
if (key === void 0) key = newChar;
|
|
684
|
+
else key += newChar;
|
|
685
|
+
};
|
|
686
|
+
actions[1] = () => {
|
|
687
|
+
if (key !== void 0) {
|
|
688
|
+
keys.push(key);
|
|
689
|
+
key = void 0;
|
|
690
|
+
}
|
|
691
|
+
};
|
|
692
|
+
actions[2] = () => {
|
|
693
|
+
actions[0]();
|
|
694
|
+
subPathDepth++;
|
|
695
|
+
};
|
|
696
|
+
actions[3] = () => {
|
|
697
|
+
if (subPathDepth > 0) {
|
|
698
|
+
subPathDepth--;
|
|
699
|
+
mode = 4;
|
|
700
|
+
actions[0]();
|
|
701
|
+
} else {
|
|
702
|
+
subPathDepth = 0;
|
|
703
|
+
if (key === void 0) return false;
|
|
704
|
+
key = formatSubPath(key);
|
|
705
|
+
if (key === false) return false;
|
|
706
|
+
else actions[1]();
|
|
707
|
+
}
|
|
708
|
+
};
|
|
709
|
+
function maybeUnescapeQuote() {
|
|
710
|
+
const nextChar = path[index + 1];
|
|
711
|
+
if (mode === 5 && nextChar === "'" || mode === 6 && nextChar === "\"") {
|
|
712
|
+
index++;
|
|
713
|
+
newChar = "\\" + nextChar;
|
|
714
|
+
actions[0]();
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
while (mode !== null) {
|
|
719
|
+
index++;
|
|
720
|
+
c = path[index];
|
|
721
|
+
if (c === "\\" && maybeUnescapeQuote()) continue;
|
|
722
|
+
type = getPathCharType(c);
|
|
723
|
+
typeMap = pathStateMachine[mode];
|
|
724
|
+
transition = typeMap[type] || typeMap["l"] || 8;
|
|
725
|
+
if (transition === 8) return;
|
|
726
|
+
mode = transition[0];
|
|
727
|
+
if (transition[1] !== void 0) {
|
|
728
|
+
action = actions[transition[1]];
|
|
729
|
+
if (action) {
|
|
730
|
+
newChar = c;
|
|
731
|
+
if (action() === false) return;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
if (mode === 7) return keys;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
const cache = /* @__PURE__ */ new Map();
|
|
828
738
|
/**
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
739
|
+
* key-value message resolver
|
|
740
|
+
*
|
|
741
|
+
* @remarks
|
|
742
|
+
* Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
|
|
743
|
+
*
|
|
744
|
+
* @param obj - A target object to be resolved with path
|
|
745
|
+
* @param path - A {@link Path | path} to resolve the value of message
|
|
746
|
+
*
|
|
747
|
+
* @returns A resolved {@link PathValue | path value}
|
|
748
|
+
*
|
|
749
|
+
* @VueI18nGeneral
|
|
750
|
+
*/
|
|
841
751
|
function resolveWithKeyValue(obj, path) {
|
|
842
|
-
|
|
752
|
+
return isObject(obj) ? obj[path] : null;
|
|
843
753
|
}
|
|
844
754
|
/**
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
755
|
+
* message resolver
|
|
756
|
+
*
|
|
757
|
+
* @remarks
|
|
758
|
+
* Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
|
|
759
|
+
*
|
|
760
|
+
* @param obj - A target object to be resolved with path
|
|
761
|
+
* @param path - A {@link Path | path} to resolve the value of message
|
|
762
|
+
*
|
|
763
|
+
* @returns A resolved {@link PathValue | path value}
|
|
764
|
+
*
|
|
765
|
+
* @VueI18nGeneral
|
|
766
|
+
*/
|
|
857
767
|
function resolveValue(obj, path) {
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
const val = last[key];
|
|
889
|
-
if (val === undefined) {
|
|
890
|
-
return null;
|
|
891
|
-
}
|
|
892
|
-
if (isFunction(last)) {
|
|
893
|
-
return null;
|
|
894
|
-
}
|
|
895
|
-
last = val;
|
|
896
|
-
i++;
|
|
897
|
-
}
|
|
898
|
-
return last;
|
|
899
|
-
}
|
|
900
|
-
|
|
768
|
+
if (!isObject(obj)) return null;
|
|
769
|
+
let hit = cache.get(path);
|
|
770
|
+
if (!hit) {
|
|
771
|
+
hit = parse(path);
|
|
772
|
+
if (hit) cache.set(path, hit);
|
|
773
|
+
}
|
|
774
|
+
if (!hit) return null;
|
|
775
|
+
const len = hit.length;
|
|
776
|
+
let last = obj;
|
|
777
|
+
let i = 0;
|
|
778
|
+
while (i < len) {
|
|
779
|
+
const key = hit[i];
|
|
780
|
+
/**
|
|
781
|
+
* NOTE:
|
|
782
|
+
* if `key` is intlify message format AST node key and `last` is intlify message format AST, skip it.
|
|
783
|
+
* because the AST node is not a key-value structure.
|
|
784
|
+
*/
|
|
785
|
+
if (AST_NODE_PROPS_KEYS.includes(key) && isMessageAST(last)) return null;
|
|
786
|
+
if (!isObject(last)) return null;
|
|
787
|
+
if (!hasOwn(last, key)) return null;
|
|
788
|
+
const val = last[key];
|
|
789
|
+
if (val === void 0) return null;
|
|
790
|
+
if (isFunction(last)) return null;
|
|
791
|
+
last = val;
|
|
792
|
+
i++;
|
|
793
|
+
}
|
|
794
|
+
return last;
|
|
795
|
+
}
|
|
796
|
+
//#endregion
|
|
797
|
+
//#region packages/core-base/src/warnings.ts
|
|
901
798
|
const CoreWarnCodes = {
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
799
|
+
NOT_FOUND_KEY: 1,
|
|
800
|
+
FALLBACK_TO_TRANSLATE: 2,
|
|
801
|
+
CANNOT_FORMAT_NUMBER: 3,
|
|
802
|
+
FALLBACK_TO_NUMBER_FORMAT: 4,
|
|
803
|
+
CANNOT_FORMAT_DATE: 5,
|
|
804
|
+
FALLBACK_TO_DATE_FORMAT: 6,
|
|
805
|
+
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7,
|
|
806
|
+
INVALID_NUMBER_ARGUMENT: 8,
|
|
807
|
+
INVALID_DATE_ARGUMENT: 9
|
|
909
808
|
};
|
|
910
|
-
const CORE_WARN_CODES_EXTEND_POINT =
|
|
809
|
+
const CORE_WARN_CODES_EXTEND_POINT = 10;
|
|
911
810
|
/** @internal */
|
|
912
811
|
const warnMessages = {
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
812
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
|
|
813
|
+
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
814
|
+
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
815
|
+
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
816
|
+
[CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
|
|
817
|
+
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
818
|
+
[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.`,
|
|
819
|
+
[CoreWarnCodes.INVALID_NUMBER_ARGUMENT]: `Invalid argument for number formatting: expected a number but received '{value}'.`,
|
|
820
|
+
[CoreWarnCodes.INVALID_DATE_ARGUMENT]: `Invalid argument for datetime formatting: expected a Date, number, or ISO string but received '{value}'.`
|
|
920
821
|
};
|
|
921
822
|
function getWarnMessage(code, ...args) {
|
|
922
|
-
|
|
823
|
+
return format$1(warnMessages[code], ...args);
|
|
923
824
|
}
|
|
924
|
-
|
|
925
|
-
|
|
825
|
+
//#endregion
|
|
826
|
+
//#region packages/core-base/src/context.ts
|
|
926
827
|
/**
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
const VERSION =
|
|
828
|
+
* Intlify core-base version
|
|
829
|
+
* @internal
|
|
830
|
+
*/
|
|
831
|
+
const VERSION = "12.0.0-alpha.5";
|
|
931
832
|
const NOT_RESOLVED = -1;
|
|
932
|
-
const DEFAULT_LOCALE =
|
|
933
|
-
const MISSING_RESOLVE_VALUE =
|
|
833
|
+
const DEFAULT_LOCALE = "en-US";
|
|
834
|
+
const MISSING_RESOLVE_VALUE = "";
|
|
934
835
|
const capitalize = (str) => `${str.charAt(0).toLocaleUpperCase()}${str.substr(1)}`;
|
|
935
836
|
function getDefaultLinkedModifiers() {
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
return type === 'text' && isString(val)
|
|
948
|
-
? val.toLowerCase()
|
|
949
|
-
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
950
|
-
? val.children.toLowerCase()
|
|
951
|
-
: val;
|
|
952
|
-
},
|
|
953
|
-
capitalize: (val, type) => {
|
|
954
|
-
// prettier-ignore
|
|
955
|
-
return (type === 'text' && isString(val)
|
|
956
|
-
? capitalize(val)
|
|
957
|
-
: type === 'vnode' && isObject(val) && '__v_isVNode' in val
|
|
958
|
-
? capitalize(val.children)
|
|
959
|
-
: val);
|
|
960
|
-
}
|
|
961
|
-
};
|
|
837
|
+
return {
|
|
838
|
+
upper: (val, type) => {
|
|
839
|
+
return type === "text" && isString(val) ? val.toUpperCase() : type === "vnode" && isObject(val) && "__v_isVNode" in val ? val.children.toUpperCase() : val;
|
|
840
|
+
},
|
|
841
|
+
lower: (val, type) => {
|
|
842
|
+
return type === "text" && isString(val) ? val.toLowerCase() : type === "vnode" && isObject(val) && "__v_isVNode" in val ? val.children.toLowerCase() : val;
|
|
843
|
+
},
|
|
844
|
+
capitalize: (val, type) => {
|
|
845
|
+
return type === "text" && isString(val) ? capitalize(val) : type === "vnode" && isObject(val) && "__v_isVNode" in val ? capitalize(val.children) : val;
|
|
846
|
+
}
|
|
847
|
+
};
|
|
962
848
|
}
|
|
963
849
|
let _compiler;
|
|
850
|
+
/**
|
|
851
|
+
* Register the message compiler
|
|
852
|
+
*
|
|
853
|
+
* @param compiler - A {@link MessageCompiler} function
|
|
854
|
+
*
|
|
855
|
+
* @deprecated Use `messageCompiler` option of `createI18n` or `createCoreContext` instead. This function will be removed in v13.
|
|
856
|
+
*
|
|
857
|
+
* @VueI18nGeneral
|
|
858
|
+
*/
|
|
964
859
|
function registerMessageCompiler(compiler) {
|
|
965
|
-
|
|
860
|
+
warnOnce(`registerMessageCompiler is deprecated. Use 'messageCompiler' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
861
|
+
_compiler = compiler;
|
|
966
862
|
}
|
|
967
863
|
let _resolver;
|
|
968
864
|
/**
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
865
|
+
* Register the message resolver
|
|
866
|
+
*
|
|
867
|
+
* @param resolver - A {@link MessageResolver} function
|
|
868
|
+
*
|
|
869
|
+
* @deprecated Use `messageResolver` option of `createI18n` or `createCoreContext` instead. This function will be removed in v13.
|
|
870
|
+
*
|
|
871
|
+
* @VueI18nGeneral
|
|
872
|
+
*/
|
|
975
873
|
function registerMessageResolver(resolver) {
|
|
976
|
-
|
|
874
|
+
warnOnce(`registerMessageResolver is deprecated. Use 'messageResolver' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
875
|
+
_resolver = resolver;
|
|
977
876
|
}
|
|
978
877
|
let _fallbacker;
|
|
979
878
|
/**
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
879
|
+
* Register the locale fallbacker
|
|
880
|
+
*
|
|
881
|
+
* @param fallbacker - A {@link LocaleFallbacker} function
|
|
882
|
+
*
|
|
883
|
+
* @deprecated Use `localeFallbacker` option of `createI18n` or `createCoreContext` instead. This function will be removed in v13.
|
|
884
|
+
*
|
|
885
|
+
* @VueI18nGeneral
|
|
886
|
+
*/
|
|
986
887
|
function registerLocaleFallbacker(fallbacker) {
|
|
987
|
-
|
|
888
|
+
warnOnce(`registerLocaleFallbacker is deprecated. Use 'localeFallbacker' option of 'createI18n' or 'createCoreContext' instead.`);
|
|
889
|
+
_fallbacker = fallbacker;
|
|
988
890
|
}
|
|
989
|
-
// Additional Meta for Intlify DevTools
|
|
990
|
-
let _additionalMeta = null;
|
|
991
|
-
/* #__NO_SIDE_EFFECTS__ */
|
|
992
|
-
const setAdditionalMeta = (meta) => {
|
|
993
|
-
_additionalMeta = meta;
|
|
994
|
-
};
|
|
995
|
-
/* #__NO_SIDE_EFFECTS__ */
|
|
996
|
-
const getAdditionalMeta = () => _additionalMeta;
|
|
997
|
-
let _fallbackContext = null;
|
|
998
|
-
const setFallbackContext = (context) => {
|
|
999
|
-
_fallbackContext = context;
|
|
1000
|
-
};
|
|
1001
|
-
const getFallbackContext = () => _fallbackContext;
|
|
1002
|
-
// ID for CoreContext
|
|
1003
891
|
let _cid = 0;
|
|
1004
892
|
function createCoreContext(options = {}) {
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
: undefined;
|
|
1063
|
-
// setup internal options
|
|
1064
|
-
const internalOptions = options;
|
|
1065
|
-
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters)
|
|
1066
|
-
? internalOptions.__datetimeFormatters
|
|
1067
|
-
: new Map()
|
|
1068
|
-
;
|
|
1069
|
-
const __numberFormatters = isObject(internalOptions.__numberFormatters)
|
|
1070
|
-
? internalOptions.__numberFormatters
|
|
1071
|
-
: new Map()
|
|
1072
|
-
;
|
|
1073
|
-
const __meta = isObject(internalOptions.__meta) ? internalOptions.__meta : {};
|
|
1074
|
-
_cid++;
|
|
1075
|
-
const context = {
|
|
1076
|
-
version,
|
|
1077
|
-
cid: _cid,
|
|
1078
|
-
locale,
|
|
1079
|
-
fallbackLocale,
|
|
1080
|
-
messages,
|
|
1081
|
-
modifiers,
|
|
1082
|
-
pluralRules,
|
|
1083
|
-
missing,
|
|
1084
|
-
missingWarn,
|
|
1085
|
-
fallbackWarn,
|
|
1086
|
-
fallbackFormat,
|
|
1087
|
-
unresolving,
|
|
1088
|
-
postTranslation,
|
|
1089
|
-
processor,
|
|
1090
|
-
warnHtmlMessage,
|
|
1091
|
-
escapeParameter,
|
|
1092
|
-
messageCompiler,
|
|
1093
|
-
messageResolver,
|
|
1094
|
-
localeFallbacker,
|
|
1095
|
-
fallbackContext,
|
|
1096
|
-
onWarn,
|
|
1097
|
-
__meta
|
|
1098
|
-
};
|
|
1099
|
-
{
|
|
1100
|
-
context.datetimeFormats = datetimeFormats;
|
|
1101
|
-
context.numberFormats = numberFormats;
|
|
1102
|
-
context.__datetimeFormatters = __datetimeFormatters;
|
|
1103
|
-
context.__numberFormatters = __numberFormatters;
|
|
1104
|
-
}
|
|
1105
|
-
// for vue-devtools timeline event
|
|
1106
|
-
{
|
|
1107
|
-
context.__v_emitter =
|
|
1108
|
-
internalOptions.__v_emitter != null
|
|
1109
|
-
? internalOptions.__v_emitter
|
|
1110
|
-
: undefined;
|
|
1111
|
-
}
|
|
1112
|
-
// NOTE: experimental !!
|
|
1113
|
-
{
|
|
1114
|
-
initI18nDevTools(context, version, __meta);
|
|
1115
|
-
}
|
|
1116
|
-
return context;
|
|
893
|
+
const onWarn = isFunction(options.onWarn) ? options.onWarn : warn;
|
|
894
|
+
const version = isString(options.version) ? options.version : VERSION;
|
|
895
|
+
const locale = isString(options.locale) || isFunction(options.locale) ? options.locale : DEFAULT_LOCALE;
|
|
896
|
+
const _locale = isFunction(locale) ? DEFAULT_LOCALE : locale;
|
|
897
|
+
const fallbackLocale = isArray(options.fallbackLocale) || isPlainObject(options.fallbackLocale) || isString(options.fallbackLocale) || options.fallbackLocale === false ? options.fallbackLocale : _locale;
|
|
898
|
+
const messages = isPlainObject(options.messages) ? options.messages : createResources(_locale);
|
|
899
|
+
const datetimeFormats = isPlainObject(options.datetimeFormats) ? options.datetimeFormats : createResources(_locale);
|
|
900
|
+
const numberFormats = isPlainObject(options.numberFormats) ? options.numberFormats : createResources(_locale);
|
|
901
|
+
const modifiers = assign(create(), options.modifiers, getDefaultLinkedModifiers());
|
|
902
|
+
const pluralRules = options.pluralRules || create();
|
|
903
|
+
const missing = isFunction(options.missing) ? options.missing : null;
|
|
904
|
+
const missingWarn = isBoolean(options.missingWarn) || isRegExp(options.missingWarn) ? options.missingWarn : true;
|
|
905
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) || isRegExp(options.fallbackWarn) ? options.fallbackWarn : true;
|
|
906
|
+
const fallbackFormat = !!options.fallbackFormat;
|
|
907
|
+
const unresolving = !!options.unresolving;
|
|
908
|
+
const postTranslation = isFunction(options.postTranslation) ? options.postTranslation : null;
|
|
909
|
+
const processor = isPlainObject(options.processor) ? options.processor : null;
|
|
910
|
+
const warnHtmlMessage = isBoolean(options.warnHtmlMessage) ? options.warnHtmlMessage : true;
|
|
911
|
+
const escapeParameter = !!options.escapeParameter;
|
|
912
|
+
const messageCompiler = isFunction(options.messageCompiler) ? options.messageCompiler : _compiler || null;
|
|
913
|
+
if (isFunction(options.messageCompiler)) warnOnce(getWarnMessage(CoreWarnCodes.EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER));
|
|
914
|
+
const messageResolver = isFunction(options.messageResolver) ? options.messageResolver : _resolver || resolveWithKeyValue;
|
|
915
|
+
const localeFallbacker = isFunction(options.localeFallbacker) ? options.localeFallbacker : _fallbacker || fallbackWithSimple;
|
|
916
|
+
const fallbackContext = isObject(options.fallbackContext) ? options.fallbackContext : void 0;
|
|
917
|
+
const internalOptions = options;
|
|
918
|
+
const __datetimeFormatters = isObject(internalOptions.__datetimeFormatters) ? internalOptions.__datetimeFormatters : /* @__PURE__ */ new Map();
|
|
919
|
+
const __numberFormatters = isObject(internalOptions.__numberFormatters) ? internalOptions.__numberFormatters : /* @__PURE__ */ new Map();
|
|
920
|
+
_cid++;
|
|
921
|
+
const context = {
|
|
922
|
+
version,
|
|
923
|
+
cid: _cid,
|
|
924
|
+
locale,
|
|
925
|
+
fallbackLocale,
|
|
926
|
+
messages,
|
|
927
|
+
modifiers,
|
|
928
|
+
pluralRules,
|
|
929
|
+
missing,
|
|
930
|
+
missingWarn,
|
|
931
|
+
fallbackWarn,
|
|
932
|
+
fallbackFormat,
|
|
933
|
+
unresolving,
|
|
934
|
+
postTranslation,
|
|
935
|
+
processor,
|
|
936
|
+
warnHtmlMessage,
|
|
937
|
+
escapeParameter,
|
|
938
|
+
messageCompiler,
|
|
939
|
+
messageResolver,
|
|
940
|
+
localeFallbacker,
|
|
941
|
+
fallbackContext,
|
|
942
|
+
onWarn
|
|
943
|
+
};
|
|
944
|
+
context.datetimeFormats = datetimeFormats;
|
|
945
|
+
context.numberFormats = numberFormats;
|
|
946
|
+
context.__datetimeFormatters = __datetimeFormatters;
|
|
947
|
+
context.__numberFormatters = __numberFormatters;
|
|
948
|
+
context.__v_emitter = internalOptions.__v_emitter != null ? internalOptions.__v_emitter : void 0;
|
|
949
|
+
return context;
|
|
1117
950
|
}
|
|
1118
951
|
const createResources = (locale) => ({ [locale]: create() });
|
|
1119
|
-
// TODO: API documentation, if `@intlify/core` will be documented as a separate package
|
|
1120
952
|
/**
|
|
1121
|
-
|
|
1122
|
-
|
|
953
|
+
* @version 12.0.0
|
|
954
|
+
*/
|
|
1123
955
|
function getLocaleMessage(ctx, locale) {
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
return dest;
|
|
1131
|
-
}
|
|
1132
|
-
// TODO: API documentation, if `@intlify/core` will be documented as a separate package
|
|
956
|
+
const src = getOwn(ctx.messages, locale);
|
|
957
|
+
if (src == null) return;
|
|
958
|
+
const dest = create();
|
|
959
|
+
deepCopy(src, dest);
|
|
960
|
+
return dest;
|
|
961
|
+
}
|
|
1133
962
|
/**
|
|
1134
|
-
|
|
1135
|
-
|
|
963
|
+
* @version 12.0.0
|
|
964
|
+
*/
|
|
1136
965
|
function setLocaleMessage(ctx, locale, messages) {
|
|
1137
|
-
|
|
966
|
+
if (isObjectPrototypeKey(locale)) return;
|
|
967
|
+
ctx.messages[locale] = messages;
|
|
1138
968
|
}
|
|
1139
969
|
/** @internal */
|
|
1140
970
|
function isTranslateFallbackWarn(fallback, key) {
|
|
1141
|
-
|
|
971
|
+
return isRegExp(fallback) ? fallback.test(key) : fallback;
|
|
1142
972
|
}
|
|
1143
973
|
/** @internal */
|
|
1144
974
|
function isTranslateMissingWarn(missing, key) {
|
|
1145
|
-
|
|
975
|
+
return isRegExp(missing) ? missing.test(key) : missing;
|
|
1146
976
|
}
|
|
1147
977
|
/** @internal */
|
|
1148
978
|
function handleMissing(context, key, locale, missingWarn, type) {
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
979
|
+
const { missing, onWarn } = context;
|
|
980
|
+
{
|
|
981
|
+
const emitter = context.__v_emitter;
|
|
982
|
+
if (emitter) emitter.emit("missing", {
|
|
983
|
+
locale,
|
|
984
|
+
key,
|
|
985
|
+
type,
|
|
986
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
987
|
+
});
|
|
988
|
+
}
|
|
989
|
+
if (missing !== null) {
|
|
990
|
+
const ret = missing(context, locale, key, type);
|
|
991
|
+
return isString(ret) ? ret : key;
|
|
992
|
+
} else {
|
|
993
|
+
if (isTranslateMissingWarn(missingWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, {
|
|
994
|
+
key,
|
|
995
|
+
locale
|
|
996
|
+
}));
|
|
997
|
+
return key;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
function invalidateLocaleChainCache(ctx, fallback) {
|
|
1001
|
+
const context = ctx;
|
|
1002
|
+
if (isObject(fallback)) context.__localeChainObjectCache?.delete(fallback);
|
|
1003
|
+
else context.__localeChainCache?.delete(fallback);
|
|
1172
1004
|
}
|
|
1173
1005
|
/** @internal */
|
|
1174
1006
|
function updateFallbackLocale(ctx, locale, fallback) {
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1007
|
+
invalidateLocaleChainCache(ctx, fallback);
|
|
1008
|
+
if (ctx.fallbackContext) invalidateLocaleChainCache(ctx.fallbackContext, fallback);
|
|
1009
|
+
ctx.localeFallbacker(ctx, fallback, locale);
|
|
1178
1010
|
}
|
|
1179
1011
|
/** @internal */
|
|
1180
1012
|
function isAlmostSameLocale(locale, compareLocale) {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
return locale.split('-')[0] === compareLocale.split('-')[0];
|
|
1013
|
+
if (locale === compareLocale) return false;
|
|
1014
|
+
return locale.split("-")[0] === compareLocale.split("-")[0];
|
|
1184
1015
|
}
|
|
1185
1016
|
/** @internal */
|
|
1186
1017
|
function isImplicitFallback(targetLocale, locales) {
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1018
|
+
const index = locales.indexOf(targetLocale);
|
|
1019
|
+
if (index === -1) return false;
|
|
1020
|
+
for (let i = index + 1; i < locales.length; i++) if (isAlmostSameLocale(targetLocale, locales[i])) return true;
|
|
1021
|
+
return false;
|
|
1022
|
+
}
|
|
1023
|
+
//#endregion
|
|
1024
|
+
//#region packages/core-base/src/formatter.ts
|
|
1025
|
+
function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type) {
|
|
1026
|
+
const { fallbackLocale, localeFallbacker, onWarn } = context;
|
|
1027
|
+
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
1028
|
+
let from = locale;
|
|
1029
|
+
for (let i = 0; i < locales.length; i++) {
|
|
1030
|
+
const targetLocale = locales[i];
|
|
1031
|
+
if (locale !== targetLocale && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(type === "datetime format" ? CoreWarnCodes.FALLBACK_TO_DATE_FORMAT : CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1032
|
+
key,
|
|
1033
|
+
target: targetLocale
|
|
1034
|
+
}));
|
|
1035
|
+
if (locale !== targetLocale) {
|
|
1036
|
+
const emitter = context.__v_emitter;
|
|
1037
|
+
if (emitter) emitter.emit("fallback", {
|
|
1038
|
+
type,
|
|
1039
|
+
key,
|
|
1040
|
+
from,
|
|
1041
|
+
to: targetLocale,
|
|
1042
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1043
|
+
});
|
|
1044
|
+
}
|
|
1045
|
+
const format = (formats[targetLocale] || {})[key];
|
|
1046
|
+
if (isPlainObject(format) && isString(targetLocale)) return targetLocale;
|
|
1047
|
+
handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1048
|
+
from = targetLocale;
|
|
1049
|
+
}
|
|
1050
|
+
return null;
|
|
1051
|
+
}
|
|
1052
|
+
function getFormatterCacheKey(locale, key, overrides) {
|
|
1053
|
+
let id = `${locale}__${key}`;
|
|
1054
|
+
if (isPlainObject(overrides) && !isKeylessObject(overrides)) id = `${id}__${JSON.stringify(overrides)}`;
|
|
1055
|
+
return id;
|
|
1056
|
+
}
|
|
1057
|
+
function clearFormatCache(formatters, locale, format) {
|
|
1058
|
+
for (const key in format) {
|
|
1059
|
+
const prefix = `${locale}__${key}`;
|
|
1060
|
+
for (const id of formatters.keys()) if (id === prefix || id.startsWith(`${prefix}__`)) formatters.delete(id);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
function parseFormatArgs(args, options, initialOverrides, optionsKeys) {
|
|
1064
|
+
const [, arg2, arg3, arg4] = args;
|
|
1065
|
+
let overrides = initialOverrides;
|
|
1066
|
+
if (isString(arg2)) options.key = arg2;
|
|
1067
|
+
else if (isPlainObject(arg2)) Object.keys(arg2).forEach((key) => {
|
|
1068
|
+
if (optionsKeys.includes(key)) overrides[key] = arg2[key];
|
|
1069
|
+
else options[key] = arg2[key];
|
|
1070
|
+
});
|
|
1071
|
+
if (isString(arg3)) options.locale = arg3;
|
|
1072
|
+
else if (isPlainObject(arg3)) overrides = arg3;
|
|
1073
|
+
if (isPlainObject(arg4)) overrides = arg4;
|
|
1074
|
+
return overrides;
|
|
1075
|
+
}
|
|
1076
|
+
//#endregion
|
|
1077
|
+
//#region packages/core-base/src/intl.ts
|
|
1078
|
+
const intlDefined = typeof Intl !== "undefined";
|
|
1201
1079
|
const Availabilities = {
|
|
1202
|
-
|
|
1203
|
-
|
|
1080
|
+
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== "undefined",
|
|
1081
|
+
numberFormat: intlDefined && typeof Intl.NumberFormat !== "undefined"
|
|
1204
1082
|
};
|
|
1205
|
-
|
|
1206
|
-
|
|
1083
|
+
//#endregion
|
|
1084
|
+
//#region packages/core-base/src/datetime.ts
|
|
1207
1085
|
function datetime(context, ...args) {
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
if (locale !== targetLocale &&
|
|
1238
|
-
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
1239
|
-
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
1240
|
-
key,
|
|
1241
|
-
target: targetLocale
|
|
1242
|
-
}));
|
|
1243
|
-
}
|
|
1244
|
-
// for vue-devtools timeline event
|
|
1245
|
-
if (locale !== targetLocale) {
|
|
1246
|
-
const emitter = context.__v_emitter;
|
|
1247
|
-
if (emitter) {
|
|
1248
|
-
emitter.emit('fallback', {
|
|
1249
|
-
type,
|
|
1250
|
-
key,
|
|
1251
|
-
from,
|
|
1252
|
-
to,
|
|
1253
|
-
groupId: `${type}:${key}`
|
|
1254
|
-
});
|
|
1255
|
-
}
|
|
1256
|
-
}
|
|
1257
|
-
datetimeFormat =
|
|
1258
|
-
datetimeFormats[targetLocale] || {};
|
|
1259
|
-
format = datetimeFormat[key];
|
|
1260
|
-
if (isPlainObject(format))
|
|
1261
|
-
break;
|
|
1262
|
-
handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1263
|
-
from = to;
|
|
1264
|
-
}
|
|
1265
|
-
// checking format and target locale
|
|
1266
|
-
if (!isPlainObject(format) || !isString(targetLocale)) {
|
|
1267
|
-
return unresolving ? NOT_RESOLVED : key;
|
|
1268
|
-
}
|
|
1269
|
-
let id = `${targetLocale}__${key}`;
|
|
1270
|
-
if (!isEmptyObject(overrides)) {
|
|
1271
|
-
id = `${id}__${JSON.stringify(overrides)}`;
|
|
1272
|
-
}
|
|
1273
|
-
let formatter = __datetimeFormatters.get(id);
|
|
1274
|
-
if (!formatter) {
|
|
1275
|
-
formatter = new Intl.DateTimeFormat(targetLocale, assign({}, format, overrides));
|
|
1276
|
-
__datetimeFormatters.set(id, formatter);
|
|
1277
|
-
}
|
|
1278
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1086
|
+
const { datetimeFormats, unresolving, onWarn } = context;
|
|
1087
|
+
const { __datetimeFormatters } = context;
|
|
1088
|
+
if (!Availabilities.dateTimeFormat) {
|
|
1089
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
1090
|
+
return "";
|
|
1091
|
+
}
|
|
1092
|
+
if (!isString(args[0]) && !isDate(args[0]) && !isNumber(args[0])) {
|
|
1093
|
+
onWarn(getWarnMessage(CoreWarnCodes.INVALID_DATE_ARGUMENT, { value: String(args[0]) }));
|
|
1094
|
+
return "";
|
|
1095
|
+
}
|
|
1096
|
+
const [key, value, options, overrides] = parseDateTimeArgs(...args);
|
|
1097
|
+
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
1098
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1099
|
+
const part = !!options.part;
|
|
1100
|
+
const locale = getLocale(context, options);
|
|
1101
|
+
if (!isString(key) || key === "") {
|
|
1102
|
+
const formatter = new Intl.DateTimeFormat(locale.replace(/!/g, ""), overrides);
|
|
1103
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1104
|
+
}
|
|
1105
|
+
const targetLocale = resolveFormatLocale(context, key, locale, datetimeFormats, missingWarn, fallbackWarn, "datetime format");
|
|
1106
|
+
if (!isString(targetLocale)) return unresolving ? -1 : key;
|
|
1107
|
+
const format = datetimeFormats[targetLocale][key];
|
|
1108
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
1109
|
+
let formatter = __datetimeFormatters.get(id);
|
|
1110
|
+
if (!formatter) {
|
|
1111
|
+
formatter = new Intl.DateTimeFormat(targetLocale, assign({}, format, overrides));
|
|
1112
|
+
__datetimeFormatters.set(id, formatter);
|
|
1113
|
+
}
|
|
1114
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1279
1115
|
}
|
|
1280
1116
|
/** @internal */
|
|
1281
1117
|
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1118
|
+
"localeMatcher",
|
|
1119
|
+
"weekday",
|
|
1120
|
+
"era",
|
|
1121
|
+
"year",
|
|
1122
|
+
"month",
|
|
1123
|
+
"day",
|
|
1124
|
+
"hour",
|
|
1125
|
+
"minute",
|
|
1126
|
+
"second",
|
|
1127
|
+
"timeZoneName",
|
|
1128
|
+
"formatMatcher",
|
|
1129
|
+
"hour12",
|
|
1130
|
+
"timeZone",
|
|
1131
|
+
"dateStyle",
|
|
1132
|
+
"timeStyle",
|
|
1133
|
+
"calendar",
|
|
1134
|
+
"dayPeriod",
|
|
1135
|
+
"numberingSystem",
|
|
1136
|
+
"hourCycle",
|
|
1137
|
+
"fractionalSecondDigits"
|
|
1302
1138
|
];
|
|
1303
1139
|
/** @internal */
|
|
1304
1140
|
function parseDateTimeArgs(...args) {
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
}
|
|
1332
|
-
else if (isDate(arg1)) {
|
|
1333
|
-
if (isNaN(arg1.getTime())) {
|
|
1334
|
-
throw createCoreError(CoreErrorCodes.INVALID_DATE_ARGUMENT);
|
|
1335
|
-
}
|
|
1336
|
-
value = arg1;
|
|
1337
|
-
}
|
|
1338
|
-
else if (isNumber(arg1)) {
|
|
1339
|
-
value = arg1;
|
|
1340
|
-
}
|
|
1341
|
-
else {
|
|
1342
|
-
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1343
|
-
}
|
|
1344
|
-
if (isString(arg2)) {
|
|
1345
|
-
options.key = arg2;
|
|
1346
|
-
}
|
|
1347
|
-
else if (isPlainObject(arg2)) {
|
|
1348
|
-
Object.keys(arg2).forEach(key => {
|
|
1349
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1350
|
-
overrides[key] = arg2[key];
|
|
1351
|
-
}
|
|
1352
|
-
else {
|
|
1353
|
-
options[key] = arg2[key];
|
|
1354
|
-
}
|
|
1355
|
-
});
|
|
1356
|
-
}
|
|
1357
|
-
if (isString(arg3)) {
|
|
1358
|
-
options.locale = arg3;
|
|
1359
|
-
}
|
|
1360
|
-
else if (isPlainObject(arg3)) {
|
|
1361
|
-
overrides = arg3;
|
|
1362
|
-
}
|
|
1363
|
-
if (isPlainObject(arg4)) {
|
|
1364
|
-
overrides = arg4;
|
|
1365
|
-
}
|
|
1366
|
-
return [options.key || '', value, options, overrides];
|
|
1141
|
+
const [arg1] = args;
|
|
1142
|
+
const options = create();
|
|
1143
|
+
const initialOverrides = create();
|
|
1144
|
+
let value;
|
|
1145
|
+
if (isString(arg1)) {
|
|
1146
|
+
const matches = arg1.match(/^(\d{4}-\d{2}-\d{2})(?:(T|\s)(.+))?$/);
|
|
1147
|
+
if (!matches) throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
1148
|
+
const dateTime = matches[3] ? matches[3].trim().startsWith("T") ? `${matches[1].trim()}${matches[3].trim()}` : `${matches[1].trim()}T${matches[3].trim()}` : matches[1].trim();
|
|
1149
|
+
value = new Date(dateTime);
|
|
1150
|
+
try {
|
|
1151
|
+
value.toISOString();
|
|
1152
|
+
} catch {
|
|
1153
|
+
throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
1154
|
+
}
|
|
1155
|
+
} else if (isDate(arg1)) {
|
|
1156
|
+
if (isNaN(arg1.getTime())) throw createCoreError(CoreErrorCodes.INVALID_DATE_ARGUMENT);
|
|
1157
|
+
value = arg1;
|
|
1158
|
+
} else if (isNumber(arg1)) value = arg1;
|
|
1159
|
+
else throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1160
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, DATETIME_FORMAT_OPTIONS_KEYS);
|
|
1161
|
+
return [
|
|
1162
|
+
options.key || "",
|
|
1163
|
+
value,
|
|
1164
|
+
options,
|
|
1165
|
+
overrides
|
|
1166
|
+
];
|
|
1367
1167
|
}
|
|
1368
1168
|
/** @internal */
|
|
1369
1169
|
function clearDateTimeFormat(ctx, locale, format) {
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
continue;
|
|
1375
|
-
}
|
|
1376
|
-
context.__datetimeFormatters.delete(id);
|
|
1377
|
-
}
|
|
1378
|
-
}
|
|
1379
|
-
|
|
1380
|
-
// implementation of `number` function
|
|
1170
|
+
clearFormatCache(ctx.__datetimeFormatters, locale, format);
|
|
1171
|
+
}
|
|
1172
|
+
//#endregion
|
|
1173
|
+
//#region packages/core-base/src/number.ts
|
|
1381
1174
|
function number(context, ...args) {
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
if (locale !== targetLocale &&
|
|
1412
|
-
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
1413
|
-
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1414
|
-
key,
|
|
1415
|
-
target: targetLocale
|
|
1416
|
-
}));
|
|
1417
|
-
}
|
|
1418
|
-
// for vue-devtools timeline event
|
|
1419
|
-
if (locale !== targetLocale) {
|
|
1420
|
-
const emitter = context.__v_emitter;
|
|
1421
|
-
if (emitter) {
|
|
1422
|
-
emitter.emit('fallback', {
|
|
1423
|
-
type,
|
|
1424
|
-
key,
|
|
1425
|
-
from,
|
|
1426
|
-
to,
|
|
1427
|
-
groupId: `${type}:${key}`
|
|
1428
|
-
});
|
|
1429
|
-
}
|
|
1430
|
-
}
|
|
1431
|
-
numberFormat =
|
|
1432
|
-
numberFormats[targetLocale] || {};
|
|
1433
|
-
format = numberFormat[key];
|
|
1434
|
-
if (isPlainObject(format))
|
|
1435
|
-
break;
|
|
1436
|
-
handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1437
|
-
from = to;
|
|
1438
|
-
}
|
|
1439
|
-
// checking format and target locale
|
|
1440
|
-
if (!isPlainObject(format) || !isString(targetLocale)) {
|
|
1441
|
-
return unresolving ? NOT_RESOLVED : key;
|
|
1442
|
-
}
|
|
1443
|
-
let id = `${targetLocale}__${key}`;
|
|
1444
|
-
if (!isEmptyObject(overrides)) {
|
|
1445
|
-
id = `${id}__${JSON.stringify(overrides)}`;
|
|
1446
|
-
}
|
|
1447
|
-
let formatter = __numberFormatters.get(id);
|
|
1448
|
-
if (!formatter) {
|
|
1449
|
-
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
1450
|
-
__numberFormatters.set(id, formatter);
|
|
1451
|
-
}
|
|
1452
|
-
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1175
|
+
const { numberFormats, unresolving, onWarn } = context;
|
|
1176
|
+
const { __numberFormatters } = context;
|
|
1177
|
+
if (!Availabilities.numberFormat) {
|
|
1178
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
1179
|
+
return "";
|
|
1180
|
+
}
|
|
1181
|
+
if (!isNumber(args[0])) {
|
|
1182
|
+
onWarn(getWarnMessage(CoreWarnCodes.INVALID_NUMBER_ARGUMENT, { value: String(args[0]) }));
|
|
1183
|
+
return "";
|
|
1184
|
+
}
|
|
1185
|
+
const [key, value, options, overrides] = parseNumberArgs(...args);
|
|
1186
|
+
const missingWarn = isBoolean(options.missingWarn) ? options.missingWarn : context.missingWarn;
|
|
1187
|
+
const fallbackWarn = isBoolean(options.fallbackWarn) ? options.fallbackWarn : context.fallbackWarn;
|
|
1188
|
+
const part = !!options.part;
|
|
1189
|
+
const locale = getLocale(context, options);
|
|
1190
|
+
if (!isString(key) || key === "") {
|
|
1191
|
+
const formatter = new Intl.NumberFormat(locale.replace(/!/g, ""), overrides);
|
|
1192
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1193
|
+
}
|
|
1194
|
+
const targetLocale = resolveFormatLocale(context, key, locale, numberFormats, missingWarn, fallbackWarn, "number format");
|
|
1195
|
+
if (!isString(targetLocale)) return unresolving ? -1 : key;
|
|
1196
|
+
const format = numberFormats[targetLocale][key];
|
|
1197
|
+
const id = getFormatterCacheKey(targetLocale, key, overrides);
|
|
1198
|
+
let formatter = __numberFormatters.get(id);
|
|
1199
|
+
if (!formatter) {
|
|
1200
|
+
formatter = new Intl.NumberFormat(targetLocale, assign({}, format, overrides));
|
|
1201
|
+
__numberFormatters.set(id, formatter);
|
|
1202
|
+
}
|
|
1203
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1453
1204
|
}
|
|
1454
1205
|
/** @internal */
|
|
1455
1206
|
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1207
|
+
"localeMatcher",
|
|
1208
|
+
"style",
|
|
1209
|
+
"currency",
|
|
1210
|
+
"currencyDisplay",
|
|
1211
|
+
"currencySign",
|
|
1212
|
+
"useGrouping",
|
|
1213
|
+
"minimumIntegerDigits",
|
|
1214
|
+
"minimumFractionDigits",
|
|
1215
|
+
"maximumFractionDigits",
|
|
1216
|
+
"minimumSignificantDigits",
|
|
1217
|
+
"maximumSignificantDigits",
|
|
1218
|
+
"compactDisplay",
|
|
1219
|
+
"notation",
|
|
1220
|
+
"signDisplay",
|
|
1221
|
+
"unit",
|
|
1222
|
+
"unitDisplay",
|
|
1223
|
+
"roundingMode",
|
|
1224
|
+
"roundingPriority",
|
|
1225
|
+
"roundingIncrement",
|
|
1226
|
+
"trailingZeroDisplay"
|
|
1476
1227
|
];
|
|
1477
1228
|
/** @internal */
|
|
1478
1229
|
function parseNumberArgs(...args) {
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1492
|
-
overrides[key] = arg2[key];
|
|
1493
|
-
}
|
|
1494
|
-
else {
|
|
1495
|
-
options[key] = arg2[key];
|
|
1496
|
-
}
|
|
1497
|
-
});
|
|
1498
|
-
}
|
|
1499
|
-
if (isString(arg3)) {
|
|
1500
|
-
options.locale = arg3;
|
|
1501
|
-
}
|
|
1502
|
-
else if (isPlainObject(arg3)) {
|
|
1503
|
-
overrides = arg3;
|
|
1504
|
-
}
|
|
1505
|
-
if (isPlainObject(arg4)) {
|
|
1506
|
-
overrides = arg4;
|
|
1507
|
-
}
|
|
1508
|
-
return [options.key || '', value, options, overrides];
|
|
1230
|
+
const [arg1] = args;
|
|
1231
|
+
const options = create();
|
|
1232
|
+
const initialOverrides = create();
|
|
1233
|
+
if (!isNumber(arg1)) throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1234
|
+
const value = arg1;
|
|
1235
|
+
const overrides = parseFormatArgs(args, options, initialOverrides, NUMBER_FORMAT_OPTIONS_KEYS);
|
|
1236
|
+
return [
|
|
1237
|
+
options.key || "",
|
|
1238
|
+
value,
|
|
1239
|
+
options,
|
|
1240
|
+
overrides
|
|
1241
|
+
];
|
|
1509
1242
|
}
|
|
1510
1243
|
/** @internal */
|
|
1511
1244
|
function clearNumberFormat(ctx, locale, format) {
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
continue;
|
|
1517
|
-
}
|
|
1518
|
-
context.__numberFormatters.delete(id);
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
|
|
1245
|
+
clearFormatCache(ctx.__numberFormatters, locale, format);
|
|
1246
|
+
}
|
|
1247
|
+
//#endregion
|
|
1248
|
+
//#region packages/core-base/src/runtime.ts
|
|
1522
1249
|
const DEFAULT_MODIFIER = (str) => str;
|
|
1523
|
-
const DEFAULT_MESSAGE = (
|
|
1524
|
-
const DEFAULT_MESSAGE_DATA_TYPE =
|
|
1525
|
-
const DEFAULT_NORMALIZE = (values) => values.length === 0 ?
|
|
1250
|
+
const DEFAULT_MESSAGE = (_ctx) => "";
|
|
1251
|
+
const DEFAULT_MESSAGE_DATA_TYPE = "text";
|
|
1252
|
+
const DEFAULT_NORMALIZE = (values) => values.length === 0 ? "" : join(values);
|
|
1526
1253
|
const DEFAULT_INTERPOLATE = toDisplayString;
|
|
1527
1254
|
function pluralDefault(choice, choicesLength) {
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1255
|
+
choice = Math.abs(choice);
|
|
1256
|
+
if (choicesLength === 2) return choice === 1 ? 0 : 1;
|
|
1257
|
+
return Math.min(choice, 2);
|
|
1258
|
+
}
|
|
1259
|
+
const PLURAL_CATEGORY_ORDER = [
|
|
1260
|
+
"zero",
|
|
1261
|
+
"one",
|
|
1262
|
+
"two",
|
|
1263
|
+
"few",
|
|
1264
|
+
"many",
|
|
1265
|
+
"other"
|
|
1266
|
+
];
|
|
1267
|
+
const intlPluralRulesCache = /* @__PURE__ */ new Map();
|
|
1268
|
+
function getIntlPluralRules(locale) {
|
|
1269
|
+
let cached = intlPluralRulesCache.get(locale);
|
|
1270
|
+
if (!cached) {
|
|
1271
|
+
const rules = new Intl.PluralRules(locale);
|
|
1272
|
+
cached = {
|
|
1273
|
+
rules,
|
|
1274
|
+
categories: rules.resolvedOptions().pluralCategories.slice().sort((a, b) => PLURAL_CATEGORY_ORDER.indexOf(a) - PLURAL_CATEGORY_ORDER.indexOf(b))
|
|
1275
|
+
};
|
|
1276
|
+
intlPluralRulesCache.set(locale, cached);
|
|
1277
|
+
}
|
|
1278
|
+
return cached;
|
|
1538
1279
|
}
|
|
1539
1280
|
function getPluralIndex(options) {
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
? options.pluralIndex
|
|
1543
|
-
: -1;
|
|
1544
|
-
// prettier-ignore
|
|
1545
|
-
return options.named && (isNumber(options.named.count) || isNumber(options.named.n))
|
|
1546
|
-
? isNumber(options.named.count)
|
|
1547
|
-
? options.named.count
|
|
1548
|
-
: isNumber(options.named.n)
|
|
1549
|
-
? options.named.n
|
|
1550
|
-
: index
|
|
1551
|
-
: index;
|
|
1552
|
-
}
|
|
1553
|
-
function normalizeNamed(pluralIndex, props) {
|
|
1554
|
-
if (!props.count) {
|
|
1555
|
-
props.count = pluralIndex;
|
|
1556
|
-
}
|
|
1557
|
-
if (!props.n) {
|
|
1558
|
-
props.n = pluralIndex;
|
|
1559
|
-
}
|
|
1281
|
+
if (isNumber(options.pluralIndex)) return options.pluralIndex;
|
|
1282
|
+
return isNumber(options.named?.count) ? options.named.count : isNumber(options.named?.n) ? options.named.n : -1;
|
|
1560
1283
|
}
|
|
1561
1284
|
function createMessageContext(options = {}) {
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
if (isString(arg2)) {
|
|
1627
|
-
type = arg2 || type;
|
|
1628
|
-
}
|
|
1629
|
-
}
|
|
1630
|
-
const ret = message(key, true)(ctx);
|
|
1631
|
-
const msg =
|
|
1632
|
-
// The message in vnode resolved with linked are returned as an array by processor.nomalize
|
|
1633
|
-
type === 'vnode' && isArray(ret) && modifier
|
|
1634
|
-
? ret[0]
|
|
1635
|
-
: ret;
|
|
1636
|
-
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
1637
|
-
};
|
|
1638
|
-
const ctx = {
|
|
1639
|
-
["list" /* HelperNameMap.LIST */]: list,
|
|
1640
|
-
["named" /* HelperNameMap.NAMED */]: named,
|
|
1641
|
-
["plural" /* HelperNameMap.PLURAL */]: plural,
|
|
1642
|
-
["linked" /* HelperNameMap.LINKED */]: linked,
|
|
1643
|
-
["message" /* HelperNameMap.MESSAGE */]: message,
|
|
1644
|
-
["type" /* HelperNameMap.TYPE */]: type,
|
|
1645
|
-
["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
|
|
1646
|
-
["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
|
|
1647
|
-
["values" /* HelperNameMap.VALUES */]: assign(create(), _list, _named)
|
|
1648
|
-
};
|
|
1649
|
-
return ctx;
|
|
1650
|
-
}
|
|
1651
|
-
|
|
1652
|
-
const NOOP_MESSAGE_FUNCTION = () => '';
|
|
1285
|
+
const locale = options.locale;
|
|
1286
|
+
const pluralIndex = getPluralIndex(options);
|
|
1287
|
+
let resolvedPluralRule;
|
|
1288
|
+
if (isString(locale) && isFunction(options.pluralRules?.[locale])) resolvedPluralRule = options.pluralRules[locale];
|
|
1289
|
+
else if (isString(locale) && typeof Intl !== "undefined" && typeof Intl.PluralRules !== "undefined") resolvedPluralRule = (choice, choicesLength) => {
|
|
1290
|
+
const { rules, categories } = getIntlPluralRules(locale);
|
|
1291
|
+
if (choicesLength > categories.length) return pluralDefault(choice, choicesLength);
|
|
1292
|
+
const category = rules.select(Math.abs(choice));
|
|
1293
|
+
const index = categories.indexOf(category);
|
|
1294
|
+
return Math.min(index === -1 ? choicesLength - 1 : index, choicesLength - 1);
|
|
1295
|
+
};
|
|
1296
|
+
else resolvedPluralRule = pluralDefault;
|
|
1297
|
+
const orgPluralRule = resolvedPluralRule === pluralDefault ? void 0 : pluralDefault;
|
|
1298
|
+
const plural = (messages) => messages[resolvedPluralRule(pluralIndex, messages.length, orgPluralRule)];
|
|
1299
|
+
const _list = options.list || [];
|
|
1300
|
+
const list = (index) => _list[index];
|
|
1301
|
+
const _named = options.named || create();
|
|
1302
|
+
if (isNumber(options.pluralIndex)) {
|
|
1303
|
+
_named.count ??= options.pluralIndex;
|
|
1304
|
+
_named.n ??= options.pluralIndex;
|
|
1305
|
+
}
|
|
1306
|
+
const named = (key) => _named[key];
|
|
1307
|
+
function message(key, useLinked) {
|
|
1308
|
+
const msg = isFunction(options.messages) ? options.messages(key, !!useLinked) : isObject(options.messages) ? options.messages[key] : false;
|
|
1309
|
+
return !msg ? options.parent ? options.parent.message(key) : DEFAULT_MESSAGE : msg;
|
|
1310
|
+
}
|
|
1311
|
+
const _modifier = (name) => options.modifiers ? options.modifiers[name] : DEFAULT_MODIFIER;
|
|
1312
|
+
const normalize = isFunction(options.processor?.normalize) ? options.processor.normalize : DEFAULT_NORMALIZE;
|
|
1313
|
+
const interpolate = isFunction(options.processor?.interpolate) ? options.processor.interpolate : DEFAULT_INTERPOLATE;
|
|
1314
|
+
const type = isString(options.processor?.type) ? options.processor.type : DEFAULT_MESSAGE_DATA_TYPE;
|
|
1315
|
+
const linked = (key, ...args) => {
|
|
1316
|
+
const [arg1, arg2] = args;
|
|
1317
|
+
let type = "text";
|
|
1318
|
+
let modifier = "";
|
|
1319
|
+
if (args.length === 1) {
|
|
1320
|
+
if (isObject(arg1)) {
|
|
1321
|
+
modifier = arg1.modifier || modifier;
|
|
1322
|
+
type = arg1.type || type;
|
|
1323
|
+
} else if (isString(arg1)) modifier = arg1 || modifier;
|
|
1324
|
+
} else if (args.length === 2) {
|
|
1325
|
+
if (isString(arg1)) modifier = arg1 || modifier;
|
|
1326
|
+
if (isString(arg2)) type = arg2 || type;
|
|
1327
|
+
}
|
|
1328
|
+
const ret = message(key, true)(ctx);
|
|
1329
|
+
const resolved = ret === "" || ret === void 0 ? key : ret;
|
|
1330
|
+
const msg = type === "vnode" && isArray(resolved) && modifier ? resolved[0] : resolved;
|
|
1331
|
+
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
1332
|
+
};
|
|
1333
|
+
const ctx = {
|
|
1334
|
+
["list"]: list,
|
|
1335
|
+
["named"]: named,
|
|
1336
|
+
["plural"]: plural,
|
|
1337
|
+
["linked"]: linked,
|
|
1338
|
+
["message"]: message,
|
|
1339
|
+
["type"]: type,
|
|
1340
|
+
["interpolate"]: interpolate,
|
|
1341
|
+
["normalize"]: normalize,
|
|
1342
|
+
["values"]: assign(create(), _list, _named)
|
|
1343
|
+
};
|
|
1344
|
+
return ctx;
|
|
1345
|
+
}
|
|
1346
|
+
//#endregion
|
|
1347
|
+
//#region packages/core-base/src/translate.ts
|
|
1348
|
+
const NOOP_MESSAGE_FUNCTION = () => "";
|
|
1653
1349
|
const isMessageFunction = (val) => isFunction(val);
|
|
1654
|
-
|
|
1350
|
+
/** @internal */
|
|
1351
|
+
const isMessageFormat = (val) => isString(val) || isMessageAST(val) || isMessageFunction(val);
|
|
1655
1352
|
function translate(context, ...args) {
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
];
|
|
1691
|
-
// NOTE:
|
|
1692
|
-
// Fix to work around `ssrTransfrom` bug in Vite.
|
|
1693
|
-
// https://github.com/vitejs/vite/issues/4306
|
|
1694
|
-
// To get around this, use temporary variables.
|
|
1695
|
-
// https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
|
|
1696
|
-
let format = formatScope;
|
|
1697
|
-
// if you use default message, set it as message format!
|
|
1698
|
-
let cacheBaseKey = key;
|
|
1699
|
-
if (!resolvedMessage &&
|
|
1700
|
-
!(isString(format) ||
|
|
1701
|
-
isMessageAST(format) ||
|
|
1702
|
-
isMessageFunction(format))) {
|
|
1703
|
-
if (enableDefaultMsg) {
|
|
1704
|
-
format = defaultMsgOrKey;
|
|
1705
|
-
cacheBaseKey = format;
|
|
1706
|
-
}
|
|
1707
|
-
}
|
|
1708
|
-
// checking message format and target locale
|
|
1709
|
-
if (!resolvedMessage &&
|
|
1710
|
-
(!(isString(format) ||
|
|
1711
|
-
isMessageAST(format) ||
|
|
1712
|
-
isMessageFunction(format)) ||
|
|
1713
|
-
!isString(targetLocale))) {
|
|
1714
|
-
return unresolving ? NOT_RESOLVED : key;
|
|
1715
|
-
}
|
|
1716
|
-
// TODO: refactor
|
|
1717
|
-
if (isString(format) && context.messageCompiler == null) {
|
|
1718
|
-
warn(`The message format compilation is not supported in this build. ` +
|
|
1719
|
-
`Because message compiler isn't included. ` +
|
|
1720
|
-
`You need to pre-compilation all message format. ` +
|
|
1721
|
-
`So translate function return '${key}'.`);
|
|
1722
|
-
return key;
|
|
1723
|
-
}
|
|
1724
|
-
// setup compile error detecting
|
|
1725
|
-
let occurred = false;
|
|
1726
|
-
const onError = () => {
|
|
1727
|
-
occurred = true;
|
|
1728
|
-
};
|
|
1729
|
-
// compile message format
|
|
1730
|
-
const msg = !isMessageFunction(format)
|
|
1731
|
-
? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
|
|
1732
|
-
: format;
|
|
1733
|
-
// if occurred compile error, return the message format
|
|
1734
|
-
if (occurred) {
|
|
1735
|
-
return format;
|
|
1736
|
-
}
|
|
1737
|
-
// evaluate message with context
|
|
1738
|
-
const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
|
|
1739
|
-
const msgContext = createMessageContext(ctxOptions);
|
|
1740
|
-
const messaged = evaluateMessage(context, msg, msgContext);
|
|
1741
|
-
// if use post translation option, proceed it with handler
|
|
1742
|
-
const ret = postTranslation
|
|
1743
|
-
? postTranslation(messaged, key)
|
|
1744
|
-
: messaged;
|
|
1745
|
-
// NOTE: experimental !!
|
|
1746
|
-
{
|
|
1747
|
-
// prettier-ignore
|
|
1748
|
-
const payloads = {
|
|
1749
|
-
timestamp: Date.now(),
|
|
1750
|
-
key: isString(key)
|
|
1751
|
-
? key
|
|
1752
|
-
: isMessageFunction(format)
|
|
1753
|
-
? format.key
|
|
1754
|
-
: '',
|
|
1755
|
-
locale: targetLocale || (isMessageFunction(format)
|
|
1756
|
-
? format.locale
|
|
1757
|
-
: ''),
|
|
1758
|
-
format: isString(format)
|
|
1759
|
-
? format
|
|
1760
|
-
: isMessageFunction(format)
|
|
1761
|
-
? format.source
|
|
1762
|
-
: '',
|
|
1763
|
-
message: ret
|
|
1764
|
-
};
|
|
1765
|
-
payloads.meta = assign({}, context.__meta, getAdditionalMeta() || {});
|
|
1766
|
-
translateDevTools(payloads);
|
|
1767
|
-
}
|
|
1768
|
-
return ret;
|
|
1353
|
+
const [key, options] = parseTranslateArgs(...args);
|
|
1354
|
+
const locale = getLocale(context, options);
|
|
1355
|
+
const escapeParameter = isBoolean(options.escapeParameter) ? options.escapeParameter : context.escapeParameter;
|
|
1356
|
+
escapeParameter && escapeParams(options);
|
|
1357
|
+
const resolvedMessage = !!options.resolvedMessage;
|
|
1358
|
+
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) : [
|
|
1359
|
+
key,
|
|
1360
|
+
locale,
|
|
1361
|
+
context.messages[locale] || create()
|
|
1362
|
+
];
|
|
1363
|
+
let cacheBaseKey = key;
|
|
1364
|
+
if (!resolvedMessage && !isMessageFormat(format)) {
|
|
1365
|
+
const defaultMsgOrKey = isString(options.default) ? options.default : isBoolean(options.default) || context.fallbackFormat ? !context.messageCompiler ? () => key : key : null;
|
|
1366
|
+
if (context.fallbackFormat || defaultMsgOrKey != null && (isString(defaultMsgOrKey) || isFunction(defaultMsgOrKey))) {
|
|
1367
|
+
format = defaultMsgOrKey;
|
|
1368
|
+
cacheBaseKey = format;
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
if (!resolvedMessage && (!isMessageFormat(format) || !isString(targetLocale))) return context.unresolving ? -1 : key;
|
|
1372
|
+
if (isString(format) && context.messageCompiler == null) {
|
|
1373
|
+
warn(`The message format compilation is not supported in this build. Because message compiler isn't included. You need to pre-compilation all message format. So translate function return '${stringifyKey(key)}'.`);
|
|
1374
|
+
return key;
|
|
1375
|
+
}
|
|
1376
|
+
let occurred = false;
|
|
1377
|
+
const onError = () => {
|
|
1378
|
+
occurred = true;
|
|
1379
|
+
};
|
|
1380
|
+
const msg = !isMessageFunction(format) ? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) : format;
|
|
1381
|
+
if (occurred) return format;
|
|
1382
|
+
const msgContext = createMessageContext(getMessageContextOptions(context, targetLocale, message, options));
|
|
1383
|
+
const messaged = inBrowser ? measureEvaluateMessage(context, msg, msgContext) : msg(msgContext);
|
|
1384
|
+
let ret = context.postTranslation ? context.postTranslation(messaged, key) : messaged;
|
|
1385
|
+
if (escapeParameter && isString(ret)) ret = sanitizeTranslatedHtml(ret);
|
|
1386
|
+
return ret;
|
|
1769
1387
|
}
|
|
1770
1388
|
function escapeParams(options) {
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1389
|
+
if (isArray(options.list)) options.list = options.list.map((item) => isString(item) ? escapeHtml(item) : item);
|
|
1390
|
+
if (isObject(options.named)) {
|
|
1391
|
+
const source = options.named;
|
|
1392
|
+
const named = create();
|
|
1393
|
+
for (const key of Object.keys(source)) {
|
|
1394
|
+
const value = source[key];
|
|
1395
|
+
named[key] = isString(value) ? escapeHtml(value) : value;
|
|
1396
|
+
}
|
|
1397
|
+
options.named = named;
|
|
1398
|
+
}
|
|
1781
1399
|
}
|
|
1782
1400
|
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
}
|
|
1848
|
-
if (isString(format) || isMessageAST(format) || isMessageFunction(format)) {
|
|
1849
|
-
break;
|
|
1850
|
-
}
|
|
1851
|
-
if (!isImplicitFallback(targetLocale, locales)) {
|
|
1852
|
-
const missingRet = handleMissing(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1853
|
-
key, targetLocale, missingWarn, type);
|
|
1854
|
-
if (missingRet !== key) {
|
|
1855
|
-
format = missingRet;
|
|
1856
|
-
}
|
|
1857
|
-
}
|
|
1858
|
-
from = to;
|
|
1859
|
-
}
|
|
1860
|
-
return [format, targetLocale, message];
|
|
1401
|
+
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
1402
|
+
const locales = localeFallbacker(context, fallbackLocale, locale);
|
|
1403
|
+
let message = create();
|
|
1404
|
+
let targetLocale;
|
|
1405
|
+
let format = null;
|
|
1406
|
+
let from = locale;
|
|
1407
|
+
const type = "translate";
|
|
1408
|
+
for (let i = 0; i < locales.length; i++) {
|
|
1409
|
+
targetLocale = locales[i];
|
|
1410
|
+
if (locale !== targetLocale && !isAlmostSameLocale(locale, targetLocale) && isTranslateFallbackWarn(fallbackWarn, key)) onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_TRANSLATE, {
|
|
1411
|
+
key,
|
|
1412
|
+
target: targetLocale
|
|
1413
|
+
}));
|
|
1414
|
+
if (locale !== targetLocale) {
|
|
1415
|
+
const emitter = context.__v_emitter;
|
|
1416
|
+
if (emitter) emitter.emit("fallback", {
|
|
1417
|
+
type,
|
|
1418
|
+
key,
|
|
1419
|
+
from,
|
|
1420
|
+
to: targetLocale,
|
|
1421
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1422
|
+
});
|
|
1423
|
+
}
|
|
1424
|
+
message = messages[targetLocale] || create();
|
|
1425
|
+
let start = null;
|
|
1426
|
+
let startTag;
|
|
1427
|
+
let endTag;
|
|
1428
|
+
if (inBrowser) {
|
|
1429
|
+
start = window.performance.now();
|
|
1430
|
+
startTag = "intlify-message-resolve-start";
|
|
1431
|
+
endTag = "intlify-message-resolve-end";
|
|
1432
|
+
mark?.(startTag);
|
|
1433
|
+
}
|
|
1434
|
+
if ((format = resolveValue(message, key)) === null) format = message[key];
|
|
1435
|
+
if (inBrowser) {
|
|
1436
|
+
const end = window.performance.now();
|
|
1437
|
+
const emitter = context.__v_emitter;
|
|
1438
|
+
if (emitter && start && format) emitter.emit("message-resolve", {
|
|
1439
|
+
type: "message-resolve",
|
|
1440
|
+
key,
|
|
1441
|
+
message: format,
|
|
1442
|
+
time: end - start,
|
|
1443
|
+
groupId: toDevtoolsGroupId(type, key)
|
|
1444
|
+
});
|
|
1445
|
+
if (startTag && endTag && mark && measure) {
|
|
1446
|
+
mark(endTag);
|
|
1447
|
+
measure("intlify message resolve", startTag, endTag);
|
|
1448
|
+
}
|
|
1449
|
+
}
|
|
1450
|
+
if (isMessageFormat(format)) break;
|
|
1451
|
+
if (!isImplicitFallback(targetLocale, locales)) {
|
|
1452
|
+
const missingRet = handleMissing(context, key, targetLocale, missingWarn, type);
|
|
1453
|
+
if (missingRet !== key) format = missingRet;
|
|
1454
|
+
}
|
|
1455
|
+
from = targetLocale;
|
|
1456
|
+
}
|
|
1457
|
+
return [
|
|
1458
|
+
format,
|
|
1459
|
+
targetLocale,
|
|
1460
|
+
message
|
|
1461
|
+
];
|
|
1462
|
+
}
|
|
1463
|
+
function stringifyKey(key) {
|
|
1464
|
+
return isString(key) ? key : isObject(key) ? JSON.stringify(key) : isFunction(key) ? key.name || "function" : "";
|
|
1861
1465
|
}
|
|
1862
1466
|
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
const end = window.performance.now();
|
|
1924
|
-
const emitter = context.__v_emitter;
|
|
1925
|
-
if (emitter && start) {
|
|
1926
|
-
emitter.emit('message-evaluation', {
|
|
1927
|
-
type: 'message-evaluation',
|
|
1928
|
-
value: messaged,
|
|
1929
|
-
time: end - start,
|
|
1930
|
-
groupId: `${'translate'}:${msg.key}`
|
|
1931
|
-
});
|
|
1932
|
-
}
|
|
1933
|
-
if (startTag && endTag && mark && measure) {
|
|
1934
|
-
mark(endTag);
|
|
1935
|
-
measure('intlify message evaluation', startTag, endTag);
|
|
1936
|
-
}
|
|
1937
|
-
}
|
|
1938
|
-
return messaged;
|
|
1467
|
+
if (isMessageFunction(format)) {
|
|
1468
|
+
const msg = format;
|
|
1469
|
+
msg.locale = msg.locale || targetLocale;
|
|
1470
|
+
msg.key = msg.key || key;
|
|
1471
|
+
return msg;
|
|
1472
|
+
}
|
|
1473
|
+
if (context.messageCompiler == null) {
|
|
1474
|
+
const msg = (() => format);
|
|
1475
|
+
msg.locale = targetLocale;
|
|
1476
|
+
msg.key = key;
|
|
1477
|
+
return msg;
|
|
1478
|
+
}
|
|
1479
|
+
let start = null;
|
|
1480
|
+
let startTag;
|
|
1481
|
+
let endTag;
|
|
1482
|
+
if (inBrowser && mark) {
|
|
1483
|
+
start = window.performance.now();
|
|
1484
|
+
startTag = "intlify-message-compilation-start";
|
|
1485
|
+
endTag = "intlify-message-compilation-end";
|
|
1486
|
+
mark?.(startTag);
|
|
1487
|
+
}
|
|
1488
|
+
const msg = context.messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, context.warnHtmlMessage, onError));
|
|
1489
|
+
if (inBrowser) {
|
|
1490
|
+
const end = window.performance.now();
|
|
1491
|
+
const emitter = context.__v_emitter;
|
|
1492
|
+
if (emitter && start) emitter.emit("message-compilation", {
|
|
1493
|
+
type: "message-compilation",
|
|
1494
|
+
message: format,
|
|
1495
|
+
time: end - start,
|
|
1496
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
1497
|
+
});
|
|
1498
|
+
if (startTag && endTag && mark && measure) {
|
|
1499
|
+
mark(endTag);
|
|
1500
|
+
measure("intlify message compilation", startTag, endTag);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
msg.locale = targetLocale;
|
|
1504
|
+
msg.key = key;
|
|
1505
|
+
msg.source = format;
|
|
1506
|
+
return msg;
|
|
1507
|
+
}
|
|
1508
|
+
let measureEvaluateMessage;
|
|
1509
|
+
if (inBrowser) {
|
|
1510
|
+
const startTag = "intlify-message-evaluation-start";
|
|
1511
|
+
const endTag = "intlify-message-evaluation-end";
|
|
1512
|
+
measureEvaluateMessage = (context, msg, msgCtx) => {
|
|
1513
|
+
const start = window.performance.now();
|
|
1514
|
+
mark(startTag);
|
|
1515
|
+
const messaged = msg(msgCtx);
|
|
1516
|
+
const end = window.performance.now();
|
|
1517
|
+
context.__v_emitter?.emit("message-evaluation", {
|
|
1518
|
+
type: "message-evaluation",
|
|
1519
|
+
value: messaged,
|
|
1520
|
+
time: end - start,
|
|
1521
|
+
groupId: toDevtoolsGroupId("translate", msg.key)
|
|
1522
|
+
});
|
|
1523
|
+
mark(endTag);
|
|
1524
|
+
measure("intlify message evaluation", startTag, endTag);
|
|
1525
|
+
return messaged;
|
|
1526
|
+
};
|
|
1939
1527
|
}
|
|
1940
1528
|
/** @internal */
|
|
1941
1529
|
function parseTranslateArgs(...args) {
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
|
|
1953
|
-
|
|
1954
|
-
? arg1
|
|
1955
|
-
: arg1;
|
|
1956
|
-
if (isNumber(arg2)) {
|
|
1957
|
-
options.plural = arg2;
|
|
1958
|
-
}
|
|
1959
|
-
else if (isString(arg2)) {
|
|
1960
|
-
options.default = arg2;
|
|
1961
|
-
}
|
|
1962
|
-
else if (isPlainObject(arg2) && !isEmptyObject(arg2)) {
|
|
1963
|
-
options.named = arg2;
|
|
1964
|
-
}
|
|
1965
|
-
else if (isArray(arg2)) {
|
|
1966
|
-
options.list = arg2;
|
|
1967
|
-
}
|
|
1968
|
-
if (isNumber(arg3)) {
|
|
1969
|
-
options.plural = arg3;
|
|
1970
|
-
}
|
|
1971
|
-
else if (isString(arg3)) {
|
|
1972
|
-
options.default = arg3;
|
|
1973
|
-
}
|
|
1974
|
-
else if (isPlainObject(arg3)) {
|
|
1975
|
-
assign(options, arg3);
|
|
1976
|
-
}
|
|
1977
|
-
return [key, options];
|
|
1530
|
+
const [arg1, arg2, arg3] = args;
|
|
1531
|
+
if (!isString(arg1) && !isNumber(arg1) && !isMessageFunction(arg1) && !isMessageAST(arg1)) throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1532
|
+
const key = isNumber(arg1) ? String(arg1) : isMessageFunction(arg1) ? arg1 : arg1;
|
|
1533
|
+
const options = create();
|
|
1534
|
+
if (isNumber(arg2)) options.plural = arg2;
|
|
1535
|
+
else if (isString(arg2)) options.default = arg2;
|
|
1536
|
+
else if (isPlainObject(arg2) && !isKeylessObject(arg2)) options.named = arg2;
|
|
1537
|
+
else if (isArray(arg2)) options.list = arg2;
|
|
1538
|
+
if (isNumber(arg3)) options.plural = arg3;
|
|
1539
|
+
else if (isString(arg3)) options.default = arg3;
|
|
1540
|
+
else if (isPlainObject(arg3)) assign(options, arg3);
|
|
1541
|
+
return [key, options];
|
|
1978
1542
|
}
|
|
1979
1543
|
function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
}
|
|
2004
|
-
},
|
|
2005
|
-
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
2006
|
-
};
|
|
1544
|
+
return {
|
|
1545
|
+
locale,
|
|
1546
|
+
key,
|
|
1547
|
+
warnHtmlMessage,
|
|
1548
|
+
onError: (err) => {
|
|
1549
|
+
onError?.(err);
|
|
1550
|
+
{
|
|
1551
|
+
const _source = getSourceForCodeFrame(source);
|
|
1552
|
+
const codeFrame = err.location && _source && generateCodeFrame(_source, err.location.start.offset, err.location.end.offset);
|
|
1553
|
+
const emitter = context.__v_emitter;
|
|
1554
|
+
if (emitter && _source) emitter.emit("compile-error", {
|
|
1555
|
+
message: _source,
|
|
1556
|
+
error: err.message,
|
|
1557
|
+
start: err.location && err.location.start.offset,
|
|
1558
|
+
end: err.location && err.location.end.offset,
|
|
1559
|
+
groupId: toDevtoolsGroupId("translate", key)
|
|
1560
|
+
});
|
|
1561
|
+
const message = `Message compilation error: ${err.message}`;
|
|
1562
|
+
throw new SyntaxError(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
1563
|
+
}
|
|
1564
|
+
},
|
|
1565
|
+
onCacheKey: (source) => generateFormatCacheKey(locale, key, source)
|
|
1566
|
+
};
|
|
2007
1567
|
}
|
|
2008
1568
|
function getSourceForCodeFrame(source) {
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
}
|
|
2012
|
-
else {
|
|
2013
|
-
if (source.loc && source.loc.source) {
|
|
2014
|
-
return source.loc.source;
|
|
2015
|
-
}
|
|
2016
|
-
}
|
|
1569
|
+
if (isString(source)) return source;
|
|
1570
|
+
else if (source.loc?.source) return source.loc.source;
|
|
2017
1571
|
}
|
|
2018
1572
|
function getMessageContextOptions(context, locale, message, options) {
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
pluralRules,
|
|
2050
|
-
messages: resolveMessage
|
|
2051
|
-
};
|
|
2052
|
-
if (context.processor) {
|
|
2053
|
-
ctxOptions.processor = context.processor;
|
|
2054
|
-
}
|
|
2055
|
-
if (options.list) {
|
|
2056
|
-
ctxOptions.list = options.list;
|
|
2057
|
-
}
|
|
2058
|
-
if (options.named) {
|
|
2059
|
-
ctxOptions.named = options.named;
|
|
2060
|
-
}
|
|
2061
|
-
if (isNumber(options.plural)) {
|
|
2062
|
-
ctxOptions.pluralIndex = options.plural;
|
|
2063
|
-
}
|
|
2064
|
-
return ctxOptions;
|
|
2065
|
-
}
|
|
2066
|
-
|
|
2067
|
-
// register message compiler for jit compilation
|
|
1573
|
+
const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
|
|
1574
|
+
const resolveMessage = (key, useLinked) => {
|
|
1575
|
+
let val = resolveValue(message, key);
|
|
1576
|
+
if (val == null && (fallbackContext || useLinked)) {
|
|
1577
|
+
const [format, , message] = resolveMessageFormat(fallbackContext || context, key, locale, fallbackLocale, fallbackWarn, missingWarn);
|
|
1578
|
+
val = format ?? resolveValue(message, key);
|
|
1579
|
+
}
|
|
1580
|
+
if (isString(val) || isMessageAST(val)) {
|
|
1581
|
+
let occurred = false;
|
|
1582
|
+
const onError = () => {
|
|
1583
|
+
occurred = true;
|
|
1584
|
+
};
|
|
1585
|
+
const msg = compileMessageFormat(context, key, locale, val, key, onError);
|
|
1586
|
+
return !occurred ? msg : NOOP_MESSAGE_FUNCTION;
|
|
1587
|
+
} else if (isMessageFunction(val)) return val;
|
|
1588
|
+
else return NOOP_MESSAGE_FUNCTION;
|
|
1589
|
+
};
|
|
1590
|
+
return {
|
|
1591
|
+
locale,
|
|
1592
|
+
modifiers,
|
|
1593
|
+
pluralRules,
|
|
1594
|
+
messages: resolveMessage,
|
|
1595
|
+
processor: context.processor ?? void 0,
|
|
1596
|
+
list: options.list ?? void 0,
|
|
1597
|
+
named: options.named ?? void 0,
|
|
1598
|
+
pluralIndex: isNumber(options.plural) ? options.plural : void 0
|
|
1599
|
+
};
|
|
1600
|
+
}
|
|
1601
|
+
//#endregion
|
|
1602
|
+
//#region packages/core/src/runtime.ts
|
|
2068
1603
|
registerMessageCompiler(compile);
|
|
2069
|
-
// register message resolver at @intlify/core
|
|
2070
1604
|
registerMessageResolver(resolveValue);
|
|
2071
|
-
// register fallback locale at @intlify/core
|
|
2072
1605
|
registerLocaleFallbacker(fallbackWithLocaleChain);
|
|
2073
|
-
|
|
2074
|
-
export { AST_NODE_PROPS_KEYS, CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_RESOLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple,
|
|
1606
|
+
//#endregion
|
|
1607
|
+
export { AST_NODE_PROPS_KEYS, CORE_ERROR_CODES_EXTEND_POINT, CORE_WARN_CODES_EXTEND_POINT, CoreWarnCodes, DATETIME_FORMAT_OPTIONS_KEYS, DEFAULT_LOCALE, DEFAULT_MESSAGE_DATA_TYPE, MISSING_RESOLVE_VALUE, NOT_RESOLVED, NUMBER_FORMAT_OPTIONS_KEYS, VERSION, clearCompileCache, clearDateTimeFormat, clearNumberFormat, compile, createCompileError, createCoreContext, createCoreError, createMessageContext, datetime, fallbackWithLocaleChain, fallbackWithSimple, getLocale, getLocaleMessage, getWarnMessage, handleMissing, isAlmostSameLocale, isImplicitFallback, isMessageAST, isMessageFormat, isMessageFunction, isTranslateFallbackWarn, isTranslateMissingWarn, number, parse, parseDateTimeArgs, parseNumberArgs, parseTranslateArgs, registerLocaleFallbacker, registerMessageCompiler, registerMessageResolver, resolveLocale, resolveValue, resolveWithKeyValue, setLocaleMessage, translate, updateFallbackLocale };
|