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