@intlify/core-base 10.0.0-beta.5 → 10.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core-base.cjs +1253 -1252
- package/dist/core-base.d.ts +20 -5
- package/dist/core-base.esm-browser.js +1253 -1252
- package/dist/core-base.esm-browser.prod.js +2 -2
- package/dist/core-base.global.js +1253 -1252
- package/dist/core-base.global.prod.js +2 -2
- package/dist/core-base.mjs +1252 -1251
- package/dist/core-base.prod.cjs +1017 -1016
- package/package.json +4 -4
package/dist/core-base.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* core-base v10.0.0-
|
|
2
|
+
* core-base v10.0.0-rc.1
|
|
3
3
|
* (c) 2024 kazuya kawaguchi
|
|
4
4
|
* Released under the MIT License.
|
|
5
5
|
*/
|
|
@@ -8,402 +8,146 @@
|
|
|
8
8
|
var messageCompiler = require('@intlify/message-compiler');
|
|
9
9
|
var shared = require('@intlify/shared');
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
15
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
16
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
17
|
-
};
|
|
18
|
-
pathStateMachine[1 /* States.IN_PATH */] = {
|
|
19
|
-
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */],
|
|
20
|
-
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */],
|
|
21
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
22
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
23
|
-
};
|
|
24
|
-
pathStateMachine[2 /* States.BEFORE_IDENT */] = {
|
|
25
|
-
["w" /* PathCharTypes.WORKSPACE */]: [2 /* States.BEFORE_IDENT */],
|
|
26
|
-
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
27
|
-
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */]
|
|
28
|
-
};
|
|
29
|
-
pathStateMachine[3 /* States.IN_IDENT */] = {
|
|
30
|
-
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
31
|
-
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
32
|
-
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */, 1 /* Actions.PUSH */],
|
|
33
|
-
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */, 1 /* Actions.PUSH */],
|
|
34
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */, 1 /* Actions.PUSH */],
|
|
35
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */, 1 /* Actions.PUSH */]
|
|
36
|
-
};
|
|
37
|
-
pathStateMachine[4 /* States.IN_SUB_PATH */] = {
|
|
38
|
-
["'" /* PathCharTypes.SINGLE_QUOTE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
39
|
-
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
40
|
-
["[" /* PathCharTypes.LEFT_BRACKET */]: [
|
|
41
|
-
4 /* States.IN_SUB_PATH */,
|
|
42
|
-
2 /* Actions.INC_SUB_PATH_DEPTH */
|
|
43
|
-
],
|
|
44
|
-
["]" /* PathCharTypes.RIGHT_BRACKET */]: [1 /* States.IN_PATH */, 3 /* Actions.PUSH_SUB_PATH */],
|
|
45
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
46
|
-
["l" /* PathCharTypes.ELSE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */]
|
|
47
|
-
};
|
|
48
|
-
pathStateMachine[5 /* States.IN_SINGLE_QUOTE */] = {
|
|
49
|
-
["'" /* PathCharTypes.SINGLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
50
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
51
|
-
["l" /* PathCharTypes.ELSE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
52
|
-
};
|
|
53
|
-
pathStateMachine[6 /* States.IN_DOUBLE_QUOTE */] = {
|
|
54
|
-
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
55
|
-
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
56
|
-
["l" /* PathCharTypes.ELSE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
57
|
-
};
|
|
58
|
-
/**
|
|
59
|
-
* Check if an expression is a literal value.
|
|
60
|
-
*/
|
|
61
|
-
const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
|
|
62
|
-
function isLiteral(exp) {
|
|
63
|
-
return literalValueRE.test(exp);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Strip quotes from a string
|
|
67
|
-
*/
|
|
68
|
-
function stripQuotes(str) {
|
|
69
|
-
const a = str.charCodeAt(0);
|
|
70
|
-
const b = str.charCodeAt(str.length - 1);
|
|
71
|
-
return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str;
|
|
11
|
+
function format(ast) {
|
|
12
|
+
const msg = (ctx) => formatParts(ctx, ast);
|
|
13
|
+
return msg;
|
|
72
14
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
*/
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return
|
|
15
|
+
function formatParts(ctx, ast) {
|
|
16
|
+
const body = ast.b || ast.body;
|
|
17
|
+
if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
|
|
18
|
+
const plural = body;
|
|
19
|
+
const cases = plural.c || plural.cases;
|
|
20
|
+
return ctx.plural(cases.reduce((messages, c) => [
|
|
21
|
+
...messages,
|
|
22
|
+
formatMessageParts(ctx, c)
|
|
23
|
+
], []));
|
|
79
24
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
case 0x5b: // [
|
|
83
|
-
case 0x5d: // ]
|
|
84
|
-
case 0x2e: // .
|
|
85
|
-
case 0x22: // "
|
|
86
|
-
case 0x27: // '
|
|
87
|
-
return ch;
|
|
88
|
-
case 0x5f: // _
|
|
89
|
-
case 0x24: // $
|
|
90
|
-
case 0x2d: // -
|
|
91
|
-
return "i" /* PathCharTypes.IDENT */;
|
|
92
|
-
case 0x09: // Tab (HT)
|
|
93
|
-
case 0x0a: // Newline (LF)
|
|
94
|
-
case 0x0d: // Return (CR)
|
|
95
|
-
case 0xa0: // No-break space (NBSP)
|
|
96
|
-
case 0xfeff: // Byte Order Mark (BOM)
|
|
97
|
-
case 0x2028: // Line Separator (LS)
|
|
98
|
-
case 0x2029: // Paragraph Separator (PS)
|
|
99
|
-
return "w" /* PathCharTypes.WORKSPACE */;
|
|
25
|
+
else {
|
|
26
|
+
return formatMessageParts(ctx, body);
|
|
100
27
|
}
|
|
101
|
-
return "i" /* PathCharTypes.IDENT */;
|
|
102
28
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return
|
|
29
|
+
function formatMessageParts(ctx, node) {
|
|
30
|
+
const _static = node.s || node.static;
|
|
31
|
+
if (_static) {
|
|
32
|
+
return ctx.type === 'text'
|
|
33
|
+
? _static
|
|
34
|
+
: ctx.normalize([_static]);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
|
|
38
|
+
return ctx.normalize(messages);
|
|
113
39
|
}
|
|
114
|
-
return isLiteral(trimmed)
|
|
115
|
-
? stripQuotes(trimmed)
|
|
116
|
-
: "*" /* PathCharTypes.ASTARISK */ + trimmed;
|
|
117
40
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
let mode = 0 /* States.BEFORE_PATH */;
|
|
125
|
-
let subPathDepth = 0;
|
|
126
|
-
let c;
|
|
127
|
-
let key; // eslint-disable-line
|
|
128
|
-
let newChar;
|
|
129
|
-
let type;
|
|
130
|
-
let transition;
|
|
131
|
-
let action;
|
|
132
|
-
let typeMap;
|
|
133
|
-
const actions = [];
|
|
134
|
-
actions[0 /* Actions.APPEND */] = () => {
|
|
135
|
-
if (key === undefined) {
|
|
136
|
-
key = newChar;
|
|
137
|
-
}
|
|
138
|
-
else {
|
|
139
|
-
key += newChar;
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
actions[1 /* Actions.PUSH */] = () => {
|
|
143
|
-
if (key !== undefined) {
|
|
144
|
-
keys.push(key);
|
|
145
|
-
key = undefined;
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
actions[2 /* Actions.INC_SUB_PATH_DEPTH */] = () => {
|
|
149
|
-
actions[0 /* Actions.APPEND */]();
|
|
150
|
-
subPathDepth++;
|
|
151
|
-
};
|
|
152
|
-
actions[3 /* Actions.PUSH_SUB_PATH */] = () => {
|
|
153
|
-
if (subPathDepth > 0) {
|
|
154
|
-
subPathDepth--;
|
|
155
|
-
mode = 4 /* States.IN_SUB_PATH */;
|
|
156
|
-
actions[0 /* Actions.APPEND */]();
|
|
41
|
+
function formatMessagePart(ctx, node) {
|
|
42
|
+
const type = node.t || node.type;
|
|
43
|
+
switch (type) {
|
|
44
|
+
case 3 /* NodeTypes.Text */: {
|
|
45
|
+
const text = node;
|
|
46
|
+
return (text.v || text.value);
|
|
157
47
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
key = formatSubPath(key);
|
|
164
|
-
if (key === false) {
|
|
165
|
-
return false;
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
actions[1 /* Actions.PUSH */]();
|
|
169
|
-
}
|
|
48
|
+
case 9 /* NodeTypes.Literal */: {
|
|
49
|
+
const literal = node;
|
|
50
|
+
return (literal.v || literal.value);
|
|
170
51
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if ((mode === 5 /* States.IN_SINGLE_QUOTE */ &&
|
|
175
|
-
nextChar === "'" /* PathCharTypes.SINGLE_QUOTE */) ||
|
|
176
|
-
(mode === 6 /* States.IN_DOUBLE_QUOTE */ &&
|
|
177
|
-
nextChar === "\"" /* PathCharTypes.DOUBLE_QUOTE */)) {
|
|
178
|
-
index++;
|
|
179
|
-
newChar = '\\' + nextChar;
|
|
180
|
-
actions[0 /* Actions.APPEND */]();
|
|
181
|
-
return true;
|
|
52
|
+
case 4 /* NodeTypes.Named */: {
|
|
53
|
+
const named = node;
|
|
54
|
+
return ctx.interpolate(ctx.named(named.k || named.key));
|
|
182
55
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
c = path[index];
|
|
187
|
-
if (c === '\\' && maybeUnescapeQuote()) {
|
|
188
|
-
continue;
|
|
56
|
+
case 5 /* NodeTypes.List */: {
|
|
57
|
+
const list = node;
|
|
58
|
+
return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
|
|
189
59
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (transition === 8 /* States.ERROR */) {
|
|
195
|
-
return;
|
|
60
|
+
case 6 /* NodeTypes.Linked */: {
|
|
61
|
+
const linked = node;
|
|
62
|
+
const modifier = linked.m || linked.modifier;
|
|
63
|
+
return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
|
|
196
64
|
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
if (action) {
|
|
201
|
-
newChar = c;
|
|
202
|
-
if (action() === false) {
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
65
|
+
case 7 /* NodeTypes.LinkedKey */: {
|
|
66
|
+
const linkedKey = node;
|
|
67
|
+
return (linkedKey.v || linkedKey.value);
|
|
206
68
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
return
|
|
69
|
+
case 8 /* NodeTypes.LinkedModifier */: {
|
|
70
|
+
const linkedModifier = node;
|
|
71
|
+
return (linkedModifier.v || linkedModifier.value);
|
|
210
72
|
}
|
|
73
|
+
default:
|
|
74
|
+
throw new Error(`unhandled node type on format message part: ${type}`);
|
|
211
75
|
}
|
|
212
76
|
}
|
|
213
|
-
// path token cache
|
|
214
|
-
const cache = new Map();
|
|
215
|
-
/**
|
|
216
|
-
* key-value message resolver
|
|
217
|
-
*
|
|
218
|
-
* @remarks
|
|
219
|
-
* Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
|
|
220
|
-
*
|
|
221
|
-
* @param obj - A target object to be resolved with path
|
|
222
|
-
* @param path - A {@link Path | path} to resolve the value of message
|
|
223
|
-
*
|
|
224
|
-
* @returns A resolved {@link PathValue | path value}
|
|
225
|
-
*
|
|
226
|
-
* @VueI18nGeneral
|
|
227
|
-
*/
|
|
228
|
-
function resolveWithKeyValue(obj, path) {
|
|
229
|
-
return shared.isObject(obj) ? obj[path] : null;
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* message resolver
|
|
233
|
-
*
|
|
234
|
-
* @remarks
|
|
235
|
-
* Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
|
|
236
|
-
*
|
|
237
|
-
* @param obj - A target object to be resolved with path
|
|
238
|
-
* @param path - A {@link Path | path} to resolve the value of message
|
|
239
|
-
*
|
|
240
|
-
* @returns A resolved {@link PathValue | path value}
|
|
241
|
-
*
|
|
242
|
-
* @VueI18nGeneral
|
|
243
|
-
*/
|
|
244
|
-
function resolveValue(obj, path) {
|
|
245
|
-
// check object
|
|
246
|
-
if (!shared.isObject(obj)) {
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
// parse path
|
|
250
|
-
let hit = cache.get(path);
|
|
251
|
-
if (!hit) {
|
|
252
|
-
hit = parse(path);
|
|
253
|
-
if (hit) {
|
|
254
|
-
cache.set(path, hit);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
// check hit
|
|
258
|
-
if (!hit) {
|
|
259
|
-
return null;
|
|
260
|
-
}
|
|
261
|
-
// resolve path value
|
|
262
|
-
const len = hit.length;
|
|
263
|
-
let last = obj;
|
|
264
|
-
let i = 0;
|
|
265
|
-
while (i < len) {
|
|
266
|
-
const val = last[hit[i]];
|
|
267
|
-
if (val === undefined) {
|
|
268
|
-
return null;
|
|
269
|
-
}
|
|
270
|
-
if (shared.isFunction(last)) {
|
|
271
|
-
return null;
|
|
272
|
-
}
|
|
273
|
-
last = val;
|
|
274
|
-
i++;
|
|
275
|
-
}
|
|
276
|
-
return last;
|
|
277
|
-
}
|
|
278
77
|
|
|
279
|
-
const
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
const DEFAULT_INTERPOLATE = shared.toDisplayString;
|
|
284
|
-
function pluralDefault(choice, choicesLength) {
|
|
285
|
-
choice = Math.abs(choice);
|
|
286
|
-
if (choicesLength === 2) {
|
|
287
|
-
// prettier-ignore
|
|
288
|
-
return choice
|
|
289
|
-
? choice > 1
|
|
290
|
-
? 1
|
|
291
|
-
: 0
|
|
292
|
-
: 1;
|
|
78
|
+
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
79
|
+
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
80
|
+
if (warnHtmlMessage && messageCompiler.detectHtmlTag(source)) {
|
|
81
|
+
shared.warn(shared.format(WARN_MESSAGE, { source }));
|
|
293
82
|
}
|
|
294
|
-
return choice ? Math.min(choice, 2) : 0;
|
|
295
|
-
}
|
|
296
|
-
function getPluralIndex(options) {
|
|
297
|
-
// prettier-ignore
|
|
298
|
-
const index = shared.isNumber(options.pluralIndex)
|
|
299
|
-
? options.pluralIndex
|
|
300
|
-
: -1;
|
|
301
|
-
// prettier-ignore
|
|
302
|
-
return options.named && (shared.isNumber(options.named.count) || shared.isNumber(options.named.n))
|
|
303
|
-
? shared.isNumber(options.named.count)
|
|
304
|
-
? options.named.count
|
|
305
|
-
: shared.isNumber(options.named.n)
|
|
306
|
-
? options.named.n
|
|
307
|
-
: index
|
|
308
|
-
: index;
|
|
309
83
|
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if (!props.n) {
|
|
315
|
-
props.n = pluralIndex;
|
|
316
|
-
}
|
|
84
|
+
const defaultOnCacheKey = (message) => message;
|
|
85
|
+
let compileCache = Object.create(null);
|
|
86
|
+
function clearCompileCache() {
|
|
87
|
+
compileCache = Object.create(null);
|
|
317
88
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
shared.isFunction(options.pluralRules[locale])
|
|
329
|
-
? pluralDefault
|
|
330
|
-
: undefined;
|
|
331
|
-
const plural = (messages) => {
|
|
332
|
-
return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
|
89
|
+
const isMessageAST = (val) => shared.isObject(val) &&
|
|
90
|
+
(val.t === 0 || val.type === 0) &&
|
|
91
|
+
('b' in val || 'body' in val);
|
|
92
|
+
function baseCompile(message, options = {}) {
|
|
93
|
+
// error detecting on compile
|
|
94
|
+
let detectError = false;
|
|
95
|
+
const onError = options.onError || messageCompiler.defaultOnError;
|
|
96
|
+
options.onError = (err) => {
|
|
97
|
+
detectError = true;
|
|
98
|
+
onError(err);
|
|
333
99
|
};
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
100
|
+
// compile with mesasge-compiler
|
|
101
|
+
return { ...messageCompiler.baseCompile(message, options), detectError };
|
|
102
|
+
}
|
|
103
|
+
/* #__NO_SIDE_EFFECTS__ */
|
|
104
|
+
function compile(message, context) {
|
|
105
|
+
if (shared.isString(message)) {
|
|
106
|
+
// check HTML message
|
|
107
|
+
const warnHtmlMessage = shared.isBoolean(context.warnHtmlMessage)
|
|
108
|
+
? context.warnHtmlMessage
|
|
109
|
+
: true;
|
|
110
|
+
checkHtmlMessage(message, warnHtmlMessage);
|
|
111
|
+
// check caches
|
|
112
|
+
const onCacheKey = context.onCacheKey || defaultOnCacheKey;
|
|
113
|
+
const cacheKey = onCacheKey(message);
|
|
114
|
+
const cached = compileCache[cacheKey];
|
|
115
|
+
if (cached) {
|
|
116
|
+
return cached;
|
|
117
|
+
}
|
|
118
|
+
// compile with JIT mode
|
|
119
|
+
const { ast, detectError } = baseCompile(message, {
|
|
120
|
+
...context,
|
|
121
|
+
location: true,
|
|
122
|
+
jit: true
|
|
123
|
+
});
|
|
124
|
+
// compose message function from AST
|
|
125
|
+
const msg = format(ast);
|
|
126
|
+
// if occurred compile error, don't cache
|
|
127
|
+
return !detectError
|
|
128
|
+
? (compileCache[cacheKey] = msg)
|
|
351
129
|
: msg;
|
|
352
130
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
? options.processor.normalize
|
|
358
|
-
: DEFAULT_NORMALIZE;
|
|
359
|
-
const interpolate = shared.isPlainObject(options.processor) &&
|
|
360
|
-
shared.isFunction(options.processor.interpolate)
|
|
361
|
-
? options.processor.interpolate
|
|
362
|
-
: DEFAULT_INTERPOLATE;
|
|
363
|
-
const type = shared.isPlainObject(options.processor) && shared.isString(options.processor.type)
|
|
364
|
-
? options.processor.type
|
|
365
|
-
: DEFAULT_MESSAGE_DATA_TYPE;
|
|
366
|
-
const linked = (key, ...args) => {
|
|
367
|
-
const [arg1, arg2] = args;
|
|
368
|
-
let type = 'text';
|
|
369
|
-
let modifier = '';
|
|
370
|
-
if (args.length === 1) {
|
|
371
|
-
if (shared.isObject(arg1)) {
|
|
372
|
-
modifier = arg1.modifier || modifier;
|
|
373
|
-
type = arg1.type || type;
|
|
374
|
-
}
|
|
375
|
-
else if (shared.isString(arg1)) {
|
|
376
|
-
modifier = arg1 || modifier;
|
|
377
|
-
}
|
|
131
|
+
else {
|
|
132
|
+
if (!isMessageAST(message)) {
|
|
133
|
+
shared.warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
134
|
+
return (() => message);
|
|
378
135
|
}
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
if (
|
|
384
|
-
|
|
136
|
+
// AST case (passed from bundler)
|
|
137
|
+
const cacheKey = message.cacheKey;
|
|
138
|
+
if (cacheKey) {
|
|
139
|
+
const cached = compileCache[cacheKey];
|
|
140
|
+
if (cached) {
|
|
141
|
+
return cached;
|
|
385
142
|
}
|
|
143
|
+
// compose message function from message (AST)
|
|
144
|
+
return (compileCache[cacheKey] =
|
|
145
|
+
format(message));
|
|
386
146
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
? ret[0]
|
|
392
|
-
: ret;
|
|
393
|
-
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
394
|
-
};
|
|
395
|
-
const ctx = {
|
|
396
|
-
["list" /* HelperNameMap.LIST */]: list,
|
|
397
|
-
["named" /* HelperNameMap.NAMED */]: named,
|
|
398
|
-
["plural" /* HelperNameMap.PLURAL */]: plural,
|
|
399
|
-
["linked" /* HelperNameMap.LINKED */]: linked,
|
|
400
|
-
["message" /* HelperNameMap.MESSAGE */]: message,
|
|
401
|
-
["type" /* HelperNameMap.TYPE */]: type,
|
|
402
|
-
["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
|
|
403
|
-
["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
|
|
404
|
-
["values" /* HelperNameMap.VALUES */]: shared.assign({}, _list, _named)
|
|
405
|
-
};
|
|
406
|
-
return ctx;
|
|
147
|
+
else {
|
|
148
|
+
return format(message);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
407
151
|
}
|
|
408
152
|
|
|
409
153
|
let devtools = null;
|
|
@@ -429,30 +173,6 @@ function createDevToolsHook(hook) {
|
|
|
429
173
|
return (payloads) => devtools && devtools.emit(hook, payloads);
|
|
430
174
|
}
|
|
431
175
|
|
|
432
|
-
const CoreWarnCodes = {
|
|
433
|
-
NOT_FOUND_KEY: 1,
|
|
434
|
-
FALLBACK_TO_TRANSLATE: 2,
|
|
435
|
-
CANNOT_FORMAT_NUMBER: 3,
|
|
436
|
-
FALLBACK_TO_NUMBER_FORMAT: 4,
|
|
437
|
-
CANNOT_FORMAT_DATE: 5,
|
|
438
|
-
FALLBACK_TO_DATE_FORMAT: 6,
|
|
439
|
-
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7
|
|
440
|
-
};
|
|
441
|
-
const CORE_WARN_CODES_EXTEND_POINT = 8;
|
|
442
|
-
/** @internal */
|
|
443
|
-
const warnMessages = {
|
|
444
|
-
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
|
|
445
|
-
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
446
|
-
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
447
|
-
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
448
|
-
[CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
|
|
449
|
-
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
450
|
-
[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.`
|
|
451
|
-
};
|
|
452
|
-
function getWarnMessage(code, ...args) {
|
|
453
|
-
return shared.format(warnMessages[code], ...args);
|
|
454
|
-
}
|
|
455
|
-
|
|
456
176
|
const CoreErrorCodes = {
|
|
457
177
|
INVALID_ARGUMENT: messageCompiler.COMPILE_ERROR_CODES_EXTEND_POINT, // 17
|
|
458
178
|
INVALID_DATE_ARGUMENT: 18,
|
|
@@ -626,66 +346,358 @@ function appendItemToChain(chain, target, blocks) {
|
|
|
626
346
|
return follow;
|
|
627
347
|
}
|
|
628
348
|
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
*/
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
349
|
+
const pathStateMachine = [];
|
|
350
|
+
pathStateMachine[0 /* States.BEFORE_PATH */] = {
|
|
351
|
+
["w" /* PathCharTypes.WORKSPACE */]: [0 /* States.BEFORE_PATH */],
|
|
352
|
+
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
353
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
354
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
355
|
+
};
|
|
356
|
+
pathStateMachine[1 /* States.IN_PATH */] = {
|
|
357
|
+
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */],
|
|
358
|
+
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */],
|
|
359
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */],
|
|
360
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */]
|
|
361
|
+
};
|
|
362
|
+
pathStateMachine[2 /* States.BEFORE_IDENT */] = {
|
|
363
|
+
["w" /* PathCharTypes.WORKSPACE */]: [2 /* States.BEFORE_IDENT */],
|
|
364
|
+
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
365
|
+
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */]
|
|
366
|
+
};
|
|
367
|
+
pathStateMachine[3 /* States.IN_IDENT */] = {
|
|
368
|
+
["i" /* PathCharTypes.IDENT */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
369
|
+
["0" /* PathCharTypes.ZERO */]: [3 /* States.IN_IDENT */, 0 /* Actions.APPEND */],
|
|
370
|
+
["w" /* PathCharTypes.WORKSPACE */]: [1 /* States.IN_PATH */, 1 /* Actions.PUSH */],
|
|
371
|
+
["." /* PathCharTypes.DOT */]: [2 /* States.BEFORE_IDENT */, 1 /* Actions.PUSH */],
|
|
372
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [4 /* States.IN_SUB_PATH */, 1 /* Actions.PUSH */],
|
|
373
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: [7 /* States.AFTER_PATH */, 1 /* Actions.PUSH */]
|
|
374
|
+
};
|
|
375
|
+
pathStateMachine[4 /* States.IN_SUB_PATH */] = {
|
|
376
|
+
["'" /* PathCharTypes.SINGLE_QUOTE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
377
|
+
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */],
|
|
378
|
+
["[" /* PathCharTypes.LEFT_BRACKET */]: [
|
|
379
|
+
4 /* States.IN_SUB_PATH */,
|
|
380
|
+
2 /* Actions.INC_SUB_PATH_DEPTH */
|
|
381
|
+
],
|
|
382
|
+
["]" /* PathCharTypes.RIGHT_BRACKET */]: [1 /* States.IN_PATH */, 3 /* Actions.PUSH_SUB_PATH */],
|
|
383
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
384
|
+
["l" /* PathCharTypes.ELSE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */]
|
|
385
|
+
};
|
|
386
|
+
pathStateMachine[5 /* States.IN_SINGLE_QUOTE */] = {
|
|
387
|
+
["'" /* PathCharTypes.SINGLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
388
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
389
|
+
["l" /* PathCharTypes.ELSE */]: [5 /* States.IN_SINGLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
390
|
+
};
|
|
391
|
+
pathStateMachine[6 /* States.IN_DOUBLE_QUOTE */] = {
|
|
392
|
+
["\"" /* PathCharTypes.DOUBLE_QUOTE */]: [4 /* States.IN_SUB_PATH */, 0 /* Actions.APPEND */],
|
|
393
|
+
["o" /* PathCharTypes.END_OF_FAIL */]: 8 /* States.ERROR */,
|
|
394
|
+
["l" /* PathCharTypes.ELSE */]: [6 /* States.IN_DOUBLE_QUOTE */, 0 /* Actions.APPEND */]
|
|
395
|
+
};
|
|
396
|
+
/**
|
|
397
|
+
* Check if an expression is a literal value.
|
|
398
|
+
*/
|
|
399
|
+
const literalValueRE = /^\s?(?:true|false|-?[\d.]+|'[^']*'|"[^"]*")\s?$/;
|
|
400
|
+
function isLiteral(exp) {
|
|
401
|
+
return literalValueRE.test(exp);
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Strip quotes from a string
|
|
405
|
+
*/
|
|
406
|
+
function stripQuotes(str) {
|
|
407
|
+
const a = str.charCodeAt(0);
|
|
408
|
+
const b = str.charCodeAt(str.length - 1);
|
|
409
|
+
return a === b && (a === 0x22 || a === 0x27) ? str.slice(1, -1) : str;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Determine the type of a character in a keypath.
|
|
413
|
+
*/
|
|
414
|
+
function getPathCharType(ch) {
|
|
415
|
+
if (ch === undefined || ch === null) {
|
|
416
|
+
return "o" /* PathCharTypes.END_OF_FAIL */;
|
|
417
|
+
}
|
|
418
|
+
const code = ch.charCodeAt(0);
|
|
419
|
+
switch (code) {
|
|
420
|
+
case 0x5b: // [
|
|
421
|
+
case 0x5d: // ]
|
|
422
|
+
case 0x2e: // .
|
|
423
|
+
case 0x22: // "
|
|
424
|
+
case 0x27: // '
|
|
425
|
+
return ch;
|
|
426
|
+
case 0x5f: // _
|
|
427
|
+
case 0x24: // $
|
|
428
|
+
case 0x2d: // -
|
|
429
|
+
return "i" /* PathCharTypes.IDENT */;
|
|
430
|
+
case 0x09: // Tab (HT)
|
|
431
|
+
case 0x0a: // Newline (LF)
|
|
432
|
+
case 0x0d: // Return (CR)
|
|
433
|
+
case 0xa0: // No-break space (NBSP)
|
|
434
|
+
case 0xfeff: // Byte Order Mark (BOM)
|
|
435
|
+
case 0x2028: // Line Separator (LS)
|
|
436
|
+
case 0x2029: // Paragraph Separator (PS)
|
|
437
|
+
return "w" /* PathCharTypes.WORKSPACE */;
|
|
438
|
+
}
|
|
439
|
+
return "i" /* PathCharTypes.IDENT */;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Format a subPath, return its plain form if it is
|
|
443
|
+
* a literal string or number. Otherwise prepend the
|
|
444
|
+
* dynamic indicator (*).
|
|
445
|
+
*/
|
|
446
|
+
function formatSubPath(path) {
|
|
447
|
+
const trimmed = path.trim();
|
|
448
|
+
// invalid leading 0
|
|
449
|
+
if (path.charAt(0) === '0' && isNaN(parseInt(path))) {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
return isLiteral(trimmed)
|
|
453
|
+
? stripQuotes(trimmed)
|
|
454
|
+
: "*" /* PathCharTypes.ASTARISK */ + trimmed;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Parse a string path into an array of segments
|
|
458
|
+
*/
|
|
459
|
+
function parse(path) {
|
|
460
|
+
const keys = [];
|
|
461
|
+
let index = -1;
|
|
462
|
+
let mode = 0 /* States.BEFORE_PATH */;
|
|
463
|
+
let subPathDepth = 0;
|
|
464
|
+
let c;
|
|
465
|
+
let key; // eslint-disable-line
|
|
466
|
+
let newChar;
|
|
467
|
+
let type;
|
|
468
|
+
let transition;
|
|
469
|
+
let action;
|
|
470
|
+
let typeMap;
|
|
471
|
+
const actions = [];
|
|
472
|
+
actions[0 /* Actions.APPEND */] = () => {
|
|
473
|
+
if (key === undefined) {
|
|
474
|
+
key = newChar;
|
|
475
|
+
}
|
|
476
|
+
else {
|
|
477
|
+
key += newChar;
|
|
664
478
|
}
|
|
665
479
|
};
|
|
480
|
+
actions[1 /* Actions.PUSH */] = () => {
|
|
481
|
+
if (key !== undefined) {
|
|
482
|
+
keys.push(key);
|
|
483
|
+
key = undefined;
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
actions[2 /* Actions.INC_SUB_PATH_DEPTH */] = () => {
|
|
487
|
+
actions[0 /* Actions.APPEND */]();
|
|
488
|
+
subPathDepth++;
|
|
489
|
+
};
|
|
490
|
+
actions[3 /* Actions.PUSH_SUB_PATH */] = () => {
|
|
491
|
+
if (subPathDepth > 0) {
|
|
492
|
+
subPathDepth--;
|
|
493
|
+
mode = 4 /* States.IN_SUB_PATH */;
|
|
494
|
+
actions[0 /* Actions.APPEND */]();
|
|
495
|
+
}
|
|
496
|
+
else {
|
|
497
|
+
subPathDepth = 0;
|
|
498
|
+
if (key === undefined) {
|
|
499
|
+
return false;
|
|
500
|
+
}
|
|
501
|
+
key = formatSubPath(key);
|
|
502
|
+
if (key === false) {
|
|
503
|
+
return false;
|
|
504
|
+
}
|
|
505
|
+
else {
|
|
506
|
+
actions[1 /* Actions.PUSH */]();
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
function maybeUnescapeQuote() {
|
|
511
|
+
const nextChar = path[index + 1];
|
|
512
|
+
if ((mode === 5 /* States.IN_SINGLE_QUOTE */ &&
|
|
513
|
+
nextChar === "'" /* PathCharTypes.SINGLE_QUOTE */) ||
|
|
514
|
+
(mode === 6 /* States.IN_DOUBLE_QUOTE */ &&
|
|
515
|
+
nextChar === "\"" /* PathCharTypes.DOUBLE_QUOTE */)) {
|
|
516
|
+
index++;
|
|
517
|
+
newChar = '\\' + nextChar;
|
|
518
|
+
actions[0 /* Actions.APPEND */]();
|
|
519
|
+
return true;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
while (mode !== null) {
|
|
523
|
+
index++;
|
|
524
|
+
c = path[index];
|
|
525
|
+
if (c === '\\' && maybeUnescapeQuote()) {
|
|
526
|
+
continue;
|
|
527
|
+
}
|
|
528
|
+
type = getPathCharType(c);
|
|
529
|
+
typeMap = pathStateMachine[mode];
|
|
530
|
+
transition = typeMap[type] || typeMap["l" /* PathCharTypes.ELSE */] || 8 /* States.ERROR */;
|
|
531
|
+
// check parse error
|
|
532
|
+
if (transition === 8 /* States.ERROR */) {
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
mode = transition[0];
|
|
536
|
+
if (transition[1] !== undefined) {
|
|
537
|
+
action = actions[transition[1]];
|
|
538
|
+
if (action) {
|
|
539
|
+
newChar = c;
|
|
540
|
+
if (action() === false) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
// check parse finish
|
|
546
|
+
if (mode === 7 /* States.AFTER_PATH */) {
|
|
547
|
+
return keys;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
666
550
|
}
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
_compiler = compiler;
|
|
670
|
-
}
|
|
671
|
-
let _resolver;
|
|
551
|
+
// path token cache
|
|
552
|
+
const cache = new Map();
|
|
672
553
|
/**
|
|
673
|
-
*
|
|
554
|
+
* key-value message resolver
|
|
674
555
|
*
|
|
675
|
-
* @
|
|
556
|
+
* @remarks
|
|
557
|
+
* Resolves messages with the key-value structure. Note that messages with a hierarchical structure such as objects cannot be resolved
|
|
558
|
+
*
|
|
559
|
+
* @param obj - A target object to be resolved with path
|
|
560
|
+
* @param path - A {@link Path | path} to resolve the value of message
|
|
561
|
+
*
|
|
562
|
+
* @returns A resolved {@link PathValue | path value}
|
|
676
563
|
*
|
|
677
564
|
* @VueI18nGeneral
|
|
678
565
|
*/
|
|
679
|
-
function
|
|
680
|
-
|
|
566
|
+
function resolveWithKeyValue(obj, path) {
|
|
567
|
+
return shared.isObject(obj) ? obj[path] : null;
|
|
681
568
|
}
|
|
682
|
-
let _fallbacker;
|
|
683
569
|
/**
|
|
684
|
-
*
|
|
570
|
+
* message resolver
|
|
685
571
|
*
|
|
686
|
-
* @
|
|
572
|
+
* @remarks
|
|
573
|
+
* Resolves messages. messages with a hierarchical structure such as objects can be resolved. This resolver is used in VueI18n as default.
|
|
687
574
|
*
|
|
688
|
-
* @
|
|
575
|
+
* @param obj - A target object to be resolved with path
|
|
576
|
+
* @param path - A {@link Path | path} to resolve the value of message
|
|
577
|
+
*
|
|
578
|
+
* @returns A resolved {@link PathValue | path value}
|
|
579
|
+
*
|
|
580
|
+
* @VueI18nGeneral
|
|
581
|
+
*/
|
|
582
|
+
function resolveValue(obj, path) {
|
|
583
|
+
// check object
|
|
584
|
+
if (!shared.isObject(obj)) {
|
|
585
|
+
return null;
|
|
586
|
+
}
|
|
587
|
+
// parse path
|
|
588
|
+
let hit = cache.get(path);
|
|
589
|
+
if (!hit) {
|
|
590
|
+
hit = parse(path);
|
|
591
|
+
if (hit) {
|
|
592
|
+
cache.set(path, hit);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
// check hit
|
|
596
|
+
if (!hit) {
|
|
597
|
+
return null;
|
|
598
|
+
}
|
|
599
|
+
// resolve path value
|
|
600
|
+
const len = hit.length;
|
|
601
|
+
let last = obj;
|
|
602
|
+
let i = 0;
|
|
603
|
+
while (i < len) {
|
|
604
|
+
const val = last[hit[i]];
|
|
605
|
+
if (val === undefined) {
|
|
606
|
+
return null;
|
|
607
|
+
}
|
|
608
|
+
if (shared.isFunction(last)) {
|
|
609
|
+
return null;
|
|
610
|
+
}
|
|
611
|
+
last = val;
|
|
612
|
+
i++;
|
|
613
|
+
}
|
|
614
|
+
return last;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const CoreWarnCodes = {
|
|
618
|
+
NOT_FOUND_KEY: 1,
|
|
619
|
+
FALLBACK_TO_TRANSLATE: 2,
|
|
620
|
+
CANNOT_FORMAT_NUMBER: 3,
|
|
621
|
+
FALLBACK_TO_NUMBER_FORMAT: 4,
|
|
622
|
+
CANNOT_FORMAT_DATE: 5,
|
|
623
|
+
FALLBACK_TO_DATE_FORMAT: 6,
|
|
624
|
+
EXPERIMENTAL_CUSTOM_MESSAGE_COMPILER: 7
|
|
625
|
+
};
|
|
626
|
+
const CORE_WARN_CODES_EXTEND_POINT = 8;
|
|
627
|
+
/** @internal */
|
|
628
|
+
const warnMessages = {
|
|
629
|
+
[CoreWarnCodes.NOT_FOUND_KEY]: `Not found '{key}' key in '{locale}' locale messages.`,
|
|
630
|
+
[CoreWarnCodes.FALLBACK_TO_TRANSLATE]: `Fall back to translate '{key}' key with '{target}' locale.`,
|
|
631
|
+
[CoreWarnCodes.CANNOT_FORMAT_NUMBER]: `Cannot format a number value due to not supported Intl.NumberFormat.`,
|
|
632
|
+
[CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT]: `Fall back to number format '{key}' key with '{target}' locale.`,
|
|
633
|
+
[CoreWarnCodes.CANNOT_FORMAT_DATE]: `Cannot format a date value due to not supported Intl.DateTimeFormat.`,
|
|
634
|
+
[CoreWarnCodes.FALLBACK_TO_DATE_FORMAT]: `Fall back to datetime format '{key}' key with '{target}' locale.`,
|
|
635
|
+
[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.`
|
|
636
|
+
};
|
|
637
|
+
function getWarnMessage(code, ...args) {
|
|
638
|
+
return shared.format(warnMessages[code], ...args);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
642
|
+
/**
|
|
643
|
+
* Intlify core-base version
|
|
644
|
+
* @internal
|
|
645
|
+
*/
|
|
646
|
+
const VERSION = '10.0.0-rc.1';
|
|
647
|
+
const NOT_REOSLVED = -1;
|
|
648
|
+
const DEFAULT_LOCALE = 'en-US';
|
|
649
|
+
const MISSING_RESOLVE_VALUE = '';
|
|
650
|
+
const capitalize = (str) => `${str.charAt(0).toLocaleUpperCase()}${str.substr(1)}`;
|
|
651
|
+
function getDefaultLinkedModifiers() {
|
|
652
|
+
return {
|
|
653
|
+
upper: (val, type) => {
|
|
654
|
+
// prettier-ignore
|
|
655
|
+
return type === 'text' && shared.isString(val)
|
|
656
|
+
? val.toUpperCase()
|
|
657
|
+
: type === 'vnode' && shared.isObject(val) && '__v_isVNode' in val
|
|
658
|
+
? val.children.toUpperCase()
|
|
659
|
+
: val;
|
|
660
|
+
},
|
|
661
|
+
lower: (val, type) => {
|
|
662
|
+
// prettier-ignore
|
|
663
|
+
return type === 'text' && shared.isString(val)
|
|
664
|
+
? val.toLowerCase()
|
|
665
|
+
: type === 'vnode' && shared.isObject(val) && '__v_isVNode' in val
|
|
666
|
+
? val.children.toLowerCase()
|
|
667
|
+
: val;
|
|
668
|
+
},
|
|
669
|
+
capitalize: (val, type) => {
|
|
670
|
+
// prettier-ignore
|
|
671
|
+
return (type === 'text' && shared.isString(val)
|
|
672
|
+
? capitalize(val)
|
|
673
|
+
: type === 'vnode' && shared.isObject(val) && '__v_isVNode' in val
|
|
674
|
+
? capitalize(val.children)
|
|
675
|
+
: val);
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
let _compiler;
|
|
680
|
+
function registerMessageCompiler(compiler) {
|
|
681
|
+
_compiler = compiler;
|
|
682
|
+
}
|
|
683
|
+
let _resolver;
|
|
684
|
+
/**
|
|
685
|
+
* Register the message resolver
|
|
686
|
+
*
|
|
687
|
+
* @param resolver - A {@link MessageResolver} function
|
|
688
|
+
*
|
|
689
|
+
* @VueI18nGeneral
|
|
690
|
+
*/
|
|
691
|
+
function registerMessageResolver(resolver) {
|
|
692
|
+
_resolver = resolver;
|
|
693
|
+
}
|
|
694
|
+
let _fallbacker;
|
|
695
|
+
/**
|
|
696
|
+
* Register the locale fallbacker
|
|
697
|
+
*
|
|
698
|
+
* @param fallbacker - A {@link LocaleFallbacker} function
|
|
699
|
+
*
|
|
700
|
+
* @VueI18nGeneral
|
|
689
701
|
*/
|
|
690
702
|
function registerLocaleFallbacker(fallbacker) {
|
|
691
703
|
_fallbacker = fallbacker;
|
|
@@ -787,670 +799,288 @@ function createCoreContext(options = {}) {
|
|
|
787
799
|
missing,
|
|
788
800
|
missingWarn,
|
|
789
801
|
fallbackWarn,
|
|
790
|
-
fallbackFormat,
|
|
791
|
-
unresolving,
|
|
792
|
-
postTranslation,
|
|
793
|
-
processor,
|
|
794
|
-
warnHtmlMessage,
|
|
795
|
-
escapeParameter,
|
|
796
|
-
messageCompiler,
|
|
797
|
-
messageResolver,
|
|
798
|
-
localeFallbacker,
|
|
799
|
-
fallbackContext,
|
|
800
|
-
onWarn,
|
|
801
|
-
__meta
|
|
802
|
-
};
|
|
803
|
-
{
|
|
804
|
-
context.datetimeFormats = datetimeFormats;
|
|
805
|
-
context.numberFormats = numberFormats;
|
|
806
|
-
context.__datetimeFormatters = __datetimeFormatters;
|
|
807
|
-
context.__numberFormatters = __numberFormatters;
|
|
808
|
-
}
|
|
809
|
-
// for vue-devtools timeline event
|
|
810
|
-
{
|
|
811
|
-
context.__v_emitter =
|
|
812
|
-
internalOptions.__v_emitter != null
|
|
813
|
-
? internalOptions.__v_emitter
|
|
814
|
-
: undefined;
|
|
815
|
-
}
|
|
816
|
-
// NOTE: experimental !!
|
|
817
|
-
{
|
|
818
|
-
initI18nDevTools(context, version, __meta);
|
|
819
|
-
}
|
|
820
|
-
return context;
|
|
821
|
-
}
|
|
822
|
-
/** @internal */
|
|
823
|
-
function isTranslateFallbackWarn(fallback, key) {
|
|
824
|
-
return fallback instanceof RegExp ? fallback.test(key) : fallback;
|
|
825
|
-
}
|
|
826
|
-
/** @internal */
|
|
827
|
-
function isTranslateMissingWarn(missing, key) {
|
|
828
|
-
return missing instanceof RegExp ? missing.test(key) : missing;
|
|
829
|
-
}
|
|
830
|
-
/** @internal */
|
|
831
|
-
function handleMissing(context, key, locale, missingWarn, type) {
|
|
832
|
-
const { missing, onWarn } = context;
|
|
833
|
-
// for vue-devtools timeline event
|
|
834
|
-
{
|
|
835
|
-
const emitter = context.__v_emitter;
|
|
836
|
-
if (emitter) {
|
|
837
|
-
emitter.emit('missing', {
|
|
838
|
-
locale,
|
|
839
|
-
key,
|
|
840
|
-
type,
|
|
841
|
-
groupId: `${type}:${key}`
|
|
842
|
-
});
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
if (missing !== null) {
|
|
846
|
-
const ret = missing(context, locale, key, type);
|
|
847
|
-
return shared.isString(ret) ? ret : key;
|
|
848
|
-
}
|
|
849
|
-
else {
|
|
850
|
-
if (isTranslateMissingWarn(missingWarn, key)) {
|
|
851
|
-
onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, { key, locale }));
|
|
852
|
-
}
|
|
853
|
-
return key;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
/** @internal */
|
|
857
|
-
function updateFallbackLocale(ctx, locale, fallback) {
|
|
858
|
-
const context = ctx;
|
|
859
|
-
context.__localeChainCache = new Map();
|
|
860
|
-
ctx.localeFallbacker(ctx, fallback, locale);
|
|
861
|
-
}
|
|
862
|
-
/** @internal */
|
|
863
|
-
function isAlmostSameLocale(locale, compareLocale) {
|
|
864
|
-
if (locale === compareLocale)
|
|
865
|
-
return false;
|
|
866
|
-
return locale.split('-')[0] === compareLocale.split('-')[0];
|
|
867
|
-
}
|
|
868
|
-
/** @internal */
|
|
869
|
-
function isImplicitFallback(targetLocale, locales) {
|
|
870
|
-
const index = locales.indexOf(targetLocale);
|
|
871
|
-
if (index === -1) {
|
|
872
|
-
return false;
|
|
873
|
-
}
|
|
874
|
-
for (let i = index + 1; i < locales.length; i++) {
|
|
875
|
-
if (isAlmostSameLocale(targetLocale, locales[i])) {
|
|
876
|
-
return true;
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
return false;
|
|
880
|
-
}
|
|
881
|
-
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
882
|
-
|
|
883
|
-
function format(ast) {
|
|
884
|
-
const msg = (ctx) => formatParts(ctx, ast);
|
|
885
|
-
return msg;
|
|
886
|
-
}
|
|
887
|
-
function formatParts(ctx, ast) {
|
|
888
|
-
const body = ast.b || ast.body;
|
|
889
|
-
if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
|
|
890
|
-
const plural = body;
|
|
891
|
-
const cases = plural.c || plural.cases;
|
|
892
|
-
return ctx.plural(cases.reduce((messages, c) => [
|
|
893
|
-
...messages,
|
|
894
|
-
formatMessageParts(ctx, c)
|
|
895
|
-
], []));
|
|
896
|
-
}
|
|
897
|
-
else {
|
|
898
|
-
return formatMessageParts(ctx, body);
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
function formatMessageParts(ctx, node) {
|
|
902
|
-
const _static = node.s || node.static;
|
|
903
|
-
if (_static) {
|
|
904
|
-
return ctx.type === 'text'
|
|
905
|
-
? _static
|
|
906
|
-
: ctx.normalize([_static]);
|
|
907
|
-
}
|
|
908
|
-
else {
|
|
909
|
-
const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
|
|
910
|
-
return ctx.normalize(messages);
|
|
911
|
-
}
|
|
912
|
-
}
|
|
913
|
-
function formatMessagePart(ctx, node) {
|
|
914
|
-
const type = node.t || node.type;
|
|
915
|
-
switch (type) {
|
|
916
|
-
case 3 /* NodeTypes.Text */: {
|
|
917
|
-
const text = node;
|
|
918
|
-
return (text.v || text.value);
|
|
919
|
-
}
|
|
920
|
-
case 9 /* NodeTypes.Literal */: {
|
|
921
|
-
const literal = node;
|
|
922
|
-
return (literal.v || literal.value);
|
|
923
|
-
}
|
|
924
|
-
case 4 /* NodeTypes.Named */: {
|
|
925
|
-
const named = node;
|
|
926
|
-
return ctx.interpolate(ctx.named(named.k || named.key));
|
|
927
|
-
}
|
|
928
|
-
case 5 /* NodeTypes.List */: {
|
|
929
|
-
const list = node;
|
|
930
|
-
return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
|
|
931
|
-
}
|
|
932
|
-
case 6 /* NodeTypes.Linked */: {
|
|
933
|
-
const linked = node;
|
|
934
|
-
const modifier = linked.m || linked.modifier;
|
|
935
|
-
return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
|
|
936
|
-
}
|
|
937
|
-
case 7 /* NodeTypes.LinkedKey */: {
|
|
938
|
-
const linkedKey = node;
|
|
939
|
-
return (linkedKey.v || linkedKey.value);
|
|
940
|
-
}
|
|
941
|
-
case 8 /* NodeTypes.LinkedModifier */: {
|
|
942
|
-
const linkedModifier = node;
|
|
943
|
-
return (linkedModifier.v || linkedModifier.value);
|
|
944
|
-
}
|
|
945
|
-
default:
|
|
946
|
-
throw new Error(`unhandled node type on format message part: ${type}`);
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
const WARN_MESSAGE = `Detected HTML in '{source}' message. Recommend not using HTML messages to avoid XSS.`;
|
|
951
|
-
function checkHtmlMessage(source, warnHtmlMessage) {
|
|
952
|
-
if (warnHtmlMessage && messageCompiler.detectHtmlTag(source)) {
|
|
953
|
-
shared.warn(shared.format(WARN_MESSAGE, { source }));
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
const defaultOnCacheKey = (message) => message;
|
|
957
|
-
let compileCache = Object.create(null);
|
|
958
|
-
function clearCompileCache() {
|
|
959
|
-
compileCache = Object.create(null);
|
|
960
|
-
}
|
|
961
|
-
const isMessageAST = (val) => shared.isObject(val) &&
|
|
962
|
-
(val.t === 0 || val.type === 0) &&
|
|
963
|
-
('b' in val || 'body' in val);
|
|
964
|
-
function baseCompile(message, options = {}) {
|
|
965
|
-
// error detecting on compile
|
|
966
|
-
let detectError = false;
|
|
967
|
-
const onError = options.onError || messageCompiler.defaultOnError;
|
|
968
|
-
options.onError = (err) => {
|
|
969
|
-
detectError = true;
|
|
970
|
-
onError(err);
|
|
971
|
-
};
|
|
972
|
-
// compile with mesasge-compiler
|
|
973
|
-
return { ...messageCompiler.baseCompile(message, options), detectError };
|
|
974
|
-
}
|
|
975
|
-
/* #__NO_SIDE_EFFECTS__ */
|
|
976
|
-
function compile(message, context) {
|
|
977
|
-
if (shared.isString(message)) {
|
|
978
|
-
// check HTML message
|
|
979
|
-
const warnHtmlMessage = shared.isBoolean(context.warnHtmlMessage)
|
|
980
|
-
? context.warnHtmlMessage
|
|
981
|
-
: true;
|
|
982
|
-
checkHtmlMessage(message, warnHtmlMessage);
|
|
983
|
-
// check caches
|
|
984
|
-
const onCacheKey = context.onCacheKey || defaultOnCacheKey;
|
|
985
|
-
const cacheKey = onCacheKey(message);
|
|
986
|
-
const cached = compileCache[cacheKey];
|
|
987
|
-
if (cached) {
|
|
988
|
-
return cached;
|
|
989
|
-
}
|
|
990
|
-
// compile with JIT mode
|
|
991
|
-
const { ast, detectError } = baseCompile(message, {
|
|
992
|
-
...context,
|
|
993
|
-
location: true,
|
|
994
|
-
jit: true
|
|
995
|
-
});
|
|
996
|
-
// compose message function from AST
|
|
997
|
-
const msg = format(ast);
|
|
998
|
-
// if occurred compile error, don't cache
|
|
999
|
-
return !detectError
|
|
1000
|
-
? (compileCache[cacheKey] = msg)
|
|
1001
|
-
: msg;
|
|
1002
|
-
}
|
|
1003
|
-
else {
|
|
1004
|
-
if (!isMessageAST(message)) {
|
|
1005
|
-
shared.warn(`the message that is resolve with key '${context.key}' is not supported for jit compilation`);
|
|
1006
|
-
return (() => message);
|
|
1007
|
-
}
|
|
1008
|
-
// AST case (passed from bundler)
|
|
1009
|
-
const cacheKey = message.cacheKey;
|
|
1010
|
-
if (cacheKey) {
|
|
1011
|
-
const cached = compileCache[cacheKey];
|
|
1012
|
-
if (cached) {
|
|
1013
|
-
return cached;
|
|
1014
|
-
}
|
|
1015
|
-
// compose message function from message (AST)
|
|
1016
|
-
return (compileCache[cacheKey] =
|
|
1017
|
-
format(message));
|
|
1018
|
-
}
|
|
1019
|
-
else {
|
|
1020
|
-
return format(message);
|
|
1021
|
-
}
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
const NOOP_MESSAGE_FUNCTION = () => '';
|
|
1026
|
-
const isMessageFunction = (val) => shared.isFunction(val);
|
|
1027
|
-
// implementation of `translate` function
|
|
1028
|
-
function translate(context, ...args) {
|
|
1029
|
-
const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
|
|
1030
|
-
const [key, options] = parseTranslateArgs(...args);
|
|
1031
|
-
const missingWarn = shared.isBoolean(options.missingWarn)
|
|
1032
|
-
? options.missingWarn
|
|
1033
|
-
: context.missingWarn;
|
|
1034
|
-
const fallbackWarn = shared.isBoolean(options.fallbackWarn)
|
|
1035
|
-
? options.fallbackWarn
|
|
1036
|
-
: context.fallbackWarn;
|
|
1037
|
-
const escapeParameter = shared.isBoolean(options.escapeParameter)
|
|
1038
|
-
? options.escapeParameter
|
|
1039
|
-
: context.escapeParameter;
|
|
1040
|
-
const resolvedMessage = !!options.resolvedMessage;
|
|
1041
|
-
// prettier-ignore
|
|
1042
|
-
const defaultMsgOrKey = shared.isString(options.default) || shared.isBoolean(options.default) // default by function option
|
|
1043
|
-
? !shared.isBoolean(options.default)
|
|
1044
|
-
? options.default
|
|
1045
|
-
: (!messageCompiler ? () => key : key)
|
|
1046
|
-
: fallbackFormat // default by `fallbackFormat` option
|
|
1047
|
-
? (!messageCompiler ? () => key : key)
|
|
1048
|
-
: null;
|
|
1049
|
-
const enableDefaultMsg = fallbackFormat ||
|
|
1050
|
-
(defaultMsgOrKey != null &&
|
|
1051
|
-
(shared.isString(defaultMsgOrKey) || shared.isFunction(defaultMsgOrKey)));
|
|
1052
|
-
const locale = getLocale(context, options);
|
|
1053
|
-
// escape params
|
|
1054
|
-
escapeParameter && escapeParams(options);
|
|
1055
|
-
// resolve message format
|
|
1056
|
-
// eslint-disable-next-line prefer-const
|
|
1057
|
-
let [formatScope, targetLocale, message] = !resolvedMessage
|
|
1058
|
-
? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
|
|
1059
|
-
: [
|
|
1060
|
-
key,
|
|
1061
|
-
locale,
|
|
1062
|
-
messages[locale] || {}
|
|
1063
|
-
];
|
|
1064
|
-
// NOTE:
|
|
1065
|
-
// Fix to work around `ssrTransfrom` bug in Vite.
|
|
1066
|
-
// https://github.com/vitejs/vite/issues/4306
|
|
1067
|
-
// To get around this, use temporary variables.
|
|
1068
|
-
// https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
|
|
1069
|
-
let format = formatScope;
|
|
1070
|
-
// if you use default message, set it as message format!
|
|
1071
|
-
let cacheBaseKey = key;
|
|
1072
|
-
if (!resolvedMessage &&
|
|
1073
|
-
!(shared.isString(format) ||
|
|
1074
|
-
isMessageAST(format) ||
|
|
1075
|
-
isMessageFunction(format))) {
|
|
1076
|
-
if (enableDefaultMsg) {
|
|
1077
|
-
format = defaultMsgOrKey;
|
|
1078
|
-
cacheBaseKey = format;
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
// checking message format and target locale
|
|
1082
|
-
if (!resolvedMessage &&
|
|
1083
|
-
(!(shared.isString(format) ||
|
|
1084
|
-
isMessageAST(format) ||
|
|
1085
|
-
isMessageFunction(format)) ||
|
|
1086
|
-
!shared.isString(targetLocale))) {
|
|
1087
|
-
return unresolving ? NOT_REOSLVED : key;
|
|
1088
|
-
}
|
|
1089
|
-
// TODO: refactor
|
|
1090
|
-
if (shared.isString(format) && context.messageCompiler == null) {
|
|
1091
|
-
shared.warn(`The message format compilation is not supported in this build. ` +
|
|
1092
|
-
`Because message compiler isn't included. ` +
|
|
1093
|
-
`You need to pre-compilation all message format. ` +
|
|
1094
|
-
`So translate function return '${key}'.`);
|
|
1095
|
-
return key;
|
|
1096
|
-
}
|
|
1097
|
-
// setup compile error detecting
|
|
1098
|
-
let occurred = false;
|
|
1099
|
-
const onError = () => {
|
|
1100
|
-
occurred = true;
|
|
802
|
+
fallbackFormat,
|
|
803
|
+
unresolving,
|
|
804
|
+
postTranslation,
|
|
805
|
+
processor,
|
|
806
|
+
warnHtmlMessage,
|
|
807
|
+
escapeParameter,
|
|
808
|
+
messageCompiler,
|
|
809
|
+
messageResolver,
|
|
810
|
+
localeFallbacker,
|
|
811
|
+
fallbackContext,
|
|
812
|
+
onWarn,
|
|
813
|
+
__meta
|
|
1101
814
|
};
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
815
|
+
{
|
|
816
|
+
context.datetimeFormats = datetimeFormats;
|
|
817
|
+
context.numberFormats = numberFormats;
|
|
818
|
+
context.__datetimeFormatters = __datetimeFormatters;
|
|
819
|
+
context.__numberFormatters = __numberFormatters;
|
|
820
|
+
}
|
|
821
|
+
// for vue-devtools timeline event
|
|
822
|
+
{
|
|
823
|
+
context.__v_emitter =
|
|
824
|
+
internalOptions.__v_emitter != null
|
|
825
|
+
? internalOptions.__v_emitter
|
|
826
|
+
: undefined;
|
|
1109
827
|
}
|
|
1110
|
-
// evaluate message with context
|
|
1111
|
-
const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
|
|
1112
|
-
const msgContext = createMessageContext(ctxOptions);
|
|
1113
|
-
const messaged = evaluateMessage(context, msg, msgContext);
|
|
1114
|
-
// if use post translation option, proceed it with handler
|
|
1115
|
-
const ret = postTranslation
|
|
1116
|
-
? postTranslation(messaged, key)
|
|
1117
|
-
: messaged;
|
|
1118
828
|
// NOTE: experimental !!
|
|
1119
829
|
{
|
|
1120
|
-
|
|
1121
|
-
const payloads = {
|
|
1122
|
-
timestamp: Date.now(),
|
|
1123
|
-
key: shared.isString(key)
|
|
1124
|
-
? key
|
|
1125
|
-
: isMessageFunction(format)
|
|
1126
|
-
? format.key
|
|
1127
|
-
: '',
|
|
1128
|
-
locale: targetLocale || (isMessageFunction(format)
|
|
1129
|
-
? format.locale
|
|
1130
|
-
: ''),
|
|
1131
|
-
format: shared.isString(format)
|
|
1132
|
-
? format
|
|
1133
|
-
: isMessageFunction(format)
|
|
1134
|
-
? format.source
|
|
1135
|
-
: '',
|
|
1136
|
-
message: ret
|
|
1137
|
-
};
|
|
1138
|
-
payloads.meta = shared.assign({}, context.__meta, getAdditionalMeta() || {});
|
|
1139
|
-
translateDevTools(payloads);
|
|
830
|
+
initI18nDevTools(context, version, __meta);
|
|
1140
831
|
}
|
|
1141
|
-
return
|
|
832
|
+
return context;
|
|
1142
833
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
}
|
|
1147
|
-
else if (shared.isObject(options.named)) {
|
|
1148
|
-
Object.keys(options.named).forEach(key => {
|
|
1149
|
-
if (shared.isString(options.named[key])) {
|
|
1150
|
-
options.named[key] = shared.escapeHtml(options.named[key]);
|
|
1151
|
-
}
|
|
1152
|
-
});
|
|
1153
|
-
}
|
|
834
|
+
/** @internal */
|
|
835
|
+
function isTranslateFallbackWarn(fallback, key) {
|
|
836
|
+
return fallback instanceof RegExp ? fallback.test(key) : fallback;
|
|
1154
837
|
}
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
let message = {};
|
|
1159
|
-
let targetLocale;
|
|
1160
|
-
let format = null;
|
|
1161
|
-
let from = locale;
|
|
1162
|
-
let to = null;
|
|
1163
|
-
const type = 'translate';
|
|
1164
|
-
for (let i = 0; i < locales.length; i++) {
|
|
1165
|
-
targetLocale = to = locales[i];
|
|
1166
|
-
if (locale !== targetLocale &&
|
|
1167
|
-
!isAlmostSameLocale(locale, targetLocale) &&
|
|
1168
|
-
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
1169
|
-
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_TRANSLATE, {
|
|
1170
|
-
key,
|
|
1171
|
-
target: targetLocale
|
|
1172
|
-
}));
|
|
1173
|
-
}
|
|
1174
|
-
// for vue-devtools timeline event
|
|
1175
|
-
if (locale !== targetLocale) {
|
|
1176
|
-
const emitter = context.__v_emitter;
|
|
1177
|
-
if (emitter) {
|
|
1178
|
-
emitter.emit('fallback', {
|
|
1179
|
-
type,
|
|
1180
|
-
key,
|
|
1181
|
-
from,
|
|
1182
|
-
to,
|
|
1183
|
-
groupId: `${type}:${key}`
|
|
1184
|
-
});
|
|
1185
|
-
}
|
|
1186
|
-
}
|
|
1187
|
-
message =
|
|
1188
|
-
messages[targetLocale] || {};
|
|
1189
|
-
// for vue-devtools timeline event
|
|
1190
|
-
let start = null;
|
|
1191
|
-
let startTag;
|
|
1192
|
-
let endTag;
|
|
1193
|
-
if (shared.inBrowser) {
|
|
1194
|
-
start = window.performance.now();
|
|
1195
|
-
startTag = 'intlify-message-resolve-start';
|
|
1196
|
-
endTag = 'intlify-message-resolve-end';
|
|
1197
|
-
shared.mark && shared.mark(startTag);
|
|
1198
|
-
}
|
|
1199
|
-
if ((format = resolveValue(message, key)) === null) {
|
|
1200
|
-
// if null, resolve with object key path
|
|
1201
|
-
format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1202
|
-
}
|
|
1203
|
-
// for vue-devtools timeline event
|
|
1204
|
-
if (shared.inBrowser) {
|
|
1205
|
-
const end = window.performance.now();
|
|
1206
|
-
const emitter = context.__v_emitter;
|
|
1207
|
-
if (emitter && start && format) {
|
|
1208
|
-
emitter.emit('message-resolve', {
|
|
1209
|
-
type: 'message-resolve',
|
|
1210
|
-
key,
|
|
1211
|
-
message: format,
|
|
1212
|
-
time: end - start,
|
|
1213
|
-
groupId: `${type}:${key}`
|
|
1214
|
-
});
|
|
1215
|
-
}
|
|
1216
|
-
if (startTag && endTag && shared.mark && shared.measure) {
|
|
1217
|
-
shared.mark(endTag);
|
|
1218
|
-
shared.measure('intlify message resolve', startTag, endTag);
|
|
1219
|
-
}
|
|
1220
|
-
}
|
|
1221
|
-
if (shared.isString(format) || isMessageAST(format) || isMessageFunction(format)) {
|
|
1222
|
-
break;
|
|
1223
|
-
}
|
|
1224
|
-
if (!isImplicitFallback(targetLocale, locales)) {
|
|
1225
|
-
const missingRet = handleMissing(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1226
|
-
key, targetLocale, missingWarn, type);
|
|
1227
|
-
if (missingRet !== key) {
|
|
1228
|
-
format = missingRet;
|
|
1229
|
-
}
|
|
1230
|
-
}
|
|
1231
|
-
from = to;
|
|
1232
|
-
}
|
|
1233
|
-
return [format, targetLocale, message];
|
|
838
|
+
/** @internal */
|
|
839
|
+
function isTranslateMissingWarn(missing, key) {
|
|
840
|
+
return missing instanceof RegExp ? missing.test(key) : missing;
|
|
1234
841
|
}
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
const msg = format;
|
|
1239
|
-
msg.locale = msg.locale || targetLocale;
|
|
1240
|
-
msg.key = msg.key || key;
|
|
1241
|
-
return msg;
|
|
1242
|
-
}
|
|
1243
|
-
if (messageCompiler == null) {
|
|
1244
|
-
const msg = (() => format);
|
|
1245
|
-
msg.locale = targetLocale;
|
|
1246
|
-
msg.key = key;
|
|
1247
|
-
return msg;
|
|
1248
|
-
}
|
|
1249
|
-
// for vue-devtools timeline event
|
|
1250
|
-
let start = null;
|
|
1251
|
-
let startTag;
|
|
1252
|
-
let endTag;
|
|
1253
|
-
if (shared.inBrowser) {
|
|
1254
|
-
start = window.performance.now();
|
|
1255
|
-
startTag = 'intlify-message-compilation-start';
|
|
1256
|
-
endTag = 'intlify-message-compilation-end';
|
|
1257
|
-
shared.mark && shared.mark(startTag);
|
|
1258
|
-
}
|
|
1259
|
-
const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
|
|
842
|
+
/** @internal */
|
|
843
|
+
function handleMissing(context, key, locale, missingWarn, type) {
|
|
844
|
+
const { missing, onWarn } = context;
|
|
1260
845
|
// for vue-devtools timeline event
|
|
1261
|
-
|
|
1262
|
-
const end = window.performance.now();
|
|
846
|
+
{
|
|
1263
847
|
const emitter = context.__v_emitter;
|
|
1264
|
-
if (emitter
|
|
1265
|
-
emitter.emit('
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
groupId: `${
|
|
848
|
+
if (emitter) {
|
|
849
|
+
emitter.emit('missing', {
|
|
850
|
+
locale,
|
|
851
|
+
key,
|
|
852
|
+
type,
|
|
853
|
+
groupId: `${type}:${key}`
|
|
1270
854
|
});
|
|
1271
855
|
}
|
|
1272
|
-
if (startTag && endTag && shared.mark && shared.measure) {
|
|
1273
|
-
shared.mark(endTag);
|
|
1274
|
-
shared.measure('intlify message compilation', startTag, endTag);
|
|
1275
|
-
}
|
|
1276
856
|
}
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
return msg;
|
|
1281
|
-
}
|
|
1282
|
-
function evaluateMessage(context, msg, msgCtx) {
|
|
1283
|
-
// for vue-devtools timeline event
|
|
1284
|
-
let start = null;
|
|
1285
|
-
let startTag;
|
|
1286
|
-
let endTag;
|
|
1287
|
-
if (shared.inBrowser) {
|
|
1288
|
-
start = window.performance.now();
|
|
1289
|
-
startTag = 'intlify-message-evaluation-start';
|
|
1290
|
-
endTag = 'intlify-message-evaluation-end';
|
|
1291
|
-
shared.mark && shared.mark(startTag);
|
|
857
|
+
if (missing !== null) {
|
|
858
|
+
const ret = missing(context, locale, key, type);
|
|
859
|
+
return shared.isString(ret) ? ret : key;
|
|
1292
860
|
}
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
const end = window.performance.now();
|
|
1297
|
-
const emitter = context.__v_emitter;
|
|
1298
|
-
if (emitter && start) {
|
|
1299
|
-
emitter.emit('message-evaluation', {
|
|
1300
|
-
type: 'message-evaluation',
|
|
1301
|
-
value: messaged,
|
|
1302
|
-
time: end - start,
|
|
1303
|
-
groupId: `${'translate'}:${msg.key}`
|
|
1304
|
-
});
|
|
1305
|
-
}
|
|
1306
|
-
if (startTag && endTag && shared.mark && shared.measure) {
|
|
1307
|
-
shared.mark(endTag);
|
|
1308
|
-
shared.measure('intlify message evaluation', startTag, endTag);
|
|
861
|
+
else {
|
|
862
|
+
if (isTranslateMissingWarn(missingWarn, key)) {
|
|
863
|
+
onWarn(getWarnMessage(CoreWarnCodes.NOT_FOUND_KEY, { key, locale }));
|
|
1309
864
|
}
|
|
865
|
+
return key;
|
|
1310
866
|
}
|
|
1311
|
-
return messaged;
|
|
1312
867
|
}
|
|
1313
868
|
/** @internal */
|
|
1314
|
-
function
|
|
1315
|
-
const
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
options.plural = arg2;
|
|
1331
|
-
}
|
|
1332
|
-
else if (shared.isString(arg2)) {
|
|
1333
|
-
options.default = arg2;
|
|
869
|
+
function updateFallbackLocale(ctx, locale, fallback) {
|
|
870
|
+
const context = ctx;
|
|
871
|
+
context.__localeChainCache = new Map();
|
|
872
|
+
ctx.localeFallbacker(ctx, fallback, locale);
|
|
873
|
+
}
|
|
874
|
+
/** @internal */
|
|
875
|
+
function isAlmostSameLocale(locale, compareLocale) {
|
|
876
|
+
if (locale === compareLocale)
|
|
877
|
+
return false;
|
|
878
|
+
return locale.split('-')[0] === compareLocale.split('-')[0];
|
|
879
|
+
}
|
|
880
|
+
/** @internal */
|
|
881
|
+
function isImplicitFallback(targetLocale, locales) {
|
|
882
|
+
const index = locales.indexOf(targetLocale);
|
|
883
|
+
if (index === -1) {
|
|
884
|
+
return false;
|
|
1334
885
|
}
|
|
1335
|
-
|
|
1336
|
-
|
|
886
|
+
for (let i = index + 1; i < locales.length; i++) {
|
|
887
|
+
if (isAlmostSameLocale(targetLocale, locales[i])) {
|
|
888
|
+
return true;
|
|
889
|
+
}
|
|
1337
890
|
}
|
|
1338
|
-
|
|
1339
|
-
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
894
|
+
|
|
895
|
+
const intlDefined = typeof Intl !== 'undefined';
|
|
896
|
+
const Availabilities = {
|
|
897
|
+
dateTimeFormat: intlDefined && typeof Intl.DateTimeFormat !== 'undefined',
|
|
898
|
+
numberFormat: intlDefined && typeof Intl.NumberFormat !== 'undefined'
|
|
899
|
+
};
|
|
900
|
+
|
|
901
|
+
// implementation of `datetime` function
|
|
902
|
+
function datetime(context, ...args) {
|
|
903
|
+
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
904
|
+
const { __datetimeFormatters } = context;
|
|
905
|
+
if (!Availabilities.dateTimeFormat) {
|
|
906
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
907
|
+
return MISSING_RESOLVE_VALUE;
|
|
1340
908
|
}
|
|
1341
|
-
|
|
1342
|
-
|
|
909
|
+
const [key, value, options, overrides] = parseDateTimeArgs(...args);
|
|
910
|
+
const missingWarn = shared.isBoolean(options.missingWarn)
|
|
911
|
+
? options.missingWarn
|
|
912
|
+
: context.missingWarn;
|
|
913
|
+
const fallbackWarn = shared.isBoolean(options.fallbackWarn)
|
|
914
|
+
? options.fallbackWarn
|
|
915
|
+
: context.fallbackWarn;
|
|
916
|
+
const part = !!options.part;
|
|
917
|
+
const locale = getLocale(context, options);
|
|
918
|
+
const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
919
|
+
fallbackLocale, locale);
|
|
920
|
+
if (!shared.isString(key) || key === '') {
|
|
921
|
+
return new Intl.DateTimeFormat(locale, overrides).format(value);
|
|
1343
922
|
}
|
|
1344
|
-
|
|
1345
|
-
|
|
923
|
+
// resolve format
|
|
924
|
+
let datetimeFormat = {};
|
|
925
|
+
let targetLocale;
|
|
926
|
+
let format = null;
|
|
927
|
+
let from = locale;
|
|
928
|
+
let to = null;
|
|
929
|
+
const type = 'datetime format';
|
|
930
|
+
for (let i = 0; i < locales.length; i++) {
|
|
931
|
+
targetLocale = to = locales[i];
|
|
932
|
+
if (locale !== targetLocale &&
|
|
933
|
+
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
934
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_DATE_FORMAT, {
|
|
935
|
+
key,
|
|
936
|
+
target: targetLocale
|
|
937
|
+
}));
|
|
938
|
+
}
|
|
939
|
+
// for vue-devtools timeline event
|
|
940
|
+
if (locale !== targetLocale) {
|
|
941
|
+
const emitter = context.__v_emitter;
|
|
942
|
+
if (emitter) {
|
|
943
|
+
emitter.emit('fallback', {
|
|
944
|
+
type,
|
|
945
|
+
key,
|
|
946
|
+
from,
|
|
947
|
+
to,
|
|
948
|
+
groupId: `${type}:${key}`
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
datetimeFormat =
|
|
953
|
+
datetimeFormats[targetLocale] || {};
|
|
954
|
+
format = datetimeFormat[key];
|
|
955
|
+
if (shared.isPlainObject(format))
|
|
956
|
+
break;
|
|
957
|
+
handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
958
|
+
from = to;
|
|
1346
959
|
}
|
|
1347
|
-
|
|
1348
|
-
|
|
960
|
+
// checking format and target locale
|
|
961
|
+
if (!shared.isPlainObject(format) || !shared.isString(targetLocale)) {
|
|
962
|
+
return unresolving ? NOT_REOSLVED : key;
|
|
1349
963
|
}
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
return {
|
|
1354
|
-
locale,
|
|
1355
|
-
key,
|
|
1356
|
-
warnHtmlMessage,
|
|
1357
|
-
onError: (err) => {
|
|
1358
|
-
onError && onError(err);
|
|
1359
|
-
{
|
|
1360
|
-
const _source = getSourceForCodeFrame(source);
|
|
1361
|
-
const message = `Message compilation error: ${err.message}`;
|
|
1362
|
-
const codeFrame = err.location &&
|
|
1363
|
-
_source &&
|
|
1364
|
-
shared.generateCodeFrame(_source, err.location.start.offset, err.location.end.offset);
|
|
1365
|
-
const emitter = context.__v_emitter;
|
|
1366
|
-
if (emitter && _source) {
|
|
1367
|
-
emitter.emit('compile-error', {
|
|
1368
|
-
message: _source,
|
|
1369
|
-
error: err.message,
|
|
1370
|
-
start: err.location && err.location.start.offset,
|
|
1371
|
-
end: err.location && err.location.end.offset,
|
|
1372
|
-
groupId: `${'translate'}:${key}`
|
|
1373
|
-
});
|
|
1374
|
-
}
|
|
1375
|
-
console.error(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
1376
|
-
}
|
|
1377
|
-
},
|
|
1378
|
-
onCacheKey: (source) => shared.generateFormatCacheKey(locale, key, source)
|
|
1379
|
-
};
|
|
1380
|
-
}
|
|
1381
|
-
function getSourceForCodeFrame(source) {
|
|
1382
|
-
if (shared.isString(source)) {
|
|
1383
|
-
return source;
|
|
964
|
+
let id = `${targetLocale}__${key}`;
|
|
965
|
+
if (!shared.isEmptyObject(overrides)) {
|
|
966
|
+
id = `${id}__${JSON.stringify(overrides)}`;
|
|
1384
967
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
968
|
+
let formatter = __datetimeFormatters.get(id);
|
|
969
|
+
if (!formatter) {
|
|
970
|
+
formatter = new Intl.DateTimeFormat(targetLocale, shared.assign({}, format, overrides));
|
|
971
|
+
__datetimeFormatters.set(id, formatter);
|
|
1389
972
|
}
|
|
973
|
+
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1390
974
|
}
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
975
|
+
/** @internal */
|
|
976
|
+
const DATETIME_FORMAT_OPTIONS_KEYS = [
|
|
977
|
+
'localeMatcher',
|
|
978
|
+
'weekday',
|
|
979
|
+
'era',
|
|
980
|
+
'year',
|
|
981
|
+
'month',
|
|
982
|
+
'day',
|
|
983
|
+
'hour',
|
|
984
|
+
'minute',
|
|
985
|
+
'second',
|
|
986
|
+
'timeZoneName',
|
|
987
|
+
'formatMatcher',
|
|
988
|
+
'hour12',
|
|
989
|
+
'timeZone',
|
|
990
|
+
'dateStyle',
|
|
991
|
+
'timeStyle',
|
|
992
|
+
'calendar',
|
|
993
|
+
'dayPeriod',
|
|
994
|
+
'numberingSystem',
|
|
995
|
+
'hourCycle',
|
|
996
|
+
'fractionalSecondDigits'
|
|
997
|
+
];
|
|
998
|
+
/** @internal */
|
|
999
|
+
function parseDateTimeArgs(...args) {
|
|
1000
|
+
const [arg1, arg2, arg3, arg4] = args;
|
|
1001
|
+
const options = {};
|
|
1002
|
+
let overrides = {};
|
|
1003
|
+
let value;
|
|
1004
|
+
if (shared.isString(arg1)) {
|
|
1005
|
+
// Only allow ISO strings - other date formats are often supported,
|
|
1006
|
+
// but may cause different results in different browsers.
|
|
1007
|
+
const matches = arg1.match(/(\d{4}-\d{2}-\d{2})(T|\s)?(.*)/);
|
|
1008
|
+
if (!matches) {
|
|
1009
|
+
throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
1399
1010
|
}
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1011
|
+
// Some browsers can not parse the iso datetime separated by space,
|
|
1012
|
+
// this is a compromise solution by replace the 'T'/' ' with 'T'
|
|
1013
|
+
const dateTime = matches[3]
|
|
1014
|
+
? matches[3].trim().startsWith('T')
|
|
1015
|
+
? `${matches[1].trim()}${matches[3].trim()}`
|
|
1016
|
+
: `${matches[1].trim()}T${matches[3].trim()}`
|
|
1017
|
+
: matches[1].trim();
|
|
1018
|
+
value = new Date(dateTime);
|
|
1019
|
+
try {
|
|
1020
|
+
// This will fail if the date is not valid
|
|
1021
|
+
value.toISOString();
|
|
1409
1022
|
}
|
|
1410
|
-
|
|
1411
|
-
|
|
1023
|
+
catch {
|
|
1024
|
+
throw createCoreError(CoreErrorCodes.INVALID_ISO_DATE_ARGUMENT);
|
|
1412
1025
|
}
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1026
|
+
}
|
|
1027
|
+
else if (shared.isDate(arg1)) {
|
|
1028
|
+
if (isNaN(arg1.getTime())) {
|
|
1029
|
+
throw createCoreError(CoreErrorCodes.INVALID_DATE_ARGUMENT);
|
|
1416
1030
|
}
|
|
1417
|
-
|
|
1418
|
-
const ctxOptions = {
|
|
1419
|
-
locale,
|
|
1420
|
-
modifiers,
|
|
1421
|
-
pluralRules,
|
|
1422
|
-
messages: resolveMessage
|
|
1423
|
-
};
|
|
1424
|
-
if (context.processor) {
|
|
1425
|
-
ctxOptions.processor = context.processor;
|
|
1031
|
+
value = arg1;
|
|
1426
1032
|
}
|
|
1427
|
-
if (
|
|
1428
|
-
|
|
1033
|
+
else if (shared.isNumber(arg1)) {
|
|
1034
|
+
value = arg1;
|
|
1035
|
+
}
|
|
1036
|
+
else {
|
|
1037
|
+
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1038
|
+
}
|
|
1039
|
+
if (shared.isString(arg2)) {
|
|
1040
|
+
options.key = arg2;
|
|
1041
|
+
}
|
|
1042
|
+
else if (shared.isPlainObject(arg2)) {
|
|
1043
|
+
Object.keys(arg2).forEach(key => {
|
|
1044
|
+
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1045
|
+
overrides[key] = arg2[key];
|
|
1046
|
+
}
|
|
1047
|
+
else {
|
|
1048
|
+
options[key] = arg2[key];
|
|
1049
|
+
}
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
if (shared.isString(arg3)) {
|
|
1053
|
+
options.locale = arg3;
|
|
1054
|
+
}
|
|
1055
|
+
else if (shared.isPlainObject(arg3)) {
|
|
1056
|
+
overrides = arg3;
|
|
1429
1057
|
}
|
|
1430
|
-
if (
|
|
1431
|
-
|
|
1058
|
+
if (shared.isPlainObject(arg4)) {
|
|
1059
|
+
overrides = arg4;
|
|
1432
1060
|
}
|
|
1433
|
-
|
|
1434
|
-
|
|
1061
|
+
return [options.key || '', value, options, overrides];
|
|
1062
|
+
}
|
|
1063
|
+
/** @internal */
|
|
1064
|
+
function clearDateTimeFormat(ctx, locale, format) {
|
|
1065
|
+
const context = ctx;
|
|
1066
|
+
for (const key in format) {
|
|
1067
|
+
const id = `${locale}__${key}`;
|
|
1068
|
+
if (!context.__datetimeFormatters.has(id)) {
|
|
1069
|
+
continue;
|
|
1070
|
+
}
|
|
1071
|
+
context.__datetimeFormatters.delete(id);
|
|
1435
1072
|
}
|
|
1436
|
-
return ctxOptions;
|
|
1437
1073
|
}
|
|
1438
1074
|
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
// implementation of `datetime` function
|
|
1446
|
-
function datetime(context, ...args) {
|
|
1447
|
-
const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
1448
|
-
const { __datetimeFormatters } = context;
|
|
1449
|
-
if (!Availabilities.dateTimeFormat) {
|
|
1450
|
-
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_DATE));
|
|
1075
|
+
// implementation of `number` function
|
|
1076
|
+
function number(context, ...args) {
|
|
1077
|
+
const { numberFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
|
|
1078
|
+
const { __numberFormatters } = context;
|
|
1079
|
+
if (!Availabilities.numberFormat) {
|
|
1080
|
+
onWarn(getWarnMessage(CoreWarnCodes.CANNOT_FORMAT_NUMBER));
|
|
1451
1081
|
return MISSING_RESOLVE_VALUE;
|
|
1452
1082
|
}
|
|
1453
|
-
const [key, value, options, overrides] =
|
|
1083
|
+
const [key, value, options, overrides] = parseNumberArgs(...args);
|
|
1454
1084
|
const missingWarn = shared.isBoolean(options.missingWarn)
|
|
1455
1085
|
? options.missingWarn
|
|
1456
1086
|
: context.missingWarn;
|
|
@@ -1462,20 +1092,20 @@ function datetime(context, ...args) {
|
|
|
1462
1092
|
const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1463
1093
|
fallbackLocale, locale);
|
|
1464
1094
|
if (!shared.isString(key) || key === '') {
|
|
1465
|
-
return new Intl.
|
|
1095
|
+
return new Intl.NumberFormat(locale, overrides).format(value);
|
|
1466
1096
|
}
|
|
1467
1097
|
// resolve format
|
|
1468
|
-
let
|
|
1098
|
+
let numberFormat = {};
|
|
1469
1099
|
let targetLocale;
|
|
1470
1100
|
let format = null;
|
|
1471
1101
|
let from = locale;
|
|
1472
1102
|
let to = null;
|
|
1473
|
-
const type = '
|
|
1103
|
+
const type = 'number format';
|
|
1474
1104
|
for (let i = 0; i < locales.length; i++) {
|
|
1475
1105
|
targetLocale = to = locales[i];
|
|
1476
1106
|
if (locale !== targetLocale &&
|
|
1477
1107
|
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
1478
|
-
onWarn(getWarnMessage(CoreWarnCodes.
|
|
1108
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_NUMBER_FORMAT, {
|
|
1479
1109
|
key,
|
|
1480
1110
|
target: targetLocale
|
|
1481
1111
|
}));
|
|
@@ -1493,9 +1123,9 @@ function datetime(context, ...args) {
|
|
|
1493
1123
|
});
|
|
1494
1124
|
}
|
|
1495
1125
|
}
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
format =
|
|
1126
|
+
numberFormat =
|
|
1127
|
+
numberFormats[targetLocale] || {};
|
|
1128
|
+
format = numberFormat[key];
|
|
1499
1129
|
if (shared.isPlainObject(format))
|
|
1500
1130
|
break;
|
|
1501
1131
|
handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
@@ -1509,147 +1139,356 @@ function datetime(context, ...args) {
|
|
|
1509
1139
|
if (!shared.isEmptyObject(overrides)) {
|
|
1510
1140
|
id = `${id}__${JSON.stringify(overrides)}`;
|
|
1511
1141
|
}
|
|
1512
|
-
let formatter =
|
|
1142
|
+
let formatter = __numberFormatters.get(id);
|
|
1513
1143
|
if (!formatter) {
|
|
1514
|
-
formatter = new Intl.
|
|
1515
|
-
|
|
1144
|
+
formatter = new Intl.NumberFormat(targetLocale, shared.assign({}, format, overrides));
|
|
1145
|
+
__numberFormatters.set(id, formatter);
|
|
1516
1146
|
}
|
|
1517
1147
|
return !part ? formatter.format(value) : formatter.formatToParts(value);
|
|
1518
1148
|
}
|
|
1519
1149
|
/** @internal */
|
|
1520
|
-
const
|
|
1150
|
+
const NUMBER_FORMAT_OPTIONS_KEYS = [
|
|
1521
1151
|
'localeMatcher',
|
|
1522
|
-
'
|
|
1523
|
-
'
|
|
1524
|
-
'
|
|
1525
|
-
'
|
|
1526
|
-
'
|
|
1527
|
-
'
|
|
1528
|
-
'
|
|
1529
|
-
'
|
|
1530
|
-
'
|
|
1531
|
-
'
|
|
1532
|
-
'
|
|
1533
|
-
'
|
|
1534
|
-
'
|
|
1535
|
-
'
|
|
1536
|
-
'
|
|
1537
|
-
'
|
|
1538
|
-
'
|
|
1539
|
-
'
|
|
1540
|
-
'
|
|
1152
|
+
'style',
|
|
1153
|
+
'currency',
|
|
1154
|
+
'currencyDisplay',
|
|
1155
|
+
'currencySign',
|
|
1156
|
+
'useGrouping',
|
|
1157
|
+
'minimumIntegerDigits',
|
|
1158
|
+
'minimumFractionDigits',
|
|
1159
|
+
'maximumFractionDigits',
|
|
1160
|
+
'minimumSignificantDigits',
|
|
1161
|
+
'maximumSignificantDigits',
|
|
1162
|
+
'compactDisplay',
|
|
1163
|
+
'notation',
|
|
1164
|
+
'signDisplay',
|
|
1165
|
+
'unit',
|
|
1166
|
+
'unitDisplay',
|
|
1167
|
+
'roundingMode',
|
|
1168
|
+
'roundingPriority',
|
|
1169
|
+
'roundingIncrement',
|
|
1170
|
+
'trailingZeroDisplay'
|
|
1541
1171
|
];
|
|
1542
1172
|
/** @internal */
|
|
1543
|
-
function
|
|
1173
|
+
function parseNumberArgs(...args) {
|
|
1544
1174
|
const [arg1, arg2, arg3, arg4] = args;
|
|
1545
1175
|
const options = {};
|
|
1546
1176
|
let overrides = {};
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1177
|
+
if (!shared.isNumber(arg1)) {
|
|
1178
|
+
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1179
|
+
}
|
|
1180
|
+
const value = arg1;
|
|
1181
|
+
if (shared.isString(arg2)) {
|
|
1182
|
+
options.key = arg2;
|
|
1183
|
+
}
|
|
1184
|
+
else if (shared.isPlainObject(arg2)) {
|
|
1185
|
+
Object.keys(arg2).forEach(key => {
|
|
1186
|
+
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1187
|
+
overrides[key] = arg2[key];
|
|
1188
|
+
}
|
|
1189
|
+
else {
|
|
1190
|
+
options[key] = arg2[key];
|
|
1191
|
+
}
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
if (shared.isString(arg3)) {
|
|
1195
|
+
options.locale = arg3;
|
|
1196
|
+
}
|
|
1197
|
+
else if (shared.isPlainObject(arg3)) {
|
|
1198
|
+
overrides = arg3;
|
|
1199
|
+
}
|
|
1200
|
+
if (shared.isPlainObject(arg4)) {
|
|
1201
|
+
overrides = arg4;
|
|
1202
|
+
}
|
|
1203
|
+
return [options.key || '', value, options, overrides];
|
|
1204
|
+
}
|
|
1205
|
+
/** @internal */
|
|
1206
|
+
function clearNumberFormat(ctx, locale, format) {
|
|
1207
|
+
const context = ctx;
|
|
1208
|
+
for (const key in format) {
|
|
1209
|
+
const id = `${locale}__${key}`;
|
|
1210
|
+
if (!context.__numberFormatters.has(id)) {
|
|
1211
|
+
continue;
|
|
1569
1212
|
}
|
|
1213
|
+
context.__numberFormatters.delete(id);
|
|
1570
1214
|
}
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
const DEFAULT_MODIFIER = (str) => str;
|
|
1218
|
+
const DEFAULT_MESSAGE = (ctx) => ''; // eslint-disable-line
|
|
1219
|
+
const DEFAULT_MESSAGE_DATA_TYPE = 'text';
|
|
1220
|
+
const DEFAULT_NORMALIZE = (values) => values.length === 0 ? '' : shared.join(values);
|
|
1221
|
+
const DEFAULT_INTERPOLATE = shared.toDisplayString;
|
|
1222
|
+
function pluralDefault(choice, choicesLength) {
|
|
1223
|
+
choice = Math.abs(choice);
|
|
1224
|
+
if (choicesLength === 2) {
|
|
1225
|
+
// prettier-ignore
|
|
1226
|
+
return choice
|
|
1227
|
+
? choice > 1
|
|
1228
|
+
? 1
|
|
1229
|
+
: 0
|
|
1230
|
+
: 1;
|
|
1231
|
+
}
|
|
1232
|
+
return choice ? Math.min(choice, 2) : 0;
|
|
1233
|
+
}
|
|
1234
|
+
function getPluralIndex(options) {
|
|
1235
|
+
// prettier-ignore
|
|
1236
|
+
const index = shared.isNumber(options.pluralIndex)
|
|
1237
|
+
? options.pluralIndex
|
|
1238
|
+
: -1;
|
|
1239
|
+
// prettier-ignore
|
|
1240
|
+
return options.named && (shared.isNumber(options.named.count) || shared.isNumber(options.named.n))
|
|
1241
|
+
? shared.isNumber(options.named.count)
|
|
1242
|
+
? options.named.count
|
|
1243
|
+
: shared.isNumber(options.named.n)
|
|
1244
|
+
? options.named.n
|
|
1245
|
+
: index
|
|
1246
|
+
: index;
|
|
1247
|
+
}
|
|
1248
|
+
function normalizeNamed(pluralIndex, props) {
|
|
1249
|
+
if (!props.count) {
|
|
1250
|
+
props.count = pluralIndex;
|
|
1251
|
+
}
|
|
1252
|
+
if (!props.n) {
|
|
1253
|
+
props.n = pluralIndex;
|
|
1254
|
+
}
|
|
1255
|
+
}
|
|
1256
|
+
function createMessageContext(options = {}) {
|
|
1257
|
+
const locale = options.locale;
|
|
1258
|
+
const pluralIndex = getPluralIndex(options);
|
|
1259
|
+
const pluralRule = shared.isObject(options.pluralRules) &&
|
|
1260
|
+
shared.isString(locale) &&
|
|
1261
|
+
shared.isFunction(options.pluralRules[locale])
|
|
1262
|
+
? options.pluralRules[locale]
|
|
1263
|
+
: pluralDefault;
|
|
1264
|
+
const orgPluralRule = shared.isObject(options.pluralRules) &&
|
|
1265
|
+
shared.isString(locale) &&
|
|
1266
|
+
shared.isFunction(options.pluralRules[locale])
|
|
1267
|
+
? pluralDefault
|
|
1268
|
+
: undefined;
|
|
1269
|
+
const plural = (messages) => {
|
|
1270
|
+
return messages[pluralRule(pluralIndex, messages.length, orgPluralRule)];
|
|
1271
|
+
};
|
|
1272
|
+
const _list = options.list || [];
|
|
1273
|
+
const list = (index) => _list[index];
|
|
1274
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1275
|
+
const _named = options.named || {};
|
|
1276
|
+
shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
|
|
1277
|
+
const named = (key) => _named[key];
|
|
1278
|
+
function message(key, useLinked) {
|
|
1279
|
+
// prettier-ignore
|
|
1280
|
+
const msg = shared.isFunction(options.messages)
|
|
1281
|
+
? options.messages(key, !!useLinked)
|
|
1282
|
+
: shared.isObject(options.messages)
|
|
1283
|
+
? options.messages[key]
|
|
1284
|
+
: false;
|
|
1285
|
+
return !msg
|
|
1286
|
+
? options.parent
|
|
1287
|
+
? options.parent.message(key) // resolve from parent messages
|
|
1288
|
+
: DEFAULT_MESSAGE
|
|
1289
|
+
: msg;
|
|
1290
|
+
}
|
|
1291
|
+
const _modifier = (name) => options.modifiers
|
|
1292
|
+
? options.modifiers[name]
|
|
1293
|
+
: DEFAULT_MODIFIER;
|
|
1294
|
+
const normalize = shared.isPlainObject(options.processor) && shared.isFunction(options.processor.normalize)
|
|
1295
|
+
? options.processor.normalize
|
|
1296
|
+
: DEFAULT_NORMALIZE;
|
|
1297
|
+
const interpolate = shared.isPlainObject(options.processor) &&
|
|
1298
|
+
shared.isFunction(options.processor.interpolate)
|
|
1299
|
+
? options.processor.interpolate
|
|
1300
|
+
: DEFAULT_INTERPOLATE;
|
|
1301
|
+
const type = shared.isPlainObject(options.processor) && shared.isString(options.processor.type)
|
|
1302
|
+
? options.processor.type
|
|
1303
|
+
: DEFAULT_MESSAGE_DATA_TYPE;
|
|
1304
|
+
const linked = (key, ...args) => {
|
|
1305
|
+
const [arg1, arg2] = args;
|
|
1306
|
+
let type = 'text';
|
|
1307
|
+
let modifier = '';
|
|
1308
|
+
if (args.length === 1) {
|
|
1309
|
+
if (shared.isObject(arg1)) {
|
|
1310
|
+
modifier = arg1.modifier || modifier;
|
|
1311
|
+
type = arg1.type || type;
|
|
1312
|
+
}
|
|
1313
|
+
else if (shared.isString(arg1)) {
|
|
1314
|
+
modifier = arg1 || modifier;
|
|
1315
|
+
}
|
|
1574
1316
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
value = arg1;
|
|
1579
|
-
}
|
|
1580
|
-
else {
|
|
1581
|
-
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1582
|
-
}
|
|
1583
|
-
if (shared.isString(arg2)) {
|
|
1584
|
-
options.key = arg2;
|
|
1585
|
-
}
|
|
1586
|
-
else if (shared.isPlainObject(arg2)) {
|
|
1587
|
-
Object.keys(arg2).forEach(key => {
|
|
1588
|
-
if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1589
|
-
overrides[key] = arg2[key];
|
|
1317
|
+
else if (args.length === 2) {
|
|
1318
|
+
if (shared.isString(arg1)) {
|
|
1319
|
+
modifier = arg1 || modifier;
|
|
1590
1320
|
}
|
|
1591
|
-
|
|
1592
|
-
|
|
1321
|
+
if (shared.isString(arg2)) {
|
|
1322
|
+
type = arg2 || type;
|
|
1593
1323
|
}
|
|
1594
|
-
});
|
|
1595
|
-
}
|
|
1596
|
-
if (shared.isString(arg3)) {
|
|
1597
|
-
options.locale = arg3;
|
|
1598
|
-
}
|
|
1599
|
-
else if (shared.isPlainObject(arg3)) {
|
|
1600
|
-
overrides = arg3;
|
|
1601
|
-
}
|
|
1602
|
-
if (shared.isPlainObject(arg4)) {
|
|
1603
|
-
overrides = arg4;
|
|
1604
|
-
}
|
|
1605
|
-
return [options.key || '', value, options, overrides];
|
|
1606
|
-
}
|
|
1607
|
-
/** @internal */
|
|
1608
|
-
function clearDateTimeFormat(ctx, locale, format) {
|
|
1609
|
-
const context = ctx;
|
|
1610
|
-
for (const key in format) {
|
|
1611
|
-
const id = `${locale}__${key}`;
|
|
1612
|
-
if (!context.__datetimeFormatters.has(id)) {
|
|
1613
|
-
continue;
|
|
1614
1324
|
}
|
|
1615
|
-
|
|
1616
|
-
|
|
1325
|
+
const ret = message(key, true)(ctx);
|
|
1326
|
+
const msg =
|
|
1327
|
+
// The message in vnode resolved with linked are returned as an array by processor.nomalize
|
|
1328
|
+
type === 'vnode' && shared.isArray(ret) && modifier
|
|
1329
|
+
? ret[0]
|
|
1330
|
+
: ret;
|
|
1331
|
+
return modifier ? _modifier(modifier)(msg, type) : msg;
|
|
1332
|
+
};
|
|
1333
|
+
const ctx = {
|
|
1334
|
+
["list" /* HelperNameMap.LIST */]: list,
|
|
1335
|
+
["named" /* HelperNameMap.NAMED */]: named,
|
|
1336
|
+
["plural" /* HelperNameMap.PLURAL */]: plural,
|
|
1337
|
+
["linked" /* HelperNameMap.LINKED */]: linked,
|
|
1338
|
+
["message" /* HelperNameMap.MESSAGE */]: message,
|
|
1339
|
+
["type" /* HelperNameMap.TYPE */]: type,
|
|
1340
|
+
["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
|
|
1341
|
+
["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
|
|
1342
|
+
["values" /* HelperNameMap.VALUES */]: shared.assign({}, _list, _named)
|
|
1343
|
+
};
|
|
1344
|
+
return ctx;
|
|
1617
1345
|
}
|
|
1618
1346
|
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
return MISSING_RESOLVE_VALUE;
|
|
1626
|
-
}
|
|
1627
|
-
const [key, value, options, overrides] = parseNumberArgs(...args);
|
|
1347
|
+
const NOOP_MESSAGE_FUNCTION = () => '';
|
|
1348
|
+
const isMessageFunction = (val) => shared.isFunction(val);
|
|
1349
|
+
// implementation of `translate` function
|
|
1350
|
+
function translate(context, ...args) {
|
|
1351
|
+
const { fallbackFormat, postTranslation, unresolving, messageCompiler, fallbackLocale, messages } = context;
|
|
1352
|
+
const [key, options] = parseTranslateArgs(...args);
|
|
1628
1353
|
const missingWarn = shared.isBoolean(options.missingWarn)
|
|
1629
1354
|
? options.missingWarn
|
|
1630
1355
|
: context.missingWarn;
|
|
1631
1356
|
const fallbackWarn = shared.isBoolean(options.fallbackWarn)
|
|
1632
1357
|
? options.fallbackWarn
|
|
1633
1358
|
: context.fallbackWarn;
|
|
1634
|
-
const
|
|
1359
|
+
const escapeParameter = shared.isBoolean(options.escapeParameter)
|
|
1360
|
+
? options.escapeParameter
|
|
1361
|
+
: context.escapeParameter;
|
|
1362
|
+
const resolvedMessage = !!options.resolvedMessage;
|
|
1363
|
+
// prettier-ignore
|
|
1364
|
+
const defaultMsgOrKey = shared.isString(options.default) || shared.isBoolean(options.default) // default by function option
|
|
1365
|
+
? !shared.isBoolean(options.default)
|
|
1366
|
+
? options.default
|
|
1367
|
+
: (!messageCompiler ? () => key : key)
|
|
1368
|
+
: fallbackFormat // default by `fallbackFormat` option
|
|
1369
|
+
? (!messageCompiler ? () => key : key)
|
|
1370
|
+
: null;
|
|
1371
|
+
const enableDefaultMsg = fallbackFormat ||
|
|
1372
|
+
(defaultMsgOrKey != null &&
|
|
1373
|
+
(shared.isString(defaultMsgOrKey) || shared.isFunction(defaultMsgOrKey)));
|
|
1635
1374
|
const locale = getLocale(context, options);
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1375
|
+
// escape params
|
|
1376
|
+
escapeParameter && escapeParams(options);
|
|
1377
|
+
// resolve message format
|
|
1378
|
+
// eslint-disable-next-line prefer-const
|
|
1379
|
+
let [formatScope, targetLocale, message] = !resolvedMessage
|
|
1380
|
+
? resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn)
|
|
1381
|
+
: [
|
|
1382
|
+
key,
|
|
1383
|
+
locale,
|
|
1384
|
+
messages[locale] || {}
|
|
1385
|
+
];
|
|
1386
|
+
// NOTE:
|
|
1387
|
+
// Fix to work around `ssrTransfrom` bug in Vite.
|
|
1388
|
+
// https://github.com/vitejs/vite/issues/4306
|
|
1389
|
+
// To get around this, use temporary variables.
|
|
1390
|
+
// https://github.com/nuxt/framework/issues/1461#issuecomment-954606243
|
|
1391
|
+
let format = formatScope;
|
|
1392
|
+
// if you use default message, set it as message format!
|
|
1393
|
+
let cacheBaseKey = key;
|
|
1394
|
+
if (!resolvedMessage &&
|
|
1395
|
+
!(shared.isString(format) ||
|
|
1396
|
+
isMessageAST(format) ||
|
|
1397
|
+
isMessageFunction(format))) {
|
|
1398
|
+
if (enableDefaultMsg) {
|
|
1399
|
+
format = defaultMsgOrKey;
|
|
1400
|
+
cacheBaseKey = format;
|
|
1401
|
+
}
|
|
1640
1402
|
}
|
|
1641
|
-
//
|
|
1642
|
-
|
|
1403
|
+
// checking message format and target locale
|
|
1404
|
+
if (!resolvedMessage &&
|
|
1405
|
+
(!(shared.isString(format) ||
|
|
1406
|
+
isMessageAST(format) ||
|
|
1407
|
+
isMessageFunction(format)) ||
|
|
1408
|
+
!shared.isString(targetLocale))) {
|
|
1409
|
+
return unresolving ? NOT_REOSLVED : key;
|
|
1410
|
+
}
|
|
1411
|
+
// TODO: refactor
|
|
1412
|
+
if (shared.isString(format) && context.messageCompiler == null) {
|
|
1413
|
+
shared.warn(`The message format compilation is not supported in this build. ` +
|
|
1414
|
+
`Because message compiler isn't included. ` +
|
|
1415
|
+
`You need to pre-compilation all message format. ` +
|
|
1416
|
+
`So translate function return '${key}'.`);
|
|
1417
|
+
return key;
|
|
1418
|
+
}
|
|
1419
|
+
// setup compile error detecting
|
|
1420
|
+
let occurred = false;
|
|
1421
|
+
const onError = () => {
|
|
1422
|
+
occurred = true;
|
|
1423
|
+
};
|
|
1424
|
+
// compile message format
|
|
1425
|
+
const msg = !isMessageFunction(format)
|
|
1426
|
+
? compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError)
|
|
1427
|
+
: format;
|
|
1428
|
+
// if occurred compile error, return the message format
|
|
1429
|
+
if (occurred) {
|
|
1430
|
+
return format;
|
|
1431
|
+
}
|
|
1432
|
+
// evaluate message with context
|
|
1433
|
+
const ctxOptions = getMessageContextOptions(context, targetLocale, message, options);
|
|
1434
|
+
const msgContext = createMessageContext(ctxOptions);
|
|
1435
|
+
const messaged = evaluateMessage(context, msg, msgContext);
|
|
1436
|
+
// if use post translation option, proceed it with handler
|
|
1437
|
+
const ret = postTranslation
|
|
1438
|
+
? postTranslation(messaged, key)
|
|
1439
|
+
: messaged;
|
|
1440
|
+
// NOTE: experimental !!
|
|
1441
|
+
{
|
|
1442
|
+
// prettier-ignore
|
|
1443
|
+
const payloads = {
|
|
1444
|
+
timestamp: Date.now(),
|
|
1445
|
+
key: shared.isString(key)
|
|
1446
|
+
? key
|
|
1447
|
+
: isMessageFunction(format)
|
|
1448
|
+
? format.key
|
|
1449
|
+
: '',
|
|
1450
|
+
locale: targetLocale || (isMessageFunction(format)
|
|
1451
|
+
? format.locale
|
|
1452
|
+
: ''),
|
|
1453
|
+
format: shared.isString(format)
|
|
1454
|
+
? format
|
|
1455
|
+
: isMessageFunction(format)
|
|
1456
|
+
? format.source
|
|
1457
|
+
: '',
|
|
1458
|
+
message: ret
|
|
1459
|
+
};
|
|
1460
|
+
payloads.meta = shared.assign({}, context.__meta, getAdditionalMeta() || {});
|
|
1461
|
+
translateDevTools(payloads);
|
|
1462
|
+
}
|
|
1463
|
+
return ret;
|
|
1464
|
+
}
|
|
1465
|
+
function escapeParams(options) {
|
|
1466
|
+
if (shared.isArray(options.list)) {
|
|
1467
|
+
options.list = options.list.map(item => shared.isString(item) ? shared.escapeHtml(item) : item);
|
|
1468
|
+
}
|
|
1469
|
+
else if (shared.isObject(options.named)) {
|
|
1470
|
+
Object.keys(options.named).forEach(key => {
|
|
1471
|
+
if (shared.isString(options.named[key])) {
|
|
1472
|
+
options.named[key] = shared.escapeHtml(options.named[key]);
|
|
1473
|
+
}
|
|
1474
|
+
});
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
|
|
1478
|
+
const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
|
|
1479
|
+
const locales = localeFallbacker(context, fallbackLocale, locale); // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1480
|
+
let message = {};
|
|
1643
1481
|
let targetLocale;
|
|
1644
1482
|
let format = null;
|
|
1645
1483
|
let from = locale;
|
|
1646
1484
|
let to = null;
|
|
1647
|
-
const type = '
|
|
1485
|
+
const type = 'translate';
|
|
1648
1486
|
for (let i = 0; i < locales.length; i++) {
|
|
1649
1487
|
targetLocale = to = locales[i];
|
|
1650
1488
|
if (locale !== targetLocale &&
|
|
1489
|
+
!isAlmostSameLocale(locale, targetLocale) &&
|
|
1651
1490
|
isTranslateFallbackWarn(fallbackWarn, key)) {
|
|
1652
|
-
onWarn(getWarnMessage(CoreWarnCodes.
|
|
1491
|
+
onWarn(getWarnMessage(CoreWarnCodes.FALLBACK_TO_TRANSLATE, {
|
|
1653
1492
|
key,
|
|
1654
1493
|
target: targetLocale
|
|
1655
1494
|
}));
|
|
@@ -1667,95 +1506,257 @@ function number(context, ...args) {
|
|
|
1667
1506
|
});
|
|
1668
1507
|
}
|
|
1669
1508
|
}
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1509
|
+
message =
|
|
1510
|
+
messages[targetLocale] || {};
|
|
1511
|
+
// for vue-devtools timeline event
|
|
1512
|
+
let start = null;
|
|
1513
|
+
let startTag;
|
|
1514
|
+
let endTag;
|
|
1515
|
+
if (shared.inBrowser) {
|
|
1516
|
+
start = window.performance.now();
|
|
1517
|
+
startTag = 'intlify-message-resolve-start';
|
|
1518
|
+
endTag = 'intlify-message-resolve-end';
|
|
1519
|
+
shared.mark && shared.mark(startTag);
|
|
1520
|
+
}
|
|
1521
|
+
if ((format = resolveValue(message, key)) === null) {
|
|
1522
|
+
// if null, resolve with object key path
|
|
1523
|
+
format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1524
|
+
}
|
|
1525
|
+
// for vue-devtools timeline event
|
|
1526
|
+
if (shared.inBrowser) {
|
|
1527
|
+
const end = window.performance.now();
|
|
1528
|
+
const emitter = context.__v_emitter;
|
|
1529
|
+
if (emitter && start && format) {
|
|
1530
|
+
emitter.emit('message-resolve', {
|
|
1531
|
+
type: 'message-resolve',
|
|
1532
|
+
key,
|
|
1533
|
+
message: format,
|
|
1534
|
+
time: end - start,
|
|
1535
|
+
groupId: `${type}:${key}`
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
if (startTag && endTag && shared.mark && shared.measure) {
|
|
1539
|
+
shared.mark(endTag);
|
|
1540
|
+
shared.measure('intlify message resolve', startTag, endTag);
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
if (shared.isString(format) || isMessageAST(format) || isMessageFunction(format)) {
|
|
1544
|
+
break;
|
|
1545
|
+
}
|
|
1546
|
+
if (!isImplicitFallback(targetLocale, locales)) {
|
|
1547
|
+
const missingRet = handleMissing(context, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1548
|
+
key, targetLocale, missingWarn, type);
|
|
1549
|
+
if (missingRet !== key) {
|
|
1550
|
+
format = missingRet;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1676
1553
|
from = to;
|
|
1677
1554
|
}
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1555
|
+
return [format, targetLocale, message];
|
|
1556
|
+
}
|
|
1557
|
+
function compileMessageFormat(context, key, targetLocale, format, cacheBaseKey, onError) {
|
|
1558
|
+
const { messageCompiler, warnHtmlMessage } = context;
|
|
1559
|
+
if (isMessageFunction(format)) {
|
|
1560
|
+
const msg = format;
|
|
1561
|
+
msg.locale = msg.locale || targetLocale;
|
|
1562
|
+
msg.key = msg.key || key;
|
|
1563
|
+
return msg;
|
|
1681
1564
|
}
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1565
|
+
if (messageCompiler == null) {
|
|
1566
|
+
const msg = (() => format);
|
|
1567
|
+
msg.locale = targetLocale;
|
|
1568
|
+
msg.key = key;
|
|
1569
|
+
return msg;
|
|
1685
1570
|
}
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1571
|
+
// for vue-devtools timeline event
|
|
1572
|
+
let start = null;
|
|
1573
|
+
let startTag;
|
|
1574
|
+
let endTag;
|
|
1575
|
+
if (shared.inBrowser) {
|
|
1576
|
+
start = window.performance.now();
|
|
1577
|
+
startTag = 'intlify-message-compilation-start';
|
|
1578
|
+
endTag = 'intlify-message-compilation-end';
|
|
1579
|
+
shared.mark && shared.mark(startTag);
|
|
1690
1580
|
}
|
|
1691
|
-
|
|
1581
|
+
const msg = messageCompiler(format, getCompileContext(context, targetLocale, cacheBaseKey, format, warnHtmlMessage, onError));
|
|
1582
|
+
// for vue-devtools timeline event
|
|
1583
|
+
if (shared.inBrowser) {
|
|
1584
|
+
const end = window.performance.now();
|
|
1585
|
+
const emitter = context.__v_emitter;
|
|
1586
|
+
if (emitter && start) {
|
|
1587
|
+
emitter.emit('message-compilation', {
|
|
1588
|
+
type: 'message-compilation',
|
|
1589
|
+
message: format,
|
|
1590
|
+
time: end - start,
|
|
1591
|
+
groupId: `${'translate'}:${key}`
|
|
1592
|
+
});
|
|
1593
|
+
}
|
|
1594
|
+
if (startTag && endTag && shared.mark && shared.measure) {
|
|
1595
|
+
shared.mark(endTag);
|
|
1596
|
+
shared.measure('intlify message compilation', startTag, endTag);
|
|
1597
|
+
}
|
|
1598
|
+
}
|
|
1599
|
+
msg.locale = targetLocale;
|
|
1600
|
+
msg.key = key;
|
|
1601
|
+
msg.source = format;
|
|
1602
|
+
return msg;
|
|
1603
|
+
}
|
|
1604
|
+
function evaluateMessage(context, msg, msgCtx) {
|
|
1605
|
+
// for vue-devtools timeline event
|
|
1606
|
+
let start = null;
|
|
1607
|
+
let startTag;
|
|
1608
|
+
let endTag;
|
|
1609
|
+
if (shared.inBrowser) {
|
|
1610
|
+
start = window.performance.now();
|
|
1611
|
+
startTag = 'intlify-message-evaluation-start';
|
|
1612
|
+
endTag = 'intlify-message-evaluation-end';
|
|
1613
|
+
shared.mark && shared.mark(startTag);
|
|
1614
|
+
}
|
|
1615
|
+
const messaged = msg(msgCtx);
|
|
1616
|
+
// for vue-devtools timeline event
|
|
1617
|
+
if (shared.inBrowser) {
|
|
1618
|
+
const end = window.performance.now();
|
|
1619
|
+
const emitter = context.__v_emitter;
|
|
1620
|
+
if (emitter && start) {
|
|
1621
|
+
emitter.emit('message-evaluation', {
|
|
1622
|
+
type: 'message-evaluation',
|
|
1623
|
+
value: messaged,
|
|
1624
|
+
time: end - start,
|
|
1625
|
+
groupId: `${'translate'}:${msg.key}`
|
|
1626
|
+
});
|
|
1627
|
+
}
|
|
1628
|
+
if (startTag && endTag && shared.mark && shared.measure) {
|
|
1629
|
+
shared.mark(endTag);
|
|
1630
|
+
shared.measure('intlify message evaluation', startTag, endTag);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
return messaged;
|
|
1692
1634
|
}
|
|
1693
1635
|
/** @internal */
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
'style',
|
|
1697
|
-
'currency',
|
|
1698
|
-
'currencyDisplay',
|
|
1699
|
-
'currencySign',
|
|
1700
|
-
'useGrouping',
|
|
1701
|
-
'minimumIntegerDigits',
|
|
1702
|
-
'minimumFractionDigits',
|
|
1703
|
-
'maximumFractionDigits',
|
|
1704
|
-
'minimumSignificantDigits',
|
|
1705
|
-
'maximumSignificantDigits',
|
|
1706
|
-
'compactDisplay',
|
|
1707
|
-
'notation',
|
|
1708
|
-
'signDisplay',
|
|
1709
|
-
'unit',
|
|
1710
|
-
'unitDisplay',
|
|
1711
|
-
'roundingMode',
|
|
1712
|
-
'roundingPriority',
|
|
1713
|
-
'roundingIncrement',
|
|
1714
|
-
'trailingZeroDisplay'
|
|
1715
|
-
];
|
|
1716
|
-
/** @internal */
|
|
1717
|
-
function parseNumberArgs(...args) {
|
|
1718
|
-
const [arg1, arg2, arg3, arg4] = args;
|
|
1636
|
+
function parseTranslateArgs(...args) {
|
|
1637
|
+
const [arg1, arg2, arg3] = args;
|
|
1719
1638
|
const options = {};
|
|
1720
|
-
|
|
1721
|
-
|
|
1639
|
+
if (!shared.isString(arg1) &&
|
|
1640
|
+
!shared.isNumber(arg1) &&
|
|
1641
|
+
!isMessageFunction(arg1) &&
|
|
1642
|
+
!isMessageAST(arg1)) {
|
|
1722
1643
|
throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
|
|
1723
1644
|
}
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1645
|
+
// prettier-ignore
|
|
1646
|
+
const key = shared.isNumber(arg1)
|
|
1647
|
+
? String(arg1)
|
|
1648
|
+
: isMessageFunction(arg1)
|
|
1649
|
+
? arg1
|
|
1650
|
+
: arg1;
|
|
1651
|
+
if (shared.isNumber(arg2)) {
|
|
1652
|
+
options.plural = arg2;
|
|
1727
1653
|
}
|
|
1728
|
-
else if (shared.
|
|
1729
|
-
|
|
1730
|
-
if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
|
|
1731
|
-
overrides[key] = arg2[key];
|
|
1732
|
-
}
|
|
1733
|
-
else {
|
|
1734
|
-
options[key] = arg2[key];
|
|
1735
|
-
}
|
|
1736
|
-
});
|
|
1654
|
+
else if (shared.isString(arg2)) {
|
|
1655
|
+
options.default = arg2;
|
|
1737
1656
|
}
|
|
1738
|
-
if (shared.
|
|
1739
|
-
options.
|
|
1657
|
+
else if (shared.isPlainObject(arg2) && !shared.isEmptyObject(arg2)) {
|
|
1658
|
+
options.named = arg2;
|
|
1659
|
+
}
|
|
1660
|
+
else if (shared.isArray(arg2)) {
|
|
1661
|
+
options.list = arg2;
|
|
1662
|
+
}
|
|
1663
|
+
if (shared.isNumber(arg3)) {
|
|
1664
|
+
options.plural = arg3;
|
|
1665
|
+
}
|
|
1666
|
+
else if (shared.isString(arg3)) {
|
|
1667
|
+
options.default = arg3;
|
|
1740
1668
|
}
|
|
1741
1669
|
else if (shared.isPlainObject(arg3)) {
|
|
1742
|
-
|
|
1670
|
+
shared.assign(options, arg3);
|
|
1743
1671
|
}
|
|
1744
|
-
|
|
1745
|
-
|
|
1672
|
+
return [key, options];
|
|
1673
|
+
}
|
|
1674
|
+
function getCompileContext(context, locale, key, source, warnHtmlMessage, onError) {
|
|
1675
|
+
return {
|
|
1676
|
+
locale,
|
|
1677
|
+
key,
|
|
1678
|
+
warnHtmlMessage,
|
|
1679
|
+
onError: (err) => {
|
|
1680
|
+
onError && onError(err);
|
|
1681
|
+
{
|
|
1682
|
+
const _source = getSourceForCodeFrame(source);
|
|
1683
|
+
const message = `Message compilation error: ${err.message}`;
|
|
1684
|
+
const codeFrame = err.location &&
|
|
1685
|
+
_source &&
|
|
1686
|
+
shared.generateCodeFrame(_source, err.location.start.offset, err.location.end.offset);
|
|
1687
|
+
const emitter = context.__v_emitter;
|
|
1688
|
+
if (emitter && _source) {
|
|
1689
|
+
emitter.emit('compile-error', {
|
|
1690
|
+
message: _source,
|
|
1691
|
+
error: err.message,
|
|
1692
|
+
start: err.location && err.location.start.offset,
|
|
1693
|
+
end: err.location && err.location.end.offset,
|
|
1694
|
+
groupId: `${'translate'}:${key}`
|
|
1695
|
+
});
|
|
1696
|
+
}
|
|
1697
|
+
console.error(codeFrame ? `${message}\n${codeFrame}` : message);
|
|
1698
|
+
}
|
|
1699
|
+
},
|
|
1700
|
+
onCacheKey: (source) => shared.generateFormatCacheKey(locale, key, source)
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
function getSourceForCodeFrame(source) {
|
|
1704
|
+
if (shared.isString(source)) {
|
|
1705
|
+
return source;
|
|
1706
|
+
}
|
|
1707
|
+
else {
|
|
1708
|
+
if (source.loc && source.loc.source) {
|
|
1709
|
+
return source.loc.source;
|
|
1710
|
+
}
|
|
1746
1711
|
}
|
|
1747
|
-
return [options.key || '', value, options, overrides];
|
|
1748
1712
|
}
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
const
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
if (
|
|
1755
|
-
|
|
1713
|
+
function getMessageContextOptions(context, locale, message, options) {
|
|
1714
|
+
const { modifiers, pluralRules, messageResolver: resolveValue, fallbackLocale, fallbackWarn, missingWarn, fallbackContext } = context;
|
|
1715
|
+
const resolveMessage = (key, useLinked) => {
|
|
1716
|
+
let val = resolveValue(message, key);
|
|
1717
|
+
// fallback
|
|
1718
|
+
if (val == null && (fallbackContext || useLinked)) {
|
|
1719
|
+
const [, , message] = resolveMessageFormat(fallbackContext || context, // NOTE: if has fallbackContext, fallback to root, else if use linked, fallback to local context
|
|
1720
|
+
key, locale, fallbackLocale, fallbackWarn, missingWarn);
|
|
1721
|
+
val = resolveValue(message, key);
|
|
1756
1722
|
}
|
|
1757
|
-
|
|
1723
|
+
if (shared.isString(val) || isMessageAST(val)) {
|
|
1724
|
+
let occurred = false;
|
|
1725
|
+
const onError = () => {
|
|
1726
|
+
occurred = true;
|
|
1727
|
+
};
|
|
1728
|
+
const msg = compileMessageFormat(context, key, locale, val, key, onError);
|
|
1729
|
+
return !occurred
|
|
1730
|
+
? msg
|
|
1731
|
+
: NOOP_MESSAGE_FUNCTION;
|
|
1732
|
+
}
|
|
1733
|
+
else if (isMessageFunction(val)) {
|
|
1734
|
+
return val;
|
|
1735
|
+
}
|
|
1736
|
+
else {
|
|
1737
|
+
// TODO: should be implemented warning message
|
|
1738
|
+
return NOOP_MESSAGE_FUNCTION;
|
|
1739
|
+
}
|
|
1740
|
+
};
|
|
1741
|
+
const ctxOptions = {
|
|
1742
|
+
locale,
|
|
1743
|
+
modifiers,
|
|
1744
|
+
pluralRules,
|
|
1745
|
+
messages: resolveMessage
|
|
1746
|
+
};
|
|
1747
|
+
if (context.processor) {
|
|
1748
|
+
ctxOptions.processor = context.processor;
|
|
1749
|
+
}
|
|
1750
|
+
if (options.list) {
|
|
1751
|
+
ctxOptions.list = options.list;
|
|
1752
|
+
}
|
|
1753
|
+
if (options.named) {
|
|
1754
|
+
ctxOptions.named = options.named;
|
|
1758
1755
|
}
|
|
1756
|
+
if (shared.isNumber(options.plural)) {
|
|
1757
|
+
ctxOptions.pluralIndex = options.plural;
|
|
1758
|
+
}
|
|
1759
|
+
return ctxOptions;
|
|
1759
1760
|
}
|
|
1760
1761
|
|
|
1761
1762
|
exports.CompileErrorCodes = messageCompiler.CompileErrorCodes;
|