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