vue-i18n 9.2.2 → 9.3.0-beta.2
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/vue-i18n.cjs +2353 -0
- package/dist/vue-i18n.cjs.js +1 -2353
- package/dist/vue-i18n.cjs.prod.js +1 -2206
- package/dist/vue-i18n.esm-browser.js +3 -3
- package/dist/vue-i18n.esm-browser.prod.js +2 -2
- package/dist/vue-i18n.esm-bundler.js +1 -2817
- package/dist/vue-i18n.global.js +3 -3
- package/dist/vue-i18n.global.prod.js +2 -2
- package/dist/vue-i18n.mjs +2809 -0
- package/dist/vue-i18n.node.mjs +2 -0
- package/dist/vue-i18n.prod.cjs +2206 -0
- package/dist/vue-i18n.runtime.esm-browser.js +3 -3
- package/dist/vue-i18n.runtime.esm-browser.prod.js +2 -2
- package/dist/vue-i18n.runtime.esm-bundler.js +1 -2815
- package/dist/vue-i18n.runtime.global.js +3 -3
- package/dist/vue-i18n.runtime.global.prod.js +2 -2
- package/dist/vue-i18n.runtime.mjs +2807 -0
- package/index.js +2 -2
- package/package.json +24 -17
- package/index.mjs +0 -1
|
@@ -0,0 +1,2353 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* vue-i18n v9.3.0-beta.2
|
|
3
|
+
* (c) 2022 kazuya kawaguchi
|
|
4
|
+
* Released under the MIT License.
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
9
|
+
|
|
10
|
+
var shared = require('@intlify/shared');
|
|
11
|
+
var coreBase = require('@intlify/core-base');
|
|
12
|
+
var vue = require('vue');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Vue I18n Version
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* Semver format. Same format as the package.json `version` field.
|
|
19
|
+
*
|
|
20
|
+
* @VueI18nGeneral
|
|
21
|
+
*/
|
|
22
|
+
const VERSION = '9.3.0-beta.2';
|
|
23
|
+
|
|
24
|
+
let code$1 = coreBase.CoreWarnCodes.__EXTEND_POINT__;
|
|
25
|
+
const inc$1 = () => ++code$1;
|
|
26
|
+
const I18nWarnCodes = {
|
|
27
|
+
FALLBACK_TO_ROOT: code$1,
|
|
28
|
+
NOT_SUPPORTED_PRESERVE: inc$1(),
|
|
29
|
+
NOT_SUPPORTED_FORMATTER: inc$1(),
|
|
30
|
+
NOT_SUPPORTED_PRESERVE_DIRECTIVE: inc$1(),
|
|
31
|
+
NOT_SUPPORTED_GET_CHOICE_INDEX: inc$1(),
|
|
32
|
+
COMPONENT_NAME_LEGACY_COMPATIBLE: inc$1(),
|
|
33
|
+
NOT_FOUND_PARENT_SCOPE: inc$1() // 13
|
|
34
|
+
};
|
|
35
|
+
const warnMessages = {
|
|
36
|
+
[I18nWarnCodes.FALLBACK_TO_ROOT]: `Fall back to {type} '{key}' with root locale.`,
|
|
37
|
+
[I18nWarnCodes.NOT_SUPPORTED_PRESERVE]: `Not supported 'preserve'.`,
|
|
38
|
+
[I18nWarnCodes.NOT_SUPPORTED_FORMATTER]: `Not supported 'formatter'.`,
|
|
39
|
+
[I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE]: `Not supported 'preserveDirectiveContent'.`,
|
|
40
|
+
[I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX]: `Not supported 'getChoiceIndex'.`,
|
|
41
|
+
[I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE]: `Component name legacy compatible: '{name}' -> 'i18n'`,
|
|
42
|
+
[I18nWarnCodes.NOT_FOUND_PARENT_SCOPE]: `Not found parent scope. use the global scope.`
|
|
43
|
+
};
|
|
44
|
+
function getWarnMessage(code, ...args) {
|
|
45
|
+
return shared.format(warnMessages[code], ...args);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let code = coreBase.CompileErrorCodes.__EXTEND_POINT__;
|
|
49
|
+
const inc = () => ++code;
|
|
50
|
+
const I18nErrorCodes = {
|
|
51
|
+
// composer module errors
|
|
52
|
+
UNEXPECTED_RETURN_TYPE: code,
|
|
53
|
+
// legacy module errors
|
|
54
|
+
INVALID_ARGUMENT: inc(),
|
|
55
|
+
// i18n module errors
|
|
56
|
+
MUST_BE_CALL_SETUP_TOP: inc(),
|
|
57
|
+
NOT_INSLALLED: inc(),
|
|
58
|
+
NOT_AVAILABLE_IN_LEGACY_MODE: inc(),
|
|
59
|
+
// directive module errors
|
|
60
|
+
REQUIRED_VALUE: inc(),
|
|
61
|
+
INVALID_VALUE: inc(),
|
|
62
|
+
// vue-devtools errors
|
|
63
|
+
CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: inc(),
|
|
64
|
+
NOT_INSLALLED_WITH_PROVIDE: inc(),
|
|
65
|
+
// unexpected error
|
|
66
|
+
UNEXPECTED_ERROR: inc(),
|
|
67
|
+
// not compatible legacy vue-i18n constructor
|
|
68
|
+
NOT_COMPATIBLE_LEGACY_VUE_I18N: inc(),
|
|
69
|
+
// bridge support vue 2.x only
|
|
70
|
+
BRIDGE_SUPPORT_VUE_2_ONLY: inc(),
|
|
71
|
+
// need to define `i18n` option in `allowComposition: true` and `useScope: 'local' at `useI18n``
|
|
72
|
+
MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION: inc(),
|
|
73
|
+
// Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly
|
|
74
|
+
NOT_AVAILABLE_COMPOSITION_IN_LEGACY: inc(),
|
|
75
|
+
// for enhancement
|
|
76
|
+
__EXTEND_POINT__: inc() // 29
|
|
77
|
+
};
|
|
78
|
+
function createI18nError(code, ...args) {
|
|
79
|
+
return coreBase.createCompileError(code, null, { messages: errorMessages, args } );
|
|
80
|
+
}
|
|
81
|
+
const errorMessages = {
|
|
82
|
+
[I18nErrorCodes.UNEXPECTED_RETURN_TYPE]: 'Unexpected return type in composer',
|
|
83
|
+
[I18nErrorCodes.INVALID_ARGUMENT]: 'Invalid argument',
|
|
84
|
+
[I18nErrorCodes.MUST_BE_CALL_SETUP_TOP]: 'Must be called at the top of a `setup` function',
|
|
85
|
+
[I18nErrorCodes.NOT_INSLALLED]: 'Need to install with `app.use` function',
|
|
86
|
+
[I18nErrorCodes.UNEXPECTED_ERROR]: 'Unexpected error',
|
|
87
|
+
[I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE]: 'Not available in legacy mode',
|
|
88
|
+
[I18nErrorCodes.REQUIRED_VALUE]: `Required in value: {0}`,
|
|
89
|
+
[I18nErrorCodes.INVALID_VALUE]: `Invalid value`,
|
|
90
|
+
[I18nErrorCodes.CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN]: `Cannot setup vue-devtools plugin`,
|
|
91
|
+
[I18nErrorCodes.NOT_INSLALLED_WITH_PROVIDE]: 'Need to install with `provide` function',
|
|
92
|
+
[I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N]: 'Not compatible legacy VueI18n.',
|
|
93
|
+
[I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY]: 'vue-i18n-bridge support Vue 2.x only',
|
|
94
|
+
[I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION]: 'Must define ‘i18n’ option or custom block in Composition API with using local scope in Legacy API mode',
|
|
95
|
+
[I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY]: 'Not available Compostion API in Legacy API mode. Please make sure that the legacy API mode is working properly'
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const TransrateVNodeSymbol =
|
|
99
|
+
/* #__PURE__*/ shared.makeSymbol('__transrateVNode');
|
|
100
|
+
const DatetimePartsSymbol = /* #__PURE__*/ shared.makeSymbol('__datetimeParts');
|
|
101
|
+
const NumberPartsSymbol = /* #__PURE__*/ shared.makeSymbol('__numberParts');
|
|
102
|
+
const EnableEmitter = /* #__PURE__*/ shared.makeSymbol('__enableEmitter');
|
|
103
|
+
const DisableEmitter = /* #__PURE__*/ shared.makeSymbol('__disableEmitter');
|
|
104
|
+
const SetPluralRulesSymbol = shared.makeSymbol('__setPluralRules');
|
|
105
|
+
shared.makeSymbol('__intlifyMeta');
|
|
106
|
+
const InejctWithOption = /* #__PURE__*/ shared.makeSymbol('__injectWithOption');
|
|
107
|
+
const __VUE_I18N_BRIDGE__ = '__VUE_I18N_BRIDGE__';
|
|
108
|
+
|
|
109
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
110
|
+
/**
|
|
111
|
+
* Transform flat json in obj to normal json in obj
|
|
112
|
+
*/
|
|
113
|
+
function handleFlatJson(obj) {
|
|
114
|
+
// check obj
|
|
115
|
+
if (!shared.isObject(obj)) {
|
|
116
|
+
return obj;
|
|
117
|
+
}
|
|
118
|
+
for (const key in obj) {
|
|
119
|
+
// check key
|
|
120
|
+
if (!shared.hasOwn(obj, key)) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
// handle for normal json
|
|
124
|
+
if (!key.includes('.')) {
|
|
125
|
+
// recursive process value if value is also a object
|
|
126
|
+
if (shared.isObject(obj[key])) {
|
|
127
|
+
handleFlatJson(obj[key]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// handle for flat json, transform to normal json
|
|
131
|
+
else {
|
|
132
|
+
// go to the last object
|
|
133
|
+
const subKeys = key.split('.');
|
|
134
|
+
const lastIndex = subKeys.length - 1;
|
|
135
|
+
let currentObj = obj;
|
|
136
|
+
for (let i = 0; i < lastIndex; i++) {
|
|
137
|
+
if (!(subKeys[i] in currentObj)) {
|
|
138
|
+
currentObj[subKeys[i]] = {};
|
|
139
|
+
}
|
|
140
|
+
currentObj = currentObj[subKeys[i]];
|
|
141
|
+
}
|
|
142
|
+
// update last object value, delete old property
|
|
143
|
+
currentObj[subKeys[lastIndex]] = obj[key];
|
|
144
|
+
delete obj[key];
|
|
145
|
+
// recursive process value if value is also a object
|
|
146
|
+
if (shared.isObject(currentObj[subKeys[lastIndex]])) {
|
|
147
|
+
handleFlatJson(currentObj[subKeys[lastIndex]]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return obj;
|
|
152
|
+
}
|
|
153
|
+
function getLocaleMessages(locale, options) {
|
|
154
|
+
const { messages, __i18n, messageResolver, flatJson } = options;
|
|
155
|
+
// prettier-ignore
|
|
156
|
+
const ret = shared.isPlainObject(messages)
|
|
157
|
+
? messages
|
|
158
|
+
: shared.isArray(__i18n)
|
|
159
|
+
? {}
|
|
160
|
+
: { [locale]: {} };
|
|
161
|
+
// merge locale messages of i18n custom block
|
|
162
|
+
if (shared.isArray(__i18n)) {
|
|
163
|
+
__i18n.forEach(custom => {
|
|
164
|
+
if ('locale' in custom && 'resource' in custom) {
|
|
165
|
+
const { locale, resource } = custom;
|
|
166
|
+
if (locale) {
|
|
167
|
+
ret[locale] = ret[locale] || {};
|
|
168
|
+
deepCopy(resource, ret[locale]);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
deepCopy(resource, ret);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
shared.isString(custom) && deepCopy(JSON.parse(custom), ret);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
// handle messages for flat json
|
|
180
|
+
if (messageResolver == null && flatJson) {
|
|
181
|
+
for (const key in ret) {
|
|
182
|
+
if (shared.hasOwn(ret, key)) {
|
|
183
|
+
handleFlatJson(ret[key]);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return ret;
|
|
188
|
+
}
|
|
189
|
+
const isNotObjectOrIsArray = (val) => !shared.isObject(val) || shared.isArray(val);
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
191
|
+
function deepCopy(src, des) {
|
|
192
|
+
// src and des should both be objects, and non of then can be a array
|
|
193
|
+
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
|
|
194
|
+
throw createI18nError(I18nErrorCodes.INVALID_VALUE);
|
|
195
|
+
}
|
|
196
|
+
for (const key in src) {
|
|
197
|
+
if (shared.hasOwn(src, key)) {
|
|
198
|
+
if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
|
|
199
|
+
// replace with src[key] when:
|
|
200
|
+
// src[key] or des[key] is not a object, or
|
|
201
|
+
// src[key] or des[key] is a array
|
|
202
|
+
des[key] = src[key];
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
// src[key] and des[key] are both object, merge them
|
|
206
|
+
deepCopy(src[key], des[key]);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
212
|
+
function getComponentOptions(instance) {
|
|
213
|
+
return instance.type ;
|
|
214
|
+
}
|
|
215
|
+
function adjustI18nResources(global, options, componentOptions // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
216
|
+
) {
|
|
217
|
+
let messages = shared.isObject(options.messages) ? options.messages : {};
|
|
218
|
+
if ('__i18nGlobal' in componentOptions) {
|
|
219
|
+
messages = getLocaleMessages(global.locale.value, {
|
|
220
|
+
messages,
|
|
221
|
+
__i18n: componentOptions.__i18nGlobal
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
// merge locale messages
|
|
225
|
+
const locales = Object.keys(messages);
|
|
226
|
+
if (locales.length) {
|
|
227
|
+
locales.forEach(locale => {
|
|
228
|
+
global.mergeLocaleMessage(locale, messages[locale]);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
{
|
|
232
|
+
// merge datetime formats
|
|
233
|
+
if (shared.isObject(options.datetimeFormats)) {
|
|
234
|
+
const locales = Object.keys(options.datetimeFormats);
|
|
235
|
+
if (locales.length) {
|
|
236
|
+
locales.forEach(locale => {
|
|
237
|
+
global.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
// merge number formats
|
|
242
|
+
if (shared.isObject(options.numberFormats)) {
|
|
243
|
+
const locales = Object.keys(options.numberFormats);
|
|
244
|
+
if (locales.length) {
|
|
245
|
+
locales.forEach(locale => {
|
|
246
|
+
global.mergeNumberFormat(locale, options.numberFormats[locale]);
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
function createTextNode(key) {
|
|
253
|
+
return vue.createVNode(vue.Text, null, key, 0)
|
|
254
|
+
;
|
|
255
|
+
}
|
|
256
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
257
|
+
|
|
258
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
259
|
+
// extend VNode interface
|
|
260
|
+
const DEVTOOLS_META = '__INTLIFY_META__';
|
|
261
|
+
let composerID = 0;
|
|
262
|
+
function defineCoreMissingHandler(missing) {
|
|
263
|
+
return ((ctx, locale, key, type) => {
|
|
264
|
+
return missing(locale, key, vue.getCurrentInstance() || undefined, type);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
// for Intlify DevTools
|
|
268
|
+
const getMetaInfo = () => {
|
|
269
|
+
const instance = vue.getCurrentInstance();
|
|
270
|
+
let meta = null; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
271
|
+
return instance && (meta = getComponentOptions(instance)[DEVTOOLS_META])
|
|
272
|
+
? { [DEVTOOLS_META]: meta } // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
273
|
+
: null;
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* Create composer interface factory
|
|
277
|
+
*
|
|
278
|
+
* @internal
|
|
279
|
+
*/
|
|
280
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
281
|
+
function createComposer(options = {}, VueI18nLegacy) {
|
|
282
|
+
const { __root } = options;
|
|
283
|
+
const _isGlobal = __root === undefined;
|
|
284
|
+
let _inheritLocale = shared.isBoolean(options.inheritLocale)
|
|
285
|
+
? options.inheritLocale
|
|
286
|
+
: true;
|
|
287
|
+
const _locale = vue.ref(
|
|
288
|
+
// prettier-ignore
|
|
289
|
+
__root && _inheritLocale
|
|
290
|
+
? __root.locale.value
|
|
291
|
+
: shared.isString(options.locale)
|
|
292
|
+
? options.locale
|
|
293
|
+
: coreBase.DEFAULT_LOCALE);
|
|
294
|
+
const _fallbackLocale = vue.ref(
|
|
295
|
+
// prettier-ignore
|
|
296
|
+
__root && _inheritLocale
|
|
297
|
+
? __root.fallbackLocale.value
|
|
298
|
+
: shared.isString(options.fallbackLocale) ||
|
|
299
|
+
shared.isArray(options.fallbackLocale) ||
|
|
300
|
+
shared.isPlainObject(options.fallbackLocale) ||
|
|
301
|
+
options.fallbackLocale === false
|
|
302
|
+
? options.fallbackLocale
|
|
303
|
+
: _locale.value);
|
|
304
|
+
const _messages = vue.ref(getLocaleMessages(_locale.value, options));
|
|
305
|
+
// prettier-ignore
|
|
306
|
+
const _datetimeFormats = vue.ref(shared.isPlainObject(options.datetimeFormats)
|
|
307
|
+
? options.datetimeFormats
|
|
308
|
+
: { [_locale.value]: {} })
|
|
309
|
+
;
|
|
310
|
+
// prettier-ignore
|
|
311
|
+
const _numberFormats = vue.ref(shared.isPlainObject(options.numberFormats)
|
|
312
|
+
? options.numberFormats
|
|
313
|
+
: { [_locale.value]: {} })
|
|
314
|
+
;
|
|
315
|
+
// warning suppress options
|
|
316
|
+
// prettier-ignore
|
|
317
|
+
let _missingWarn = __root
|
|
318
|
+
? __root.missingWarn
|
|
319
|
+
: shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
|
|
320
|
+
? options.missingWarn
|
|
321
|
+
: true;
|
|
322
|
+
// prettier-ignore
|
|
323
|
+
let _fallbackWarn = __root
|
|
324
|
+
? __root.fallbackWarn
|
|
325
|
+
: shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
|
|
326
|
+
? options.fallbackWarn
|
|
327
|
+
: true;
|
|
328
|
+
// prettier-ignore
|
|
329
|
+
let _fallbackRoot = __root
|
|
330
|
+
? __root.fallbackRoot
|
|
331
|
+
: shared.isBoolean(options.fallbackRoot)
|
|
332
|
+
? options.fallbackRoot
|
|
333
|
+
: true;
|
|
334
|
+
// configure fall back to root
|
|
335
|
+
let _fallbackFormat = !!options.fallbackFormat;
|
|
336
|
+
// runtime missing
|
|
337
|
+
let _missing = shared.isFunction(options.missing) ? options.missing : null;
|
|
338
|
+
let _runtimeMissing = shared.isFunction(options.missing)
|
|
339
|
+
? defineCoreMissingHandler(options.missing)
|
|
340
|
+
: null;
|
|
341
|
+
// postTranslation handler
|
|
342
|
+
let _postTranslation = shared.isFunction(options.postTranslation)
|
|
343
|
+
? options.postTranslation
|
|
344
|
+
: null;
|
|
345
|
+
// prettier-ignore
|
|
346
|
+
let _warnHtmlMessage = __root
|
|
347
|
+
? __root.warnHtmlMessage
|
|
348
|
+
: shared.isBoolean(options.warnHtmlMessage)
|
|
349
|
+
? options.warnHtmlMessage
|
|
350
|
+
: true;
|
|
351
|
+
let _escapeParameter = !!options.escapeParameter;
|
|
352
|
+
// custom linked modifiers
|
|
353
|
+
// prettier-ignore
|
|
354
|
+
const _modifiers = __root
|
|
355
|
+
? __root.modifiers
|
|
356
|
+
: shared.isPlainObject(options.modifiers)
|
|
357
|
+
? options.modifiers
|
|
358
|
+
: {};
|
|
359
|
+
// pluralRules
|
|
360
|
+
let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
|
|
361
|
+
// runtime context
|
|
362
|
+
// eslint-disable-next-line prefer-const
|
|
363
|
+
let _context;
|
|
364
|
+
const getCoreContext = () => {
|
|
365
|
+
_isGlobal && coreBase.setFallbackContext(null);
|
|
366
|
+
const ctxOptions = {
|
|
367
|
+
version: VERSION,
|
|
368
|
+
locale: _locale.value,
|
|
369
|
+
fallbackLocale: _fallbackLocale.value,
|
|
370
|
+
messages: _messages.value,
|
|
371
|
+
modifiers: _modifiers,
|
|
372
|
+
pluralRules: _pluralRules,
|
|
373
|
+
missing: _runtimeMissing === null ? undefined : _runtimeMissing,
|
|
374
|
+
missingWarn: _missingWarn,
|
|
375
|
+
fallbackWarn: _fallbackWarn,
|
|
376
|
+
fallbackFormat: _fallbackFormat,
|
|
377
|
+
unresolving: true,
|
|
378
|
+
postTranslation: _postTranslation === null ? undefined : _postTranslation,
|
|
379
|
+
warnHtmlMessage: _warnHtmlMessage,
|
|
380
|
+
escapeParameter: _escapeParameter,
|
|
381
|
+
messageResolver: options.messageResolver,
|
|
382
|
+
__meta: { framework: 'vue' }
|
|
383
|
+
};
|
|
384
|
+
{
|
|
385
|
+
ctxOptions.datetimeFormats = _datetimeFormats.value;
|
|
386
|
+
ctxOptions.numberFormats = _numberFormats.value;
|
|
387
|
+
ctxOptions.__datetimeFormatters = shared.isPlainObject(_context)
|
|
388
|
+
? _context.__datetimeFormatters
|
|
389
|
+
: undefined;
|
|
390
|
+
ctxOptions.__numberFormatters = shared.isPlainObject(_context)
|
|
391
|
+
? _context.__numberFormatters
|
|
392
|
+
: undefined;
|
|
393
|
+
}
|
|
394
|
+
{
|
|
395
|
+
ctxOptions.__v_emitter = shared.isPlainObject(_context)
|
|
396
|
+
? _context.__v_emitter
|
|
397
|
+
: undefined;
|
|
398
|
+
}
|
|
399
|
+
const ctx = coreBase.createCoreContext(ctxOptions);
|
|
400
|
+
_isGlobal && coreBase.setFallbackContext(ctx);
|
|
401
|
+
return ctx;
|
|
402
|
+
};
|
|
403
|
+
_context = getCoreContext();
|
|
404
|
+
coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
|
405
|
+
// track reactivity
|
|
406
|
+
function trackReactivityValues() {
|
|
407
|
+
return [
|
|
408
|
+
_locale.value,
|
|
409
|
+
_fallbackLocale.value,
|
|
410
|
+
_messages.value,
|
|
411
|
+
_datetimeFormats.value,
|
|
412
|
+
_numberFormats.value
|
|
413
|
+
]
|
|
414
|
+
;
|
|
415
|
+
}
|
|
416
|
+
// locale
|
|
417
|
+
const locale = vue.computed({
|
|
418
|
+
get: () => _locale.value,
|
|
419
|
+
set: val => {
|
|
420
|
+
_locale.value = val;
|
|
421
|
+
_context.locale = _locale.value;
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
// fallbackLocale
|
|
425
|
+
const fallbackLocale = vue.computed({
|
|
426
|
+
get: () => _fallbackLocale.value,
|
|
427
|
+
set: val => {
|
|
428
|
+
_fallbackLocale.value = val;
|
|
429
|
+
_context.fallbackLocale = _fallbackLocale.value;
|
|
430
|
+
coreBase.updateFallbackLocale(_context, _locale.value, val);
|
|
431
|
+
}
|
|
432
|
+
});
|
|
433
|
+
// messages
|
|
434
|
+
const messages = vue.computed(() => _messages.value);
|
|
435
|
+
// datetimeFormats
|
|
436
|
+
const datetimeFormats = /* #__PURE__*/ vue.computed(() => _datetimeFormats.value);
|
|
437
|
+
// numberFormats
|
|
438
|
+
const numberFormats = /* #__PURE__*/ vue.computed(() => _numberFormats.value);
|
|
439
|
+
// getPostTranslationHandler
|
|
440
|
+
function getPostTranslationHandler() {
|
|
441
|
+
return shared.isFunction(_postTranslation) ? _postTranslation : null;
|
|
442
|
+
}
|
|
443
|
+
// setPostTranslationHandler
|
|
444
|
+
function setPostTranslationHandler(handler) {
|
|
445
|
+
_postTranslation = handler;
|
|
446
|
+
_context.postTranslation = handler;
|
|
447
|
+
}
|
|
448
|
+
// getMissingHandler
|
|
449
|
+
function getMissingHandler() {
|
|
450
|
+
return _missing;
|
|
451
|
+
}
|
|
452
|
+
// setMissingHandler
|
|
453
|
+
function setMissingHandler(handler) {
|
|
454
|
+
if (handler !== null) {
|
|
455
|
+
_runtimeMissing = defineCoreMissingHandler(handler);
|
|
456
|
+
}
|
|
457
|
+
_missing = handler;
|
|
458
|
+
_context.missing = _runtimeMissing;
|
|
459
|
+
}
|
|
460
|
+
function isResolvedTranslateMessage(type, arg // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
461
|
+
) {
|
|
462
|
+
return type !== 'translate' || !arg.resolvedMessage;
|
|
463
|
+
}
|
|
464
|
+
const wrapWithDeps = (fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) => {
|
|
465
|
+
trackReactivityValues(); // track reactive dependency
|
|
466
|
+
// NOTE: experimental !!
|
|
467
|
+
let ret;
|
|
468
|
+
{
|
|
469
|
+
try {
|
|
470
|
+
coreBase.setAdditionalMeta(getMetaInfo());
|
|
471
|
+
if (!_isGlobal) {
|
|
472
|
+
_context.fallbackContext = __root
|
|
473
|
+
? coreBase.getFallbackContext()
|
|
474
|
+
: undefined;
|
|
475
|
+
}
|
|
476
|
+
ret = fn(_context);
|
|
477
|
+
}
|
|
478
|
+
finally {
|
|
479
|
+
coreBase.setAdditionalMeta(null);
|
|
480
|
+
if (!_isGlobal) {
|
|
481
|
+
_context.fallbackContext = undefined;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (shared.isNumber(ret) && ret === coreBase.NOT_REOSLVED) {
|
|
486
|
+
const [key, arg2] = argumentParser();
|
|
487
|
+
if (__root &&
|
|
488
|
+
shared.isString(key) &&
|
|
489
|
+
isResolvedTranslateMessage(warnType, arg2)) {
|
|
490
|
+
if (_fallbackRoot &&
|
|
491
|
+
(coreBase.isTranslateFallbackWarn(_fallbackWarn, key) ||
|
|
492
|
+
coreBase.isTranslateMissingWarn(_missingWarn, key))) {
|
|
493
|
+
shared.warn(getWarnMessage(I18nWarnCodes.FALLBACK_TO_ROOT, {
|
|
494
|
+
key,
|
|
495
|
+
type: warnType
|
|
496
|
+
}));
|
|
497
|
+
}
|
|
498
|
+
// for vue-devtools timeline event
|
|
499
|
+
{
|
|
500
|
+
const { __v_emitter: emitter } = _context;
|
|
501
|
+
if (emitter && _fallbackRoot) {
|
|
502
|
+
emitter.emit("fallback" /* FALBACK */, {
|
|
503
|
+
type: warnType,
|
|
504
|
+
key,
|
|
505
|
+
to: 'global',
|
|
506
|
+
groupId: `${warnType}:${key}`
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
return __root && _fallbackRoot
|
|
512
|
+
? fallbackSuccess(__root)
|
|
513
|
+
: fallbackFail(key);
|
|
514
|
+
}
|
|
515
|
+
else if (successCondition(ret)) {
|
|
516
|
+
return ret;
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
/* istanbul ignore next */
|
|
520
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE);
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
// t
|
|
524
|
+
function t(...args) {
|
|
525
|
+
return wrapWithDeps(context => Reflect.apply(coreBase.translate, null, [context, ...args]), () => coreBase.parseTranslateArgs(...args), 'translate', root => Reflect.apply(root.t, root, [...args]), key => key, val => shared.isString(val));
|
|
526
|
+
}
|
|
527
|
+
// rt
|
|
528
|
+
function rt(...args) {
|
|
529
|
+
const [arg1, arg2, arg3] = args;
|
|
530
|
+
if (arg3 && !shared.isObject(arg3)) {
|
|
531
|
+
throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
|
|
532
|
+
}
|
|
533
|
+
return t(...[arg1, arg2, shared.assign({ resolvedMessage: true }, arg3 || {})]);
|
|
534
|
+
}
|
|
535
|
+
// d
|
|
536
|
+
function d(...args) {
|
|
537
|
+
return wrapWithDeps(context => Reflect.apply(coreBase.datetime, null, [context, ...args]), () => coreBase.parseDateTimeArgs(...args), 'datetime format', root => Reflect.apply(root.d, root, [...args]), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
|
|
538
|
+
}
|
|
539
|
+
// n
|
|
540
|
+
function n(...args) {
|
|
541
|
+
return wrapWithDeps(context => Reflect.apply(coreBase.number, null, [context, ...args]), () => coreBase.parseNumberArgs(...args), 'number format', root => Reflect.apply(root.n, root, [...args]), () => coreBase.MISSING_RESOLVE_VALUE, val => shared.isString(val));
|
|
542
|
+
}
|
|
543
|
+
// for custom processor
|
|
544
|
+
function normalize(values) {
|
|
545
|
+
return values.map(val => shared.isString(val) || shared.isNumber(val) || shared.isBoolean(val)
|
|
546
|
+
? createTextNode(String(val))
|
|
547
|
+
: val);
|
|
548
|
+
}
|
|
549
|
+
const interpolate = (val) => val;
|
|
550
|
+
const processor = {
|
|
551
|
+
normalize,
|
|
552
|
+
interpolate,
|
|
553
|
+
type: 'vnode'
|
|
554
|
+
};
|
|
555
|
+
// transrateVNode, using for `i18n-t` component
|
|
556
|
+
function transrateVNode(...args) {
|
|
557
|
+
return wrapWithDeps(context => {
|
|
558
|
+
let ret;
|
|
559
|
+
const _context = context;
|
|
560
|
+
try {
|
|
561
|
+
_context.processor = processor;
|
|
562
|
+
ret = Reflect.apply(coreBase.translate, null, [_context, ...args]);
|
|
563
|
+
}
|
|
564
|
+
finally {
|
|
565
|
+
_context.processor = null;
|
|
566
|
+
}
|
|
567
|
+
return ret;
|
|
568
|
+
}, () => coreBase.parseTranslateArgs(...args), 'translate',
|
|
569
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
570
|
+
root => root[TransrateVNodeSymbol](...args), key => [createTextNode(key)], val => shared.isArray(val));
|
|
571
|
+
}
|
|
572
|
+
// numberParts, using for `i18n-n` component
|
|
573
|
+
function numberParts(...args) {
|
|
574
|
+
return wrapWithDeps(context => Reflect.apply(coreBase.number, null, [context, ...args]), () => coreBase.parseNumberArgs(...args), 'number format',
|
|
575
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
576
|
+
root => root[NumberPartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
|
|
577
|
+
}
|
|
578
|
+
// datetimeParts, using for `i18n-d` component
|
|
579
|
+
function datetimeParts(...args) {
|
|
580
|
+
return wrapWithDeps(context => Reflect.apply(coreBase.datetime, null, [context, ...args]), () => coreBase.parseDateTimeArgs(...args), 'datetime format',
|
|
581
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
582
|
+
root => root[DatetimePartsSymbol](...args), () => [], val => shared.isString(val) || shared.isArray(val));
|
|
583
|
+
}
|
|
584
|
+
function setPluralRules(rules) {
|
|
585
|
+
_pluralRules = rules;
|
|
586
|
+
_context.pluralRules = _pluralRules;
|
|
587
|
+
}
|
|
588
|
+
// te
|
|
589
|
+
function te(key, locale) {
|
|
590
|
+
const targetLocale = shared.isString(locale) ? locale : _locale.value;
|
|
591
|
+
const message = getLocaleMessage(targetLocale);
|
|
592
|
+
return _context.messageResolver(message, key) !== null;
|
|
593
|
+
}
|
|
594
|
+
function resolveMessages(key) {
|
|
595
|
+
let messages = null;
|
|
596
|
+
const locales = coreBase.fallbackWithLocaleChain(_context, _fallbackLocale.value, _locale.value);
|
|
597
|
+
for (let i = 0; i < locales.length; i++) {
|
|
598
|
+
const targetLocaleMessages = _messages.value[locales[i]] || {};
|
|
599
|
+
const messageValue = _context.messageResolver(targetLocaleMessages, key);
|
|
600
|
+
if (messageValue != null) {
|
|
601
|
+
messages = messageValue;
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
return messages;
|
|
606
|
+
}
|
|
607
|
+
// tm
|
|
608
|
+
function tm(key) {
|
|
609
|
+
const messages = resolveMessages(key);
|
|
610
|
+
// prettier-ignore
|
|
611
|
+
return messages != null
|
|
612
|
+
? messages
|
|
613
|
+
: __root
|
|
614
|
+
? __root.tm(key) || {}
|
|
615
|
+
: {};
|
|
616
|
+
}
|
|
617
|
+
// getLocaleMessage
|
|
618
|
+
function getLocaleMessage(locale) {
|
|
619
|
+
return (_messages.value[locale] || {});
|
|
620
|
+
}
|
|
621
|
+
// setLocaleMessage
|
|
622
|
+
function setLocaleMessage(locale, message) {
|
|
623
|
+
_messages.value[locale] = message;
|
|
624
|
+
_context.messages = _messages.value;
|
|
625
|
+
}
|
|
626
|
+
// mergeLocaleMessage
|
|
627
|
+
function mergeLocaleMessage(locale, message) {
|
|
628
|
+
_messages.value[locale] = _messages.value[locale] || {};
|
|
629
|
+
deepCopy(message, _messages.value[locale]);
|
|
630
|
+
_context.messages = _messages.value;
|
|
631
|
+
}
|
|
632
|
+
// getDateTimeFormat
|
|
633
|
+
function getDateTimeFormat(locale) {
|
|
634
|
+
return _datetimeFormats.value[locale] || {};
|
|
635
|
+
}
|
|
636
|
+
// setDateTimeFormat
|
|
637
|
+
function setDateTimeFormat(locale, format) {
|
|
638
|
+
_datetimeFormats.value[locale] = format;
|
|
639
|
+
_context.datetimeFormats = _datetimeFormats.value;
|
|
640
|
+
coreBase.clearDateTimeFormat(_context, locale, format);
|
|
641
|
+
}
|
|
642
|
+
// mergeDateTimeFormat
|
|
643
|
+
function mergeDateTimeFormat(locale, format) {
|
|
644
|
+
_datetimeFormats.value[locale] = shared.assign(_datetimeFormats.value[locale] || {}, format);
|
|
645
|
+
_context.datetimeFormats = _datetimeFormats.value;
|
|
646
|
+
coreBase.clearDateTimeFormat(_context, locale, format);
|
|
647
|
+
}
|
|
648
|
+
// getNumberFormat
|
|
649
|
+
function getNumberFormat(locale) {
|
|
650
|
+
return _numberFormats.value[locale] || {};
|
|
651
|
+
}
|
|
652
|
+
// setNumberFormat
|
|
653
|
+
function setNumberFormat(locale, format) {
|
|
654
|
+
_numberFormats.value[locale] = format;
|
|
655
|
+
_context.numberFormats = _numberFormats.value;
|
|
656
|
+
coreBase.clearNumberFormat(_context, locale, format);
|
|
657
|
+
}
|
|
658
|
+
// mergeNumberFormat
|
|
659
|
+
function mergeNumberFormat(locale, format) {
|
|
660
|
+
_numberFormats.value[locale] = shared.assign(_numberFormats.value[locale] || {}, format);
|
|
661
|
+
_context.numberFormats = _numberFormats.value;
|
|
662
|
+
coreBase.clearNumberFormat(_context, locale, format);
|
|
663
|
+
}
|
|
664
|
+
// for debug
|
|
665
|
+
composerID++;
|
|
666
|
+
// watch root locale & fallbackLocale
|
|
667
|
+
if (__root && shared.inBrowser) {
|
|
668
|
+
vue.watch(__root.locale, (val) => {
|
|
669
|
+
if (_inheritLocale) {
|
|
670
|
+
_locale.value = val;
|
|
671
|
+
_context.locale = val;
|
|
672
|
+
coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
vue.watch(__root.fallbackLocale, (val) => {
|
|
676
|
+
if (_inheritLocale) {
|
|
677
|
+
_fallbackLocale.value = val;
|
|
678
|
+
_context.fallbackLocale = val;
|
|
679
|
+
coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
// define basic composition API!
|
|
684
|
+
const composer = {
|
|
685
|
+
id: composerID,
|
|
686
|
+
locale,
|
|
687
|
+
fallbackLocale,
|
|
688
|
+
get inheritLocale() {
|
|
689
|
+
return _inheritLocale;
|
|
690
|
+
},
|
|
691
|
+
set inheritLocale(val) {
|
|
692
|
+
_inheritLocale = val;
|
|
693
|
+
if (val && __root) {
|
|
694
|
+
_locale.value = __root.locale.value;
|
|
695
|
+
_fallbackLocale.value = __root.fallbackLocale.value;
|
|
696
|
+
coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
get availableLocales() {
|
|
700
|
+
return Object.keys(_messages.value).sort();
|
|
701
|
+
},
|
|
702
|
+
messages,
|
|
703
|
+
get modifiers() {
|
|
704
|
+
return _modifiers;
|
|
705
|
+
},
|
|
706
|
+
get pluralRules() {
|
|
707
|
+
return _pluralRules || {};
|
|
708
|
+
},
|
|
709
|
+
get isGlobal() {
|
|
710
|
+
return _isGlobal;
|
|
711
|
+
},
|
|
712
|
+
get missingWarn() {
|
|
713
|
+
return _missingWarn;
|
|
714
|
+
},
|
|
715
|
+
set missingWarn(val) {
|
|
716
|
+
_missingWarn = val;
|
|
717
|
+
_context.missingWarn = _missingWarn;
|
|
718
|
+
},
|
|
719
|
+
get fallbackWarn() {
|
|
720
|
+
return _fallbackWarn;
|
|
721
|
+
},
|
|
722
|
+
set fallbackWarn(val) {
|
|
723
|
+
_fallbackWarn = val;
|
|
724
|
+
_context.fallbackWarn = _fallbackWarn;
|
|
725
|
+
},
|
|
726
|
+
get fallbackRoot() {
|
|
727
|
+
return _fallbackRoot;
|
|
728
|
+
},
|
|
729
|
+
set fallbackRoot(val) {
|
|
730
|
+
_fallbackRoot = val;
|
|
731
|
+
},
|
|
732
|
+
get fallbackFormat() {
|
|
733
|
+
return _fallbackFormat;
|
|
734
|
+
},
|
|
735
|
+
set fallbackFormat(val) {
|
|
736
|
+
_fallbackFormat = val;
|
|
737
|
+
_context.fallbackFormat = _fallbackFormat;
|
|
738
|
+
},
|
|
739
|
+
get warnHtmlMessage() {
|
|
740
|
+
return _warnHtmlMessage;
|
|
741
|
+
},
|
|
742
|
+
set warnHtmlMessage(val) {
|
|
743
|
+
_warnHtmlMessage = val;
|
|
744
|
+
_context.warnHtmlMessage = val;
|
|
745
|
+
},
|
|
746
|
+
get escapeParameter() {
|
|
747
|
+
return _escapeParameter;
|
|
748
|
+
},
|
|
749
|
+
set escapeParameter(val) {
|
|
750
|
+
_escapeParameter = val;
|
|
751
|
+
_context.escapeParameter = val;
|
|
752
|
+
},
|
|
753
|
+
t,
|
|
754
|
+
getLocaleMessage,
|
|
755
|
+
setLocaleMessage,
|
|
756
|
+
mergeLocaleMessage,
|
|
757
|
+
getPostTranslationHandler,
|
|
758
|
+
setPostTranslationHandler,
|
|
759
|
+
getMissingHandler,
|
|
760
|
+
setMissingHandler,
|
|
761
|
+
[SetPluralRulesSymbol]: setPluralRules
|
|
762
|
+
};
|
|
763
|
+
{
|
|
764
|
+
composer.datetimeFormats = datetimeFormats;
|
|
765
|
+
composer.numberFormats = numberFormats;
|
|
766
|
+
composer.rt = rt;
|
|
767
|
+
composer.te = te;
|
|
768
|
+
composer.tm = tm;
|
|
769
|
+
composer.d = d;
|
|
770
|
+
composer.n = n;
|
|
771
|
+
composer.getDateTimeFormat = getDateTimeFormat;
|
|
772
|
+
composer.setDateTimeFormat = setDateTimeFormat;
|
|
773
|
+
composer.mergeDateTimeFormat = mergeDateTimeFormat;
|
|
774
|
+
composer.getNumberFormat = getNumberFormat;
|
|
775
|
+
composer.setNumberFormat = setNumberFormat;
|
|
776
|
+
composer.mergeNumberFormat = mergeNumberFormat;
|
|
777
|
+
composer[InejctWithOption] = options.__injectWithOption;
|
|
778
|
+
composer[TransrateVNodeSymbol] = transrateVNode;
|
|
779
|
+
composer[DatetimePartsSymbol] = datetimeParts;
|
|
780
|
+
composer[NumberPartsSymbol] = numberParts;
|
|
781
|
+
}
|
|
782
|
+
// for vue-devtools timeline event
|
|
783
|
+
{
|
|
784
|
+
composer[EnableEmitter] = (emitter) => {
|
|
785
|
+
_context.__v_emitter = emitter;
|
|
786
|
+
};
|
|
787
|
+
composer[DisableEmitter] = () => {
|
|
788
|
+
_context.__v_emitter = undefined;
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
return composer;
|
|
792
|
+
}
|
|
793
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
794
|
+
|
|
795
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
796
|
+
/**
|
|
797
|
+
* Convert to I18n Composer Options from VueI18n Options
|
|
798
|
+
*
|
|
799
|
+
* @internal
|
|
800
|
+
*/
|
|
801
|
+
function convertComposerOptions(options) {
|
|
802
|
+
const locale = shared.isString(options.locale) ? options.locale : coreBase.DEFAULT_LOCALE;
|
|
803
|
+
const fallbackLocale = shared.isString(options.fallbackLocale) ||
|
|
804
|
+
shared.isArray(options.fallbackLocale) ||
|
|
805
|
+
shared.isPlainObject(options.fallbackLocale) ||
|
|
806
|
+
options.fallbackLocale === false
|
|
807
|
+
? options.fallbackLocale
|
|
808
|
+
: locale;
|
|
809
|
+
const missing = shared.isFunction(options.missing) ? options.missing : undefined;
|
|
810
|
+
const missingWarn = shared.isBoolean(options.silentTranslationWarn) ||
|
|
811
|
+
shared.isRegExp(options.silentTranslationWarn)
|
|
812
|
+
? !options.silentTranslationWarn
|
|
813
|
+
: true;
|
|
814
|
+
const fallbackWarn = shared.isBoolean(options.silentFallbackWarn) ||
|
|
815
|
+
shared.isRegExp(options.silentFallbackWarn)
|
|
816
|
+
? !options.silentFallbackWarn
|
|
817
|
+
: true;
|
|
818
|
+
const fallbackRoot = shared.isBoolean(options.fallbackRoot)
|
|
819
|
+
? options.fallbackRoot
|
|
820
|
+
: true;
|
|
821
|
+
const fallbackFormat = !!options.formatFallbackMessages;
|
|
822
|
+
const modifiers = shared.isPlainObject(options.modifiers) ? options.modifiers : {};
|
|
823
|
+
const pluralizationRules = options.pluralizationRules;
|
|
824
|
+
const postTranslation = shared.isFunction(options.postTranslation)
|
|
825
|
+
? options.postTranslation
|
|
826
|
+
: undefined;
|
|
827
|
+
const warnHtmlMessage = shared.isString(options.warnHtmlInMessage)
|
|
828
|
+
? options.warnHtmlInMessage !== 'off'
|
|
829
|
+
: true;
|
|
830
|
+
const escapeParameter = !!options.escapeParameterHtml;
|
|
831
|
+
const inheritLocale = shared.isBoolean(options.sync) ? options.sync : true;
|
|
832
|
+
if (options.formatter) {
|
|
833
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
|
|
834
|
+
}
|
|
835
|
+
if (options.preserveDirectiveContent) {
|
|
836
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
|
|
837
|
+
}
|
|
838
|
+
let messages = options.messages;
|
|
839
|
+
if (shared.isPlainObject(options.sharedMessages)) {
|
|
840
|
+
const sharedMessages = options.sharedMessages;
|
|
841
|
+
const locales = Object.keys(sharedMessages);
|
|
842
|
+
messages = locales.reduce((messages, locale) => {
|
|
843
|
+
const message = messages[locale] || (messages[locale] = {});
|
|
844
|
+
shared.assign(message, sharedMessages[locale]);
|
|
845
|
+
return messages;
|
|
846
|
+
}, (messages || {}));
|
|
847
|
+
}
|
|
848
|
+
const { __i18n, __root, __injectWithOption } = options;
|
|
849
|
+
const datetimeFormats = options.datetimeFormats;
|
|
850
|
+
const numberFormats = options.numberFormats;
|
|
851
|
+
const flatJson = options.flatJson;
|
|
852
|
+
return {
|
|
853
|
+
locale,
|
|
854
|
+
fallbackLocale,
|
|
855
|
+
messages,
|
|
856
|
+
flatJson,
|
|
857
|
+
datetimeFormats,
|
|
858
|
+
numberFormats,
|
|
859
|
+
missing,
|
|
860
|
+
missingWarn,
|
|
861
|
+
fallbackWarn,
|
|
862
|
+
fallbackRoot,
|
|
863
|
+
fallbackFormat,
|
|
864
|
+
modifiers,
|
|
865
|
+
pluralRules: pluralizationRules,
|
|
866
|
+
postTranslation,
|
|
867
|
+
warnHtmlMessage,
|
|
868
|
+
escapeParameter,
|
|
869
|
+
messageResolver: options.messageResolver,
|
|
870
|
+
inheritLocale,
|
|
871
|
+
__i18n,
|
|
872
|
+
__root,
|
|
873
|
+
__injectWithOption
|
|
874
|
+
};
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* create VueI18n interface factory
|
|
878
|
+
*
|
|
879
|
+
* @internal
|
|
880
|
+
*/
|
|
881
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
882
|
+
function createVueI18n(options = {}, VueI18nLegacy) {
|
|
883
|
+
{
|
|
884
|
+
const composer = createComposer(convertComposerOptions(options));
|
|
885
|
+
// defines VueI18n
|
|
886
|
+
const vueI18n = {
|
|
887
|
+
// id
|
|
888
|
+
id: composer.id,
|
|
889
|
+
// locale
|
|
890
|
+
get locale() {
|
|
891
|
+
return composer.locale.value;
|
|
892
|
+
},
|
|
893
|
+
set locale(val) {
|
|
894
|
+
composer.locale.value = val;
|
|
895
|
+
},
|
|
896
|
+
// fallbackLocale
|
|
897
|
+
get fallbackLocale() {
|
|
898
|
+
return composer.fallbackLocale.value;
|
|
899
|
+
},
|
|
900
|
+
set fallbackLocale(val) {
|
|
901
|
+
composer.fallbackLocale.value = val;
|
|
902
|
+
},
|
|
903
|
+
// messages
|
|
904
|
+
get messages() {
|
|
905
|
+
return composer.messages.value;
|
|
906
|
+
},
|
|
907
|
+
// datetimeFormats
|
|
908
|
+
get datetimeFormats() {
|
|
909
|
+
return composer.datetimeFormats.value;
|
|
910
|
+
},
|
|
911
|
+
// numberFormats
|
|
912
|
+
get numberFormats() {
|
|
913
|
+
return composer.numberFormats.value;
|
|
914
|
+
},
|
|
915
|
+
// availableLocales
|
|
916
|
+
get availableLocales() {
|
|
917
|
+
return composer.availableLocales;
|
|
918
|
+
},
|
|
919
|
+
// formatter
|
|
920
|
+
get formatter() {
|
|
921
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
|
|
922
|
+
// dummy
|
|
923
|
+
return {
|
|
924
|
+
interpolate() {
|
|
925
|
+
return [];
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
},
|
|
929
|
+
set formatter(val) {
|
|
930
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_FORMATTER));
|
|
931
|
+
},
|
|
932
|
+
// missing
|
|
933
|
+
get missing() {
|
|
934
|
+
return composer.getMissingHandler();
|
|
935
|
+
},
|
|
936
|
+
set missing(handler) {
|
|
937
|
+
composer.setMissingHandler(handler);
|
|
938
|
+
},
|
|
939
|
+
// silentTranslationWarn
|
|
940
|
+
get silentTranslationWarn() {
|
|
941
|
+
return shared.isBoolean(composer.missingWarn)
|
|
942
|
+
? !composer.missingWarn
|
|
943
|
+
: composer.missingWarn;
|
|
944
|
+
},
|
|
945
|
+
set silentTranslationWarn(val) {
|
|
946
|
+
composer.missingWarn = shared.isBoolean(val) ? !val : val;
|
|
947
|
+
},
|
|
948
|
+
// silentFallbackWarn
|
|
949
|
+
get silentFallbackWarn() {
|
|
950
|
+
return shared.isBoolean(composer.fallbackWarn)
|
|
951
|
+
? !composer.fallbackWarn
|
|
952
|
+
: composer.fallbackWarn;
|
|
953
|
+
},
|
|
954
|
+
set silentFallbackWarn(val) {
|
|
955
|
+
composer.fallbackWarn = shared.isBoolean(val) ? !val : val;
|
|
956
|
+
},
|
|
957
|
+
// modifiers
|
|
958
|
+
get modifiers() {
|
|
959
|
+
return composer.modifiers;
|
|
960
|
+
},
|
|
961
|
+
// formatFallbackMessages
|
|
962
|
+
get formatFallbackMessages() {
|
|
963
|
+
return composer.fallbackFormat;
|
|
964
|
+
},
|
|
965
|
+
set formatFallbackMessages(val) {
|
|
966
|
+
composer.fallbackFormat = val;
|
|
967
|
+
},
|
|
968
|
+
// postTranslation
|
|
969
|
+
get postTranslation() {
|
|
970
|
+
return composer.getPostTranslationHandler();
|
|
971
|
+
},
|
|
972
|
+
set postTranslation(handler) {
|
|
973
|
+
composer.setPostTranslationHandler(handler);
|
|
974
|
+
},
|
|
975
|
+
// sync
|
|
976
|
+
get sync() {
|
|
977
|
+
return composer.inheritLocale;
|
|
978
|
+
},
|
|
979
|
+
set sync(val) {
|
|
980
|
+
composer.inheritLocale = val;
|
|
981
|
+
},
|
|
982
|
+
// warnInHtmlMessage
|
|
983
|
+
get warnHtmlInMessage() {
|
|
984
|
+
return composer.warnHtmlMessage ? 'warn' : 'off';
|
|
985
|
+
},
|
|
986
|
+
set warnHtmlInMessage(val) {
|
|
987
|
+
composer.warnHtmlMessage = val !== 'off';
|
|
988
|
+
},
|
|
989
|
+
// escapeParameterHtml
|
|
990
|
+
get escapeParameterHtml() {
|
|
991
|
+
return composer.escapeParameter;
|
|
992
|
+
},
|
|
993
|
+
set escapeParameterHtml(val) {
|
|
994
|
+
composer.escapeParameter = val;
|
|
995
|
+
},
|
|
996
|
+
// preserveDirectiveContent
|
|
997
|
+
get preserveDirectiveContent() {
|
|
998
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
|
|
999
|
+
return true;
|
|
1000
|
+
},
|
|
1001
|
+
set preserveDirectiveContent(val) {
|
|
1002
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE_DIRECTIVE));
|
|
1003
|
+
},
|
|
1004
|
+
// pluralizationRules
|
|
1005
|
+
get pluralizationRules() {
|
|
1006
|
+
return composer.pluralRules || {};
|
|
1007
|
+
},
|
|
1008
|
+
// for internal
|
|
1009
|
+
__composer: composer,
|
|
1010
|
+
// t
|
|
1011
|
+
t(...args) {
|
|
1012
|
+
const [arg1, arg2, arg3] = args;
|
|
1013
|
+
const options = {};
|
|
1014
|
+
let list = null;
|
|
1015
|
+
let named = null;
|
|
1016
|
+
if (!shared.isString(arg1)) {
|
|
1017
|
+
throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
|
|
1018
|
+
}
|
|
1019
|
+
const key = arg1;
|
|
1020
|
+
if (shared.isString(arg2)) {
|
|
1021
|
+
options.locale = arg2;
|
|
1022
|
+
}
|
|
1023
|
+
else if (shared.isArray(arg2)) {
|
|
1024
|
+
list = arg2;
|
|
1025
|
+
}
|
|
1026
|
+
else if (shared.isPlainObject(arg2)) {
|
|
1027
|
+
named = arg2;
|
|
1028
|
+
}
|
|
1029
|
+
if (shared.isArray(arg3)) {
|
|
1030
|
+
list = arg3;
|
|
1031
|
+
}
|
|
1032
|
+
else if (shared.isPlainObject(arg3)) {
|
|
1033
|
+
named = arg3;
|
|
1034
|
+
}
|
|
1035
|
+
// return composer.t(key, (list || named || {}) as any, options)
|
|
1036
|
+
return Reflect.apply(composer.t, composer, [
|
|
1037
|
+
key,
|
|
1038
|
+
(list || named || {}),
|
|
1039
|
+
options
|
|
1040
|
+
]);
|
|
1041
|
+
},
|
|
1042
|
+
rt(...args) {
|
|
1043
|
+
return Reflect.apply(composer.rt, composer, [...args]);
|
|
1044
|
+
},
|
|
1045
|
+
// tc
|
|
1046
|
+
tc(...args) {
|
|
1047
|
+
const [arg1, arg2, arg3] = args;
|
|
1048
|
+
const options = { plural: 1 };
|
|
1049
|
+
let list = null;
|
|
1050
|
+
let named = null;
|
|
1051
|
+
if (!shared.isString(arg1)) {
|
|
1052
|
+
throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
|
|
1053
|
+
}
|
|
1054
|
+
const key = arg1;
|
|
1055
|
+
if (shared.isString(arg2)) {
|
|
1056
|
+
options.locale = arg2;
|
|
1057
|
+
}
|
|
1058
|
+
else if (shared.isNumber(arg2)) {
|
|
1059
|
+
options.plural = arg2;
|
|
1060
|
+
}
|
|
1061
|
+
else if (shared.isArray(arg2)) {
|
|
1062
|
+
list = arg2;
|
|
1063
|
+
}
|
|
1064
|
+
else if (shared.isPlainObject(arg2)) {
|
|
1065
|
+
named = arg2;
|
|
1066
|
+
}
|
|
1067
|
+
if (shared.isString(arg3)) {
|
|
1068
|
+
options.locale = arg3;
|
|
1069
|
+
}
|
|
1070
|
+
else if (shared.isArray(arg3)) {
|
|
1071
|
+
list = arg3;
|
|
1072
|
+
}
|
|
1073
|
+
else if (shared.isPlainObject(arg3)) {
|
|
1074
|
+
named = arg3;
|
|
1075
|
+
}
|
|
1076
|
+
// return composer.t(key, (list || named || {}) as any, options)
|
|
1077
|
+
return Reflect.apply(composer.t, composer, [
|
|
1078
|
+
key,
|
|
1079
|
+
(list || named || {}),
|
|
1080
|
+
options
|
|
1081
|
+
]);
|
|
1082
|
+
},
|
|
1083
|
+
// te
|
|
1084
|
+
te(key, locale) {
|
|
1085
|
+
return composer.te(key, locale);
|
|
1086
|
+
},
|
|
1087
|
+
// tm
|
|
1088
|
+
tm(key) {
|
|
1089
|
+
return composer.tm(key);
|
|
1090
|
+
},
|
|
1091
|
+
// getLocaleMessage
|
|
1092
|
+
getLocaleMessage(locale) {
|
|
1093
|
+
return composer.getLocaleMessage(locale);
|
|
1094
|
+
},
|
|
1095
|
+
// setLocaleMessage
|
|
1096
|
+
setLocaleMessage(locale, message) {
|
|
1097
|
+
composer.setLocaleMessage(locale, message);
|
|
1098
|
+
},
|
|
1099
|
+
// mergeLocaleMessage
|
|
1100
|
+
mergeLocaleMessage(locale, message) {
|
|
1101
|
+
composer.mergeLocaleMessage(locale, message);
|
|
1102
|
+
},
|
|
1103
|
+
// d
|
|
1104
|
+
d(...args) {
|
|
1105
|
+
return Reflect.apply(composer.d, composer, [...args]);
|
|
1106
|
+
},
|
|
1107
|
+
// getDateTimeFormat
|
|
1108
|
+
getDateTimeFormat(locale) {
|
|
1109
|
+
return composer.getDateTimeFormat(locale);
|
|
1110
|
+
},
|
|
1111
|
+
// setDateTimeFormat
|
|
1112
|
+
setDateTimeFormat(locale, format) {
|
|
1113
|
+
composer.setDateTimeFormat(locale, format);
|
|
1114
|
+
},
|
|
1115
|
+
// mergeDateTimeFormat
|
|
1116
|
+
mergeDateTimeFormat(locale, format) {
|
|
1117
|
+
composer.mergeDateTimeFormat(locale, format);
|
|
1118
|
+
},
|
|
1119
|
+
// n
|
|
1120
|
+
n(...args) {
|
|
1121
|
+
return Reflect.apply(composer.n, composer, [...args]);
|
|
1122
|
+
},
|
|
1123
|
+
// getNumberFormat
|
|
1124
|
+
getNumberFormat(locale) {
|
|
1125
|
+
return composer.getNumberFormat(locale);
|
|
1126
|
+
},
|
|
1127
|
+
// setNumberFormat
|
|
1128
|
+
setNumberFormat(locale, format) {
|
|
1129
|
+
composer.setNumberFormat(locale, format);
|
|
1130
|
+
},
|
|
1131
|
+
// mergeNumberFormat
|
|
1132
|
+
mergeNumberFormat(locale, format) {
|
|
1133
|
+
composer.mergeNumberFormat(locale, format);
|
|
1134
|
+
},
|
|
1135
|
+
// getChoiceIndex
|
|
1136
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
1137
|
+
getChoiceIndex(choice, choicesLength) {
|
|
1138
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_GET_CHOICE_INDEX));
|
|
1139
|
+
return -1;
|
|
1140
|
+
},
|
|
1141
|
+
// for internal
|
|
1142
|
+
__onComponentInstanceCreated(target) {
|
|
1143
|
+
const { componentInstanceCreatedListener } = options;
|
|
1144
|
+
if (componentInstanceCreatedListener) {
|
|
1145
|
+
componentInstanceCreatedListener(target, vueI18n);
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
};
|
|
1149
|
+
// for vue-devtools timeline event
|
|
1150
|
+
{
|
|
1151
|
+
vueI18n.__enableEmitter = (emitter) => {
|
|
1152
|
+
const __composer = composer;
|
|
1153
|
+
__composer[EnableEmitter] && __composer[EnableEmitter](emitter);
|
|
1154
|
+
};
|
|
1155
|
+
vueI18n.__disableEmitter = () => {
|
|
1156
|
+
const __composer = composer;
|
|
1157
|
+
__composer[DisableEmitter] && __composer[DisableEmitter]();
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
return vueI18n;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
1164
|
+
|
|
1165
|
+
const baseFormatProps = {
|
|
1166
|
+
tag: {
|
|
1167
|
+
type: [String, Object]
|
|
1168
|
+
},
|
|
1169
|
+
locale: {
|
|
1170
|
+
type: String
|
|
1171
|
+
},
|
|
1172
|
+
scope: {
|
|
1173
|
+
type: String,
|
|
1174
|
+
// NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
|
|
1175
|
+
validator: (val /* ComponetI18nScope */) => val === 'parent' || val === 'global',
|
|
1176
|
+
default: 'parent' /* ComponetI18nScope */
|
|
1177
|
+
},
|
|
1178
|
+
i18n: {
|
|
1179
|
+
type: Object
|
|
1180
|
+
}
|
|
1181
|
+
};
|
|
1182
|
+
|
|
1183
|
+
function getInterpolateArg(
|
|
1184
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1185
|
+
{ slots }, // SetupContext,
|
|
1186
|
+
keys) {
|
|
1187
|
+
if (keys.length === 1 && keys[0] === 'default') {
|
|
1188
|
+
// default slot with list
|
|
1189
|
+
const ret = slots.default ? slots.default() : [];
|
|
1190
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1191
|
+
return ret.reduce((slot, current) => {
|
|
1192
|
+
return (slot = [
|
|
1193
|
+
...slot,
|
|
1194
|
+
...(shared.isArray(current.children) ? current.children : [current])
|
|
1195
|
+
]);
|
|
1196
|
+
}, []);
|
|
1197
|
+
}
|
|
1198
|
+
else {
|
|
1199
|
+
// named slots
|
|
1200
|
+
return keys.reduce((arg, key) => {
|
|
1201
|
+
const slot = slots[key];
|
|
1202
|
+
if (slot) {
|
|
1203
|
+
arg[key] = slot();
|
|
1204
|
+
}
|
|
1205
|
+
return arg;
|
|
1206
|
+
}, {});
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1210
|
+
function getFragmentableTag(tag) {
|
|
1211
|
+
return vue.Fragment ;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/**
|
|
1215
|
+
* Translation Component
|
|
1216
|
+
*
|
|
1217
|
+
* @remarks
|
|
1218
|
+
* See the following items for property about details
|
|
1219
|
+
*
|
|
1220
|
+
* @VueI18nSee [TranslationProps](component#translationprops)
|
|
1221
|
+
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
|
|
1222
|
+
* @VueI18nSee [Component Interpolation](../guide/advanced/component)
|
|
1223
|
+
*
|
|
1224
|
+
* @example
|
|
1225
|
+
* ```html
|
|
1226
|
+
* <div id="app">
|
|
1227
|
+
* <!-- ... -->
|
|
1228
|
+
* <i18n path="term" tag="label" for="tos">
|
|
1229
|
+
* <a :href="url" target="_blank">{{ $t('tos') }}</a>
|
|
1230
|
+
* </i18n>
|
|
1231
|
+
* <!-- ... -->
|
|
1232
|
+
* </div>
|
|
1233
|
+
* ```
|
|
1234
|
+
* ```js
|
|
1235
|
+
* import { createApp } from 'vue'
|
|
1236
|
+
* import { createI18n } from 'vue-i18n'
|
|
1237
|
+
*
|
|
1238
|
+
* const messages = {
|
|
1239
|
+
* en: {
|
|
1240
|
+
* tos: 'Term of Service',
|
|
1241
|
+
* term: 'I accept xxx {0}.'
|
|
1242
|
+
* },
|
|
1243
|
+
* ja: {
|
|
1244
|
+
* tos: '利用規約',
|
|
1245
|
+
* term: '私は xxx の{0}に同意します。'
|
|
1246
|
+
* }
|
|
1247
|
+
* }
|
|
1248
|
+
*
|
|
1249
|
+
* const i18n = createI18n({
|
|
1250
|
+
* locale: 'en',
|
|
1251
|
+
* messages
|
|
1252
|
+
* })
|
|
1253
|
+
*
|
|
1254
|
+
* const app = createApp({
|
|
1255
|
+
* data: {
|
|
1256
|
+
* url: '/term'
|
|
1257
|
+
* }
|
|
1258
|
+
* }).use(i18n).mount('#app')
|
|
1259
|
+
* ```
|
|
1260
|
+
*
|
|
1261
|
+
* @VueI18nComponent
|
|
1262
|
+
*/
|
|
1263
|
+
const Translation = /* defineComponent */ {
|
|
1264
|
+
/* eslint-disable */
|
|
1265
|
+
name: 'i18n-t',
|
|
1266
|
+
props: shared.assign({
|
|
1267
|
+
keypath: {
|
|
1268
|
+
type: String,
|
|
1269
|
+
required: true
|
|
1270
|
+
},
|
|
1271
|
+
plural: {
|
|
1272
|
+
type: [Number, String],
|
|
1273
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1274
|
+
validator: (val) => shared.isNumber(val) || !isNaN(val)
|
|
1275
|
+
}
|
|
1276
|
+
}, baseFormatProps),
|
|
1277
|
+
/* eslint-enable */
|
|
1278
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1279
|
+
setup(props, context) {
|
|
1280
|
+
const { slots, attrs } = context;
|
|
1281
|
+
// NOTE: avoid https://github.com/microsoft/rushstack/issues/1050
|
|
1282
|
+
const i18n = props.i18n ||
|
|
1283
|
+
useI18n({
|
|
1284
|
+
useScope: props.scope,
|
|
1285
|
+
__useComponent: true
|
|
1286
|
+
});
|
|
1287
|
+
return () => {
|
|
1288
|
+
const keys = Object.keys(slots).filter(key => key !== '_');
|
|
1289
|
+
const options = {};
|
|
1290
|
+
if (props.locale) {
|
|
1291
|
+
options.locale = props.locale;
|
|
1292
|
+
}
|
|
1293
|
+
if (props.plural !== undefined) {
|
|
1294
|
+
options.plural = shared.isString(props.plural) ? +props.plural : props.plural;
|
|
1295
|
+
}
|
|
1296
|
+
const arg = getInterpolateArg(context, keys);
|
|
1297
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1298
|
+
const children = i18n[TransrateVNodeSymbol](props.keypath, arg, options);
|
|
1299
|
+
const assignedAttrs = shared.assign({}, attrs);
|
|
1300
|
+
const tag = shared.isString(props.tag) || shared.isObject(props.tag)
|
|
1301
|
+
? props.tag
|
|
1302
|
+
: getFragmentableTag();
|
|
1303
|
+
return vue.h(tag, assignedAttrs, children);
|
|
1304
|
+
};
|
|
1305
|
+
}
|
|
1306
|
+
};
|
|
1307
|
+
|
|
1308
|
+
function isVNode(target) {
|
|
1309
|
+
return shared.isArray(target) && !shared.isString(target[0]);
|
|
1310
|
+
}
|
|
1311
|
+
function renderFormatter(props, context, slotKeys, partFormatter) {
|
|
1312
|
+
const { slots, attrs } = context;
|
|
1313
|
+
return () => {
|
|
1314
|
+
const options = { part: true };
|
|
1315
|
+
let overrides = {};
|
|
1316
|
+
if (props.locale) {
|
|
1317
|
+
options.locale = props.locale;
|
|
1318
|
+
}
|
|
1319
|
+
if (shared.isString(props.format)) {
|
|
1320
|
+
options.key = props.format;
|
|
1321
|
+
}
|
|
1322
|
+
else if (shared.isObject(props.format)) {
|
|
1323
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1324
|
+
if (shared.isString(props.format.key)) {
|
|
1325
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1326
|
+
options.key = props.format.key;
|
|
1327
|
+
}
|
|
1328
|
+
// Filter out number format options only
|
|
1329
|
+
overrides = Object.keys(props.format).reduce((options, prop) => {
|
|
1330
|
+
return slotKeys.includes(prop)
|
|
1331
|
+
? shared.assign({}, options, { [prop]: props.format[prop] }) // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1332
|
+
: options;
|
|
1333
|
+
}, {});
|
|
1334
|
+
}
|
|
1335
|
+
const parts = partFormatter(...[props.value, options, overrides]);
|
|
1336
|
+
let children = [options.key];
|
|
1337
|
+
if (shared.isArray(parts)) {
|
|
1338
|
+
children = parts.map((part, index) => {
|
|
1339
|
+
const slot = slots[part.type];
|
|
1340
|
+
const node = slot
|
|
1341
|
+
? slot({ [part.type]: part.value, index, parts })
|
|
1342
|
+
: [part.value];
|
|
1343
|
+
if (isVNode(node)) {
|
|
1344
|
+
node[0].key = `${part.type}-${index}`;
|
|
1345
|
+
}
|
|
1346
|
+
return node;
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
else if (shared.isString(parts)) {
|
|
1350
|
+
children = [parts];
|
|
1351
|
+
}
|
|
1352
|
+
const assignedAttrs = shared.assign({}, attrs);
|
|
1353
|
+
const tag = shared.isString(props.tag) || shared.isObject(props.tag)
|
|
1354
|
+
? props.tag
|
|
1355
|
+
: getFragmentableTag();
|
|
1356
|
+
return vue.h(tag, assignedAttrs, children);
|
|
1357
|
+
};
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
/**
|
|
1361
|
+
* Number Format Component
|
|
1362
|
+
*
|
|
1363
|
+
* @remarks
|
|
1364
|
+
* See the following items for property about details
|
|
1365
|
+
*
|
|
1366
|
+
* @VueI18nSee [FormattableProps](component#formattableprops)
|
|
1367
|
+
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
|
|
1368
|
+
* @VueI18nSee [Custom Formatting](../guide/essentials/number#custom-formatting)
|
|
1369
|
+
*
|
|
1370
|
+
* @VueI18nDanger
|
|
1371
|
+
* Not supported IE, due to no support `Intl.NumberFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatToParts)
|
|
1372
|
+
*
|
|
1373
|
+
* If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-numberformat)
|
|
1374
|
+
*
|
|
1375
|
+
* @VueI18nComponent
|
|
1376
|
+
*/
|
|
1377
|
+
const NumberFormat = /* defineComponent */ {
|
|
1378
|
+
/* eslint-disable */
|
|
1379
|
+
name: 'i18n-n',
|
|
1380
|
+
props: shared.assign({
|
|
1381
|
+
value: {
|
|
1382
|
+
type: Number,
|
|
1383
|
+
required: true
|
|
1384
|
+
},
|
|
1385
|
+
format: {
|
|
1386
|
+
type: [String, Object]
|
|
1387
|
+
}
|
|
1388
|
+
}, baseFormatProps),
|
|
1389
|
+
/* eslint-enable */
|
|
1390
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1391
|
+
setup(props, context) {
|
|
1392
|
+
const i18n = props.i18n ||
|
|
1393
|
+
useI18n({ useScope: 'parent', __useComponent: true });
|
|
1394
|
+
return renderFormatter(props, context, coreBase.NUMBER_FORMAT_OPTIONS_KEYS, (...args) =>
|
|
1395
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1396
|
+
i18n[NumberPartsSymbol](...args));
|
|
1397
|
+
}
|
|
1398
|
+
};
|
|
1399
|
+
|
|
1400
|
+
/**
|
|
1401
|
+
* Datetime Format Component
|
|
1402
|
+
*
|
|
1403
|
+
* @remarks
|
|
1404
|
+
* See the following items for property about details
|
|
1405
|
+
*
|
|
1406
|
+
* @VueI18nSee [FormattableProps](component#formattableprops)
|
|
1407
|
+
* @VueI18nSee [BaseFormatProps](component#baseformatprops)
|
|
1408
|
+
* @VueI18nSee [Custom Formatting](../guide/essentials/datetime#custom-formatting)
|
|
1409
|
+
*
|
|
1410
|
+
* @VueI18nDanger
|
|
1411
|
+
* Not supported IE, due to no support `Intl.DateTimeFormat#formatToParts` in [IE](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/formatToParts)
|
|
1412
|
+
*
|
|
1413
|
+
* If you want to use it, you need to use [polyfill](https://github.com/formatjs/formatjs/tree/main/packages/intl-datetimeformat)
|
|
1414
|
+
*
|
|
1415
|
+
* @VueI18nComponent
|
|
1416
|
+
*/
|
|
1417
|
+
const DatetimeFormat = /*defineComponent */ {
|
|
1418
|
+
/* eslint-disable */
|
|
1419
|
+
name: 'i18n-d',
|
|
1420
|
+
props: shared.assign({
|
|
1421
|
+
value: {
|
|
1422
|
+
type: [Number, Date],
|
|
1423
|
+
required: true
|
|
1424
|
+
},
|
|
1425
|
+
format: {
|
|
1426
|
+
type: [String, Object]
|
|
1427
|
+
}
|
|
1428
|
+
}, baseFormatProps),
|
|
1429
|
+
/* eslint-enable */
|
|
1430
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1431
|
+
setup(props, context) {
|
|
1432
|
+
const i18n = props.i18n ||
|
|
1433
|
+
useI18n({ useScope: 'parent', __useComponent: true });
|
|
1434
|
+
return renderFormatter(props, context, coreBase.DATETIME_FORMAT_OPTIONS_KEYS, (...args) =>
|
|
1435
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1436
|
+
i18n[DatetimePartsSymbol](...args));
|
|
1437
|
+
}
|
|
1438
|
+
};
|
|
1439
|
+
|
|
1440
|
+
function getComposer$1(i18n, instance) {
|
|
1441
|
+
const i18nInternal = i18n;
|
|
1442
|
+
if (i18n.mode === 'composition') {
|
|
1443
|
+
return (i18nInternal.__getInstance(instance) || i18n.global);
|
|
1444
|
+
}
|
|
1445
|
+
else {
|
|
1446
|
+
const vueI18n = i18nInternal.__getInstance(instance);
|
|
1447
|
+
return vueI18n != null
|
|
1448
|
+
? vueI18n.__composer
|
|
1449
|
+
: i18n.global.__composer;
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
function vTDirective(i18n) {
|
|
1453
|
+
const _process = (binding) => {
|
|
1454
|
+
const { instance, modifiers, value } = binding;
|
|
1455
|
+
/* istanbul ignore if */
|
|
1456
|
+
if (!instance || !instance.$) {
|
|
1457
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
|
1458
|
+
}
|
|
1459
|
+
const composer = getComposer$1(i18n, instance.$);
|
|
1460
|
+
if (modifiers.preserve) {
|
|
1461
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_SUPPORTED_PRESERVE));
|
|
1462
|
+
}
|
|
1463
|
+
const parsedValue = parseValue(value);
|
|
1464
|
+
return [
|
|
1465
|
+
Reflect.apply(composer.t, composer, [...makeParams(parsedValue)]),
|
|
1466
|
+
composer
|
|
1467
|
+
];
|
|
1468
|
+
};
|
|
1469
|
+
const register = (el, binding) => {
|
|
1470
|
+
const [textContent, composer] = _process(binding);
|
|
1471
|
+
if (shared.inBrowser && i18n.global === composer) {
|
|
1472
|
+
// global scope only
|
|
1473
|
+
el.__i18nWatcher = vue.watch(composer.locale, () => {
|
|
1474
|
+
binding.instance && binding.instance.$forceUpdate();
|
|
1475
|
+
});
|
|
1476
|
+
}
|
|
1477
|
+
el.__composer = composer;
|
|
1478
|
+
el.textContent = textContent;
|
|
1479
|
+
};
|
|
1480
|
+
const unregister = (el) => {
|
|
1481
|
+
if (shared.inBrowser && el.__i18nWatcher) {
|
|
1482
|
+
el.__i18nWatcher();
|
|
1483
|
+
el.__i18nWatcher = undefined;
|
|
1484
|
+
delete el.__i18nWatcher;
|
|
1485
|
+
}
|
|
1486
|
+
if (el.__composer) {
|
|
1487
|
+
el.__composer = undefined;
|
|
1488
|
+
delete el.__composer;
|
|
1489
|
+
}
|
|
1490
|
+
};
|
|
1491
|
+
const update = (el, { value }) => {
|
|
1492
|
+
if (el.__composer) {
|
|
1493
|
+
const composer = el.__composer;
|
|
1494
|
+
const parsedValue = parseValue(value);
|
|
1495
|
+
el.textContent = Reflect.apply(composer.t, composer, [
|
|
1496
|
+
...makeParams(parsedValue)
|
|
1497
|
+
]);
|
|
1498
|
+
}
|
|
1499
|
+
};
|
|
1500
|
+
const getSSRProps = (binding) => {
|
|
1501
|
+
const [textContent] = _process(binding);
|
|
1502
|
+
return { textContent };
|
|
1503
|
+
};
|
|
1504
|
+
return {
|
|
1505
|
+
created: register,
|
|
1506
|
+
unmounted: unregister,
|
|
1507
|
+
beforeUpdate: update,
|
|
1508
|
+
getSSRProps
|
|
1509
|
+
};
|
|
1510
|
+
}
|
|
1511
|
+
function parseValue(value) {
|
|
1512
|
+
if (shared.isString(value)) {
|
|
1513
|
+
return { path: value };
|
|
1514
|
+
}
|
|
1515
|
+
else if (shared.isPlainObject(value)) {
|
|
1516
|
+
if (!('path' in value)) {
|
|
1517
|
+
throw createI18nError(I18nErrorCodes.REQUIRED_VALUE, 'path');
|
|
1518
|
+
}
|
|
1519
|
+
return value;
|
|
1520
|
+
}
|
|
1521
|
+
else {
|
|
1522
|
+
throw createI18nError(I18nErrorCodes.INVALID_VALUE);
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
function makeParams(value) {
|
|
1526
|
+
const { path, locale, args, choice, plural } = value;
|
|
1527
|
+
const options = {};
|
|
1528
|
+
const named = args || {};
|
|
1529
|
+
if (shared.isString(locale)) {
|
|
1530
|
+
options.locale = locale;
|
|
1531
|
+
}
|
|
1532
|
+
if (shared.isNumber(choice)) {
|
|
1533
|
+
options.plural = choice;
|
|
1534
|
+
}
|
|
1535
|
+
if (shared.isNumber(plural)) {
|
|
1536
|
+
options.plural = plural;
|
|
1537
|
+
}
|
|
1538
|
+
return [path, named, options];
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
function apply(app, i18n, ...options) {
|
|
1542
|
+
const pluginOptions = shared.isPlainObject(options[0])
|
|
1543
|
+
? options[0]
|
|
1544
|
+
: {};
|
|
1545
|
+
const useI18nComponentName = !!pluginOptions.useI18nComponentName;
|
|
1546
|
+
const globalInstall = shared.isBoolean(pluginOptions.globalInstall)
|
|
1547
|
+
? pluginOptions.globalInstall
|
|
1548
|
+
: true;
|
|
1549
|
+
if (globalInstall && useI18nComponentName) {
|
|
1550
|
+
shared.warn(getWarnMessage(I18nWarnCodes.COMPONENT_NAME_LEGACY_COMPATIBLE, {
|
|
1551
|
+
name: Translation.name
|
|
1552
|
+
}));
|
|
1553
|
+
}
|
|
1554
|
+
if (globalInstall) {
|
|
1555
|
+
// install components
|
|
1556
|
+
app.component(!useI18nComponentName ? Translation.name : 'i18n', Translation);
|
|
1557
|
+
app.component(NumberFormat.name, NumberFormat);
|
|
1558
|
+
app.component(DatetimeFormat.name, DatetimeFormat);
|
|
1559
|
+
}
|
|
1560
|
+
// install directive
|
|
1561
|
+
{
|
|
1562
|
+
app.directive('t', vTDirective(i18n));
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
/**
|
|
1567
|
+
* Supports compatibility for legacy vue-i18n APIs
|
|
1568
|
+
* This mixin is used when we use vue-i18n@v9.x or later
|
|
1569
|
+
*/
|
|
1570
|
+
function defineMixin(vuei18n, composer, i18n) {
|
|
1571
|
+
return {
|
|
1572
|
+
beforeCreate() {
|
|
1573
|
+
const instance = vue.getCurrentInstance();
|
|
1574
|
+
/* istanbul ignore if */
|
|
1575
|
+
if (!instance) {
|
|
1576
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
|
1577
|
+
}
|
|
1578
|
+
const options = this.$options;
|
|
1579
|
+
if (options.i18n) {
|
|
1580
|
+
const optionsI18n = options.i18n;
|
|
1581
|
+
if (options.__i18n) {
|
|
1582
|
+
optionsI18n.__i18n = options.__i18n;
|
|
1583
|
+
}
|
|
1584
|
+
optionsI18n.__root = composer;
|
|
1585
|
+
if (this === this.$root) {
|
|
1586
|
+
this.$i18n = mergeToRoot(vuei18n, optionsI18n);
|
|
1587
|
+
}
|
|
1588
|
+
else {
|
|
1589
|
+
optionsI18n.__injectWithOption = true;
|
|
1590
|
+
this.$i18n = createVueI18n(optionsI18n);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
else if (options.__i18n) {
|
|
1594
|
+
if (this === this.$root) {
|
|
1595
|
+
this.$i18n = mergeToRoot(vuei18n, options);
|
|
1596
|
+
}
|
|
1597
|
+
else {
|
|
1598
|
+
this.$i18n = createVueI18n({
|
|
1599
|
+
__i18n: options.__i18n,
|
|
1600
|
+
__injectWithOption: true,
|
|
1601
|
+
__root: composer
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
else {
|
|
1606
|
+
// set global
|
|
1607
|
+
this.$i18n = vuei18n;
|
|
1608
|
+
}
|
|
1609
|
+
if (options.__i18nGlobal) {
|
|
1610
|
+
adjustI18nResources(composer, options, options);
|
|
1611
|
+
}
|
|
1612
|
+
vuei18n.__onComponentInstanceCreated(this.$i18n);
|
|
1613
|
+
i18n.__setInstance(instance, this.$i18n);
|
|
1614
|
+
// defines vue-i18n legacy APIs
|
|
1615
|
+
this.$t = (...args) => this.$i18n.t(...args);
|
|
1616
|
+
this.$rt = (...args) => this.$i18n.rt(...args);
|
|
1617
|
+
this.$tc = (...args) => this.$i18n.tc(...args);
|
|
1618
|
+
this.$te = (key, locale) => this.$i18n.te(key, locale);
|
|
1619
|
+
this.$d = (...args) => this.$i18n.d(...args);
|
|
1620
|
+
this.$n = (...args) => this.$i18n.n(...args);
|
|
1621
|
+
this.$tm = (key) => this.$i18n.tm(key);
|
|
1622
|
+
},
|
|
1623
|
+
mounted() {
|
|
1624
|
+
},
|
|
1625
|
+
unmounted() {
|
|
1626
|
+
const instance = vue.getCurrentInstance();
|
|
1627
|
+
/* istanbul ignore if */
|
|
1628
|
+
if (!instance) {
|
|
1629
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
|
1630
|
+
}
|
|
1631
|
+
delete this.$t;
|
|
1632
|
+
delete this.$rt;
|
|
1633
|
+
delete this.$tc;
|
|
1634
|
+
delete this.$te;
|
|
1635
|
+
delete this.$d;
|
|
1636
|
+
delete this.$n;
|
|
1637
|
+
delete this.$tm;
|
|
1638
|
+
i18n.__deleteInstance(instance);
|
|
1639
|
+
delete this.$i18n;
|
|
1640
|
+
}
|
|
1641
|
+
};
|
|
1642
|
+
}
|
|
1643
|
+
function mergeToRoot(root, options) {
|
|
1644
|
+
root.locale = options.locale || root.locale;
|
|
1645
|
+
root.fallbackLocale = options.fallbackLocale || root.fallbackLocale;
|
|
1646
|
+
root.missing = options.missing || root.missing;
|
|
1647
|
+
root.silentTranslationWarn =
|
|
1648
|
+
options.silentTranslationWarn || root.silentFallbackWarn;
|
|
1649
|
+
root.silentFallbackWarn =
|
|
1650
|
+
options.silentFallbackWarn || root.silentFallbackWarn;
|
|
1651
|
+
root.formatFallbackMessages =
|
|
1652
|
+
options.formatFallbackMessages || root.formatFallbackMessages;
|
|
1653
|
+
root.postTranslation = options.postTranslation || root.postTranslation;
|
|
1654
|
+
root.warnHtmlInMessage = options.warnHtmlInMessage || root.warnHtmlInMessage;
|
|
1655
|
+
root.escapeParameterHtml =
|
|
1656
|
+
options.escapeParameterHtml || root.escapeParameterHtml;
|
|
1657
|
+
root.sync = options.sync || root.sync;
|
|
1658
|
+
root.__composer[SetPluralRulesSymbol](options.pluralizationRules || root.pluralizationRules);
|
|
1659
|
+
const messages = getLocaleMessages(root.locale, {
|
|
1660
|
+
messages: options.messages,
|
|
1661
|
+
__i18n: options.__i18n
|
|
1662
|
+
});
|
|
1663
|
+
Object.keys(messages).forEach(locale => root.mergeLocaleMessage(locale, messages[locale]));
|
|
1664
|
+
if (options.datetimeFormats) {
|
|
1665
|
+
Object.keys(options.datetimeFormats).forEach(locale => root.mergeDateTimeFormat(locale, options.datetimeFormats[locale]));
|
|
1666
|
+
}
|
|
1667
|
+
if (options.numberFormats) {
|
|
1668
|
+
Object.keys(options.numberFormats).forEach(locale => root.mergeNumberFormat(locale, options.numberFormats[locale]));
|
|
1669
|
+
}
|
|
1670
|
+
return root;
|
|
1671
|
+
}
|
|
1672
|
+
|
|
1673
|
+
/**
|
|
1674
|
+
* Injection key for {@link useI18n}
|
|
1675
|
+
*
|
|
1676
|
+
* @remarks
|
|
1677
|
+
* The global injection key for I18n instances with `useI18n`. this injection key is used in Web Components.
|
|
1678
|
+
* Specify the i18n instance created by {@link createI18n} together with `provide` function.
|
|
1679
|
+
*
|
|
1680
|
+
* @VueI18nGeneral
|
|
1681
|
+
*/
|
|
1682
|
+
const I18nInjectionKey =
|
|
1683
|
+
/* #__PURE__*/ shared.makeSymbol('global-vue-i18n');
|
|
1684
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
|
1685
|
+
function createI18n(options = {}, VueI18nLegacy) {
|
|
1686
|
+
// prettier-ignore
|
|
1687
|
+
const __legacyMode = shared.isBoolean(options.legacy)
|
|
1688
|
+
? options.legacy
|
|
1689
|
+
: true;
|
|
1690
|
+
// prettier-ignore
|
|
1691
|
+
const __globalInjection = shared.isBoolean(options.globalInjection)
|
|
1692
|
+
? options.globalInjection
|
|
1693
|
+
: true;
|
|
1694
|
+
// prettier-ignore
|
|
1695
|
+
const __allowComposition = __legacyMode
|
|
1696
|
+
? !!options.allowComposition
|
|
1697
|
+
: true;
|
|
1698
|
+
const __instances = new Map();
|
|
1699
|
+
const [globalScope, __global] = createGlobal(options, __legacyMode);
|
|
1700
|
+
const symbol = shared.makeSymbol('vue-i18n' );
|
|
1701
|
+
function __getInstance(component) {
|
|
1702
|
+
return __instances.get(component) || null;
|
|
1703
|
+
}
|
|
1704
|
+
function __setInstance(component, instance) {
|
|
1705
|
+
__instances.set(component, instance);
|
|
1706
|
+
}
|
|
1707
|
+
function __deleteInstance(component) {
|
|
1708
|
+
__instances.delete(component);
|
|
1709
|
+
}
|
|
1710
|
+
{
|
|
1711
|
+
const i18n = {
|
|
1712
|
+
// mode
|
|
1713
|
+
get mode() {
|
|
1714
|
+
return __legacyMode
|
|
1715
|
+
? 'legacy'
|
|
1716
|
+
: 'composition';
|
|
1717
|
+
},
|
|
1718
|
+
// allowComposition
|
|
1719
|
+
get allowComposition() {
|
|
1720
|
+
return __allowComposition;
|
|
1721
|
+
},
|
|
1722
|
+
// install plugin
|
|
1723
|
+
async install(app, ...options) {
|
|
1724
|
+
// setup global provider
|
|
1725
|
+
app.__VUE_I18N_SYMBOL__ = symbol;
|
|
1726
|
+
app.provide(app.__VUE_I18N_SYMBOL__, i18n);
|
|
1727
|
+
// global method and properties injection for Composition API
|
|
1728
|
+
if (!__legacyMode && __globalInjection) {
|
|
1729
|
+
injectGlobalFields(app, i18n.global);
|
|
1730
|
+
}
|
|
1731
|
+
// install built-in components and directive
|
|
1732
|
+
{
|
|
1733
|
+
apply(app, i18n, ...options);
|
|
1734
|
+
}
|
|
1735
|
+
// setup mixin for Legacy API
|
|
1736
|
+
if (__legacyMode) {
|
|
1737
|
+
app.mixin(defineMixin(__global, __global.__composer, i18n));
|
|
1738
|
+
}
|
|
1739
|
+
// release global scope
|
|
1740
|
+
const unmountApp = app.unmount;
|
|
1741
|
+
app.unmount = () => {
|
|
1742
|
+
i18n.dispose();
|
|
1743
|
+
unmountApp();
|
|
1744
|
+
};
|
|
1745
|
+
},
|
|
1746
|
+
// global accessor
|
|
1747
|
+
get global() {
|
|
1748
|
+
return __global;
|
|
1749
|
+
},
|
|
1750
|
+
dispose() {
|
|
1751
|
+
globalScope.stop();
|
|
1752
|
+
},
|
|
1753
|
+
// @internal
|
|
1754
|
+
__instances,
|
|
1755
|
+
// @internal
|
|
1756
|
+
__getInstance,
|
|
1757
|
+
// @internal
|
|
1758
|
+
__setInstance,
|
|
1759
|
+
// @internal
|
|
1760
|
+
__deleteInstance
|
|
1761
|
+
};
|
|
1762
|
+
return i18n;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
1766
|
+
function useI18n(options = {}) {
|
|
1767
|
+
const instance = vue.getCurrentInstance();
|
|
1768
|
+
if (instance == null) {
|
|
1769
|
+
throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
|
|
1770
|
+
}
|
|
1771
|
+
if (!instance.isCE &&
|
|
1772
|
+
instance.appContext.app != null &&
|
|
1773
|
+
!instance.appContext.app.__VUE_I18N_SYMBOL__) {
|
|
1774
|
+
throw createI18nError(I18nErrorCodes.NOT_INSLALLED);
|
|
1775
|
+
}
|
|
1776
|
+
const i18n = getI18nInstance(instance);
|
|
1777
|
+
const global = getGlobalComposer(i18n);
|
|
1778
|
+
const componentOptions = getComponentOptions(instance);
|
|
1779
|
+
const scope = getScope(options, componentOptions);
|
|
1780
|
+
{
|
|
1781
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1782
|
+
if (i18n.mode === 'legacy' && !options.__useComponent) {
|
|
1783
|
+
if (!i18n.allowComposition) {
|
|
1784
|
+
throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE);
|
|
1785
|
+
}
|
|
1786
|
+
return useI18nForLegacy(instance, scope, global, options);
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
if (scope === 'global') {
|
|
1790
|
+
adjustI18nResources(global, options, componentOptions);
|
|
1791
|
+
return global;
|
|
1792
|
+
}
|
|
1793
|
+
if (scope === 'parent') {
|
|
1794
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1795
|
+
let composer = getComposer(i18n, instance, options.__useComponent);
|
|
1796
|
+
if (composer == null) {
|
|
1797
|
+
{
|
|
1798
|
+
shared.warn(getWarnMessage(I18nWarnCodes.NOT_FOUND_PARENT_SCOPE));
|
|
1799
|
+
}
|
|
1800
|
+
composer = global;
|
|
1801
|
+
}
|
|
1802
|
+
return composer;
|
|
1803
|
+
}
|
|
1804
|
+
const i18nInternal = i18n;
|
|
1805
|
+
let composer = i18nInternal.__getInstance(instance);
|
|
1806
|
+
if (composer == null) {
|
|
1807
|
+
const composerOptions = shared.assign({}, options);
|
|
1808
|
+
if ('__i18n' in componentOptions) {
|
|
1809
|
+
composerOptions.__i18n = componentOptions.__i18n;
|
|
1810
|
+
}
|
|
1811
|
+
if (global) {
|
|
1812
|
+
composerOptions.__root = global;
|
|
1813
|
+
}
|
|
1814
|
+
composer = createComposer(composerOptions);
|
|
1815
|
+
setupLifeCycle(i18nInternal, instance);
|
|
1816
|
+
i18nInternal.__setInstance(instance, composer);
|
|
1817
|
+
}
|
|
1818
|
+
return composer;
|
|
1819
|
+
}
|
|
1820
|
+
/**
|
|
1821
|
+
* Cast to VueI18n legacy compatible type
|
|
1822
|
+
*
|
|
1823
|
+
* @remarks
|
|
1824
|
+
* This API is provided only with [vue-i18n-bridge](https://vue-i18n.intlify.dev/guide/migration/ways.html#what-is-vue-i18n-bridge).
|
|
1825
|
+
*
|
|
1826
|
+
* The purpose of this function is to convert an {@link I18n} instance created with {@link createI18n | createI18n(legacy: true)} into a `vue-i18n@v8.x` compatible instance of `new VueI18n` in a TypeScript environment.
|
|
1827
|
+
*
|
|
1828
|
+
* @param i18n - An instance of {@link I18n}
|
|
1829
|
+
* @returns A i18n instance which is casted to {@link VueI18n} type
|
|
1830
|
+
*
|
|
1831
|
+
* @VueI18nTip
|
|
1832
|
+
* :new: provided by **vue-i18n-bridge only**
|
|
1833
|
+
*
|
|
1834
|
+
* @VueI18nGeneral
|
|
1835
|
+
*/
|
|
1836
|
+
const castToVueI18n = (i18n
|
|
1837
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1838
|
+
) => {
|
|
1839
|
+
if (!(__VUE_I18N_BRIDGE__ in i18n)) {
|
|
1840
|
+
throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N);
|
|
1841
|
+
}
|
|
1842
|
+
return i18n;
|
|
1843
|
+
};
|
|
1844
|
+
function createGlobal(options, legacyMode, VueI18nLegacy // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1845
|
+
) {
|
|
1846
|
+
const scope = vue.effectScope();
|
|
1847
|
+
{
|
|
1848
|
+
const obj = legacyMode
|
|
1849
|
+
? scope.run(() => createVueI18n(options))
|
|
1850
|
+
: scope.run(() => createComposer(options));
|
|
1851
|
+
if (obj == null) {
|
|
1852
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
|
1853
|
+
}
|
|
1854
|
+
return [scope, obj];
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
function getI18nInstance(instance) {
|
|
1858
|
+
{
|
|
1859
|
+
const i18n = vue.inject(!instance.isCE
|
|
1860
|
+
? instance.appContext.app.__VUE_I18N_SYMBOL__
|
|
1861
|
+
: I18nInjectionKey);
|
|
1862
|
+
/* istanbul ignore if */
|
|
1863
|
+
if (!i18n) {
|
|
1864
|
+
throw createI18nError(!instance.isCE
|
|
1865
|
+
? I18nErrorCodes.UNEXPECTED_ERROR
|
|
1866
|
+
: I18nErrorCodes.NOT_INSLALLED_WITH_PROVIDE);
|
|
1867
|
+
}
|
|
1868
|
+
return i18n;
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1872
|
+
function getScope(options, componentOptions) {
|
|
1873
|
+
// prettier-ignore
|
|
1874
|
+
return shared.isEmptyObject(options)
|
|
1875
|
+
? ('__i18n' in componentOptions)
|
|
1876
|
+
? 'local'
|
|
1877
|
+
: 'global'
|
|
1878
|
+
: !options.useScope
|
|
1879
|
+
? 'local'
|
|
1880
|
+
: options.useScope;
|
|
1881
|
+
}
|
|
1882
|
+
function getGlobalComposer(i18n) {
|
|
1883
|
+
// prettier-ignore
|
|
1884
|
+
return i18n.mode === 'composition'
|
|
1885
|
+
? i18n.global
|
|
1886
|
+
: i18n.global.__composer
|
|
1887
|
+
;
|
|
1888
|
+
}
|
|
1889
|
+
function getComposer(i18n, target, useComponent = false) {
|
|
1890
|
+
let composer = null;
|
|
1891
|
+
const root = target.root;
|
|
1892
|
+
let current = target.parent;
|
|
1893
|
+
while (current != null) {
|
|
1894
|
+
const i18nInternal = i18n;
|
|
1895
|
+
if (i18n.mode === 'composition') {
|
|
1896
|
+
composer = i18nInternal.__getInstance(current);
|
|
1897
|
+
}
|
|
1898
|
+
else {
|
|
1899
|
+
{
|
|
1900
|
+
const vueI18n = i18nInternal.__getInstance(current);
|
|
1901
|
+
if (vueI18n != null) {
|
|
1902
|
+
composer = vueI18n
|
|
1903
|
+
.__composer;
|
|
1904
|
+
if (useComponent &&
|
|
1905
|
+
composer &&
|
|
1906
|
+
!composer[InejctWithOption] // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1907
|
+
) {
|
|
1908
|
+
composer = null;
|
|
1909
|
+
}
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
if (composer != null) {
|
|
1914
|
+
break;
|
|
1915
|
+
}
|
|
1916
|
+
if (root === current) {
|
|
1917
|
+
break;
|
|
1918
|
+
}
|
|
1919
|
+
current = current.parent;
|
|
1920
|
+
}
|
|
1921
|
+
return composer;
|
|
1922
|
+
}
|
|
1923
|
+
function setupLifeCycle(i18n, target, composer) {
|
|
1924
|
+
{
|
|
1925
|
+
vue.onMounted(() => {
|
|
1926
|
+
}, target);
|
|
1927
|
+
vue.onUnmounted(() => {
|
|
1928
|
+
i18n.__deleteInstance(target);
|
|
1929
|
+
}, target);
|
|
1930
|
+
}
|
|
1931
|
+
}
|
|
1932
|
+
function useI18nForLegacy(instance, scope, root, options = {} // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1933
|
+
) {
|
|
1934
|
+
const isLocale = scope === 'local';
|
|
1935
|
+
const _composer = vue.shallowRef(null);
|
|
1936
|
+
if (isLocale &&
|
|
1937
|
+
instance.proxy &&
|
|
1938
|
+
!(instance.proxy.$options.i18n || instance.proxy.$options.__i18n)) {
|
|
1939
|
+
throw createI18nError(I18nErrorCodes.MUST_DEFINE_I18N_OPTION_IN_ALLOW_COMPOSITION);
|
|
1940
|
+
}
|
|
1941
|
+
const _inheritLocale = shared.isBoolean(options.inheritLocale)
|
|
1942
|
+
? options.inheritLocale
|
|
1943
|
+
: true;
|
|
1944
|
+
const _locale = vue.ref(
|
|
1945
|
+
// prettier-ignore
|
|
1946
|
+
isLocale && _inheritLocale
|
|
1947
|
+
? root.locale.value
|
|
1948
|
+
: shared.isString(options.locale)
|
|
1949
|
+
? options.locale
|
|
1950
|
+
: coreBase.DEFAULT_LOCALE);
|
|
1951
|
+
const _fallbackLocale = vue.ref(
|
|
1952
|
+
// prettier-ignore
|
|
1953
|
+
isLocale && _inheritLocale
|
|
1954
|
+
? root.fallbackLocale.value
|
|
1955
|
+
: shared.isString(options.fallbackLocale) ||
|
|
1956
|
+
shared.isArray(options.fallbackLocale) ||
|
|
1957
|
+
shared.isPlainObject(options.fallbackLocale) ||
|
|
1958
|
+
options.fallbackLocale === false
|
|
1959
|
+
? options.fallbackLocale
|
|
1960
|
+
: _locale.value);
|
|
1961
|
+
const _messages = vue.ref(getLocaleMessages(_locale.value, options));
|
|
1962
|
+
// prettier-ignore
|
|
1963
|
+
const _datetimeFormats = vue.ref(shared.isPlainObject(options.datetimeFormats)
|
|
1964
|
+
? options.datetimeFormats
|
|
1965
|
+
: { [_locale.value]: {} });
|
|
1966
|
+
// prettier-ignore
|
|
1967
|
+
const _numberFormats = vue.ref(shared.isPlainObject(options.numberFormats)
|
|
1968
|
+
? options.numberFormats
|
|
1969
|
+
: { [_locale.value]: {} });
|
|
1970
|
+
// prettier-ignore
|
|
1971
|
+
const _missingWarn = isLocale
|
|
1972
|
+
? root.missingWarn
|
|
1973
|
+
: shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
|
|
1974
|
+
? options.missingWarn
|
|
1975
|
+
: true;
|
|
1976
|
+
// prettier-ignore
|
|
1977
|
+
const _fallbackWarn = isLocale
|
|
1978
|
+
? root.fallbackWarn
|
|
1979
|
+
: shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
|
|
1980
|
+
? options.fallbackWarn
|
|
1981
|
+
: true;
|
|
1982
|
+
// prettier-ignore
|
|
1983
|
+
const _fallbackRoot = isLocale
|
|
1984
|
+
? root.fallbackRoot
|
|
1985
|
+
: shared.isBoolean(options.fallbackRoot)
|
|
1986
|
+
? options.fallbackRoot
|
|
1987
|
+
: true;
|
|
1988
|
+
// configure fall back to root
|
|
1989
|
+
const _fallbackFormat = !!options.fallbackFormat;
|
|
1990
|
+
// runtime missing
|
|
1991
|
+
const _missing = shared.isFunction(options.missing) ? options.missing : null;
|
|
1992
|
+
// postTranslation handler
|
|
1993
|
+
const _postTranslation = shared.isFunction(options.postTranslation)
|
|
1994
|
+
? options.postTranslation
|
|
1995
|
+
: null;
|
|
1996
|
+
// prettier-ignore
|
|
1997
|
+
const _warnHtmlMessage = isLocale
|
|
1998
|
+
? root.warnHtmlMessage
|
|
1999
|
+
: shared.isBoolean(options.warnHtmlMessage)
|
|
2000
|
+
? options.warnHtmlMessage
|
|
2001
|
+
: true;
|
|
2002
|
+
const _escapeParameter = !!options.escapeParameter;
|
|
2003
|
+
// prettier-ignore
|
|
2004
|
+
const _modifiers = isLocale
|
|
2005
|
+
? root.modifiers
|
|
2006
|
+
: shared.isPlainObject(options.modifiers)
|
|
2007
|
+
? options.modifiers
|
|
2008
|
+
: {};
|
|
2009
|
+
// pluralRules
|
|
2010
|
+
const _pluralRules = options.pluralRules || (isLocale && root.pluralRules);
|
|
2011
|
+
// track reactivity
|
|
2012
|
+
function trackReactivityValues() {
|
|
2013
|
+
return [
|
|
2014
|
+
_locale.value,
|
|
2015
|
+
_fallbackLocale.value,
|
|
2016
|
+
_messages.value,
|
|
2017
|
+
_datetimeFormats.value,
|
|
2018
|
+
_numberFormats.value
|
|
2019
|
+
];
|
|
2020
|
+
}
|
|
2021
|
+
// locale
|
|
2022
|
+
const locale = vue.computed({
|
|
2023
|
+
get: () => {
|
|
2024
|
+
return _composer.value ? _composer.value.locale.value : _locale.value;
|
|
2025
|
+
},
|
|
2026
|
+
set: val => {
|
|
2027
|
+
if (_composer.value) {
|
|
2028
|
+
_composer.value.locale.value = val;
|
|
2029
|
+
}
|
|
2030
|
+
_locale.value = val;
|
|
2031
|
+
}
|
|
2032
|
+
});
|
|
2033
|
+
// fallbackLocale
|
|
2034
|
+
const fallbackLocale = vue.computed({
|
|
2035
|
+
get: () => {
|
|
2036
|
+
return _composer.value
|
|
2037
|
+
? _composer.value.fallbackLocale.value
|
|
2038
|
+
: _fallbackLocale.value;
|
|
2039
|
+
},
|
|
2040
|
+
set: val => {
|
|
2041
|
+
if (_composer.value) {
|
|
2042
|
+
_composer.value.fallbackLocale.value = val;
|
|
2043
|
+
}
|
|
2044
|
+
_fallbackLocale.value = val;
|
|
2045
|
+
}
|
|
2046
|
+
});
|
|
2047
|
+
// messages
|
|
2048
|
+
const messages = vue.computed(() => {
|
|
2049
|
+
if (_composer.value) {
|
|
2050
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2051
|
+
return _composer.value.messages.value;
|
|
2052
|
+
}
|
|
2053
|
+
else {
|
|
2054
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2055
|
+
return _messages.value;
|
|
2056
|
+
}
|
|
2057
|
+
});
|
|
2058
|
+
const datetimeFormats = vue.computed(() => _datetimeFormats.value);
|
|
2059
|
+
const numberFormats = vue.computed(() => _numberFormats.value);
|
|
2060
|
+
function getPostTranslationHandler() {
|
|
2061
|
+
return _composer.value
|
|
2062
|
+
? _composer.value.getPostTranslationHandler()
|
|
2063
|
+
: _postTranslation;
|
|
2064
|
+
}
|
|
2065
|
+
function setPostTranslationHandler(handler) {
|
|
2066
|
+
if (_composer.value) {
|
|
2067
|
+
_composer.value.setPostTranslationHandler(handler);
|
|
2068
|
+
}
|
|
2069
|
+
}
|
|
2070
|
+
function getMissingHandler() {
|
|
2071
|
+
return _composer.value ? _composer.value.getMissingHandler() : _missing;
|
|
2072
|
+
}
|
|
2073
|
+
function setMissingHandler(handler) {
|
|
2074
|
+
if (_composer.value) {
|
|
2075
|
+
_composer.value.setMissingHandler(handler);
|
|
2076
|
+
}
|
|
2077
|
+
}
|
|
2078
|
+
function warpWithDeps(fn) {
|
|
2079
|
+
trackReactivityValues();
|
|
2080
|
+
return fn();
|
|
2081
|
+
}
|
|
2082
|
+
function t(...args) {
|
|
2083
|
+
return _composer.value
|
|
2084
|
+
? warpWithDeps(() => Reflect.apply(_composer.value.t, null, [...args]))
|
|
2085
|
+
: warpWithDeps(() => '');
|
|
2086
|
+
}
|
|
2087
|
+
function rt(...args) {
|
|
2088
|
+
return _composer.value
|
|
2089
|
+
? Reflect.apply(_composer.value.rt, null, [...args])
|
|
2090
|
+
: '';
|
|
2091
|
+
}
|
|
2092
|
+
function d(...args) {
|
|
2093
|
+
return _composer.value
|
|
2094
|
+
? warpWithDeps(() => Reflect.apply(_composer.value.d, null, [...args]))
|
|
2095
|
+
: warpWithDeps(() => '');
|
|
2096
|
+
}
|
|
2097
|
+
function n(...args) {
|
|
2098
|
+
return _composer.value
|
|
2099
|
+
? warpWithDeps(() => Reflect.apply(_composer.value.n, null, [...args]))
|
|
2100
|
+
: warpWithDeps(() => '');
|
|
2101
|
+
}
|
|
2102
|
+
function tm(key) {
|
|
2103
|
+
return _composer.value ? _composer.value.tm(key) : {};
|
|
2104
|
+
}
|
|
2105
|
+
function te(key, locale) {
|
|
2106
|
+
return _composer.value ? _composer.value.te(key, locale) : false;
|
|
2107
|
+
}
|
|
2108
|
+
function getLocaleMessage(locale) {
|
|
2109
|
+
return _composer.value ? _composer.value.getLocaleMessage(locale) : {};
|
|
2110
|
+
}
|
|
2111
|
+
function setLocaleMessage(locale, message) {
|
|
2112
|
+
if (_composer.value) {
|
|
2113
|
+
_composer.value.setLocaleMessage(locale, message);
|
|
2114
|
+
_messages.value[locale] = message;
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
function mergeLocaleMessage(locale, message) {
|
|
2118
|
+
if (_composer.value) {
|
|
2119
|
+
_composer.value.mergeLocaleMessage(locale, message);
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
function getDateTimeFormat(locale) {
|
|
2123
|
+
return _composer.value ? _composer.value.getDateTimeFormat(locale) : {};
|
|
2124
|
+
}
|
|
2125
|
+
function setDateTimeFormat(locale, format) {
|
|
2126
|
+
if (_composer.value) {
|
|
2127
|
+
_composer.value.setDateTimeFormat(locale, format);
|
|
2128
|
+
_datetimeFormats.value[locale] = format;
|
|
2129
|
+
}
|
|
2130
|
+
}
|
|
2131
|
+
function mergeDateTimeFormat(locale, format) {
|
|
2132
|
+
if (_composer.value) {
|
|
2133
|
+
_composer.value.mergeDateTimeFormat(locale, format);
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
function getNumberFormat(locale) {
|
|
2137
|
+
return _composer.value ? _composer.value.getNumberFormat(locale) : {};
|
|
2138
|
+
}
|
|
2139
|
+
function setNumberFormat(locale, format) {
|
|
2140
|
+
if (_composer.value) {
|
|
2141
|
+
_composer.value.setNumberFormat(locale, format);
|
|
2142
|
+
_numberFormats.value[locale] = format;
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
function mergeNumberFormat(locale, format) {
|
|
2146
|
+
if (_composer.value) {
|
|
2147
|
+
_composer.value.mergeNumberFormat(locale, format);
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
const wrapper = {
|
|
2151
|
+
get id() {
|
|
2152
|
+
return _composer.value ? _composer.value.id : -1;
|
|
2153
|
+
},
|
|
2154
|
+
locale,
|
|
2155
|
+
fallbackLocale,
|
|
2156
|
+
messages,
|
|
2157
|
+
datetimeFormats,
|
|
2158
|
+
numberFormats,
|
|
2159
|
+
get inheritLocale() {
|
|
2160
|
+
return _composer.value ? _composer.value.inheritLocale : _inheritLocale;
|
|
2161
|
+
},
|
|
2162
|
+
set inheritLocale(val) {
|
|
2163
|
+
if (_composer.value) {
|
|
2164
|
+
_composer.value.inheritLocale = val;
|
|
2165
|
+
}
|
|
2166
|
+
},
|
|
2167
|
+
get availableLocales() {
|
|
2168
|
+
return _composer.value
|
|
2169
|
+
? _composer.value.availableLocales
|
|
2170
|
+
: Object.keys(_messages.value);
|
|
2171
|
+
},
|
|
2172
|
+
get modifiers() {
|
|
2173
|
+
return (_composer.value ? _composer.value.modifiers : _modifiers);
|
|
2174
|
+
},
|
|
2175
|
+
get pluralRules() {
|
|
2176
|
+
return (_composer.value ? _composer.value.pluralRules : _pluralRules);
|
|
2177
|
+
},
|
|
2178
|
+
get isGlobal() {
|
|
2179
|
+
return _composer.value ? _composer.value.isGlobal : false;
|
|
2180
|
+
},
|
|
2181
|
+
get missingWarn() {
|
|
2182
|
+
return _composer.value ? _composer.value.missingWarn : _missingWarn;
|
|
2183
|
+
},
|
|
2184
|
+
set missingWarn(val) {
|
|
2185
|
+
if (_composer.value) {
|
|
2186
|
+
_composer.value.missingWarn = val;
|
|
2187
|
+
}
|
|
2188
|
+
},
|
|
2189
|
+
get fallbackWarn() {
|
|
2190
|
+
return _composer.value ? _composer.value.fallbackWarn : _fallbackWarn;
|
|
2191
|
+
},
|
|
2192
|
+
set fallbackWarn(val) {
|
|
2193
|
+
if (_composer.value) {
|
|
2194
|
+
_composer.value.missingWarn = val;
|
|
2195
|
+
}
|
|
2196
|
+
},
|
|
2197
|
+
get fallbackRoot() {
|
|
2198
|
+
return _composer.value ? _composer.value.fallbackRoot : _fallbackRoot;
|
|
2199
|
+
},
|
|
2200
|
+
set fallbackRoot(val) {
|
|
2201
|
+
if (_composer.value) {
|
|
2202
|
+
_composer.value.fallbackRoot = val;
|
|
2203
|
+
}
|
|
2204
|
+
},
|
|
2205
|
+
get fallbackFormat() {
|
|
2206
|
+
return _composer.value ? _composer.value.fallbackFormat : _fallbackFormat;
|
|
2207
|
+
},
|
|
2208
|
+
set fallbackFormat(val) {
|
|
2209
|
+
if (_composer.value) {
|
|
2210
|
+
_composer.value.fallbackFormat = val;
|
|
2211
|
+
}
|
|
2212
|
+
},
|
|
2213
|
+
get warnHtmlMessage() {
|
|
2214
|
+
return _composer.value
|
|
2215
|
+
? _composer.value.warnHtmlMessage
|
|
2216
|
+
: _warnHtmlMessage;
|
|
2217
|
+
},
|
|
2218
|
+
set warnHtmlMessage(val) {
|
|
2219
|
+
if (_composer.value) {
|
|
2220
|
+
_composer.value.warnHtmlMessage = val;
|
|
2221
|
+
}
|
|
2222
|
+
},
|
|
2223
|
+
get escapeParameter() {
|
|
2224
|
+
return _composer.value
|
|
2225
|
+
? _composer.value.escapeParameter
|
|
2226
|
+
: _escapeParameter;
|
|
2227
|
+
},
|
|
2228
|
+
set escapeParameter(val) {
|
|
2229
|
+
if (_composer.value) {
|
|
2230
|
+
_composer.value.escapeParameter = val;
|
|
2231
|
+
}
|
|
2232
|
+
},
|
|
2233
|
+
t,
|
|
2234
|
+
getPostTranslationHandler,
|
|
2235
|
+
setPostTranslationHandler,
|
|
2236
|
+
getMissingHandler,
|
|
2237
|
+
setMissingHandler,
|
|
2238
|
+
rt,
|
|
2239
|
+
d,
|
|
2240
|
+
n,
|
|
2241
|
+
tm,
|
|
2242
|
+
te,
|
|
2243
|
+
getLocaleMessage,
|
|
2244
|
+
setLocaleMessage,
|
|
2245
|
+
mergeLocaleMessage,
|
|
2246
|
+
getDateTimeFormat,
|
|
2247
|
+
setDateTimeFormat,
|
|
2248
|
+
mergeDateTimeFormat,
|
|
2249
|
+
getNumberFormat,
|
|
2250
|
+
setNumberFormat,
|
|
2251
|
+
mergeNumberFormat
|
|
2252
|
+
};
|
|
2253
|
+
function sync(composer) {
|
|
2254
|
+
composer.locale.value = _locale.value;
|
|
2255
|
+
composer.fallbackLocale.value = _fallbackLocale.value;
|
|
2256
|
+
Object.keys(_messages.value).forEach(locale => {
|
|
2257
|
+
composer.mergeLocaleMessage(locale, _messages.value[locale]);
|
|
2258
|
+
});
|
|
2259
|
+
Object.keys(_datetimeFormats.value).forEach(locale => {
|
|
2260
|
+
composer.mergeDateTimeFormat(locale, _datetimeFormats.value[locale]);
|
|
2261
|
+
});
|
|
2262
|
+
Object.keys(_numberFormats.value).forEach(locale => {
|
|
2263
|
+
composer.mergeNumberFormat(locale, _numberFormats.value[locale]);
|
|
2264
|
+
});
|
|
2265
|
+
composer.escapeParameter = _escapeParameter;
|
|
2266
|
+
composer.fallbackFormat = _fallbackFormat;
|
|
2267
|
+
composer.fallbackRoot = _fallbackRoot;
|
|
2268
|
+
composer.fallbackWarn = _fallbackWarn;
|
|
2269
|
+
composer.missingWarn = _missingWarn;
|
|
2270
|
+
composer.warnHtmlMessage = _warnHtmlMessage;
|
|
2271
|
+
}
|
|
2272
|
+
vue.onBeforeMount(() => {
|
|
2273
|
+
if (instance.proxy == null || instance.proxy.$i18n == null) {
|
|
2274
|
+
throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_COMPOSITION_IN_LEGACY);
|
|
2275
|
+
}
|
|
2276
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2277
|
+
const composer = (_composer.value = instance.proxy.$i18n
|
|
2278
|
+
.__composer);
|
|
2279
|
+
if (scope === 'global') {
|
|
2280
|
+
_locale.value = composer.locale.value;
|
|
2281
|
+
_fallbackLocale.value = composer.fallbackLocale.value;
|
|
2282
|
+
_messages.value = composer.messages.value;
|
|
2283
|
+
_datetimeFormats.value = composer.datetimeFormats.value;
|
|
2284
|
+
_numberFormats.value = composer.numberFormats.value;
|
|
2285
|
+
}
|
|
2286
|
+
else if (isLocale) {
|
|
2287
|
+
sync(composer);
|
|
2288
|
+
}
|
|
2289
|
+
});
|
|
2290
|
+
return wrapper;
|
|
2291
|
+
}
|
|
2292
|
+
const globalExportProps = [
|
|
2293
|
+
'locale',
|
|
2294
|
+
'fallbackLocale',
|
|
2295
|
+
'availableLocales'
|
|
2296
|
+
];
|
|
2297
|
+
const globalExportMethods = ['t', 'rt', 'd', 'n', 'tm'] ;
|
|
2298
|
+
function injectGlobalFields(app, composer) {
|
|
2299
|
+
const i18n = Object.create(null);
|
|
2300
|
+
globalExportProps.forEach(prop => {
|
|
2301
|
+
const desc = Object.getOwnPropertyDescriptor(composer, prop);
|
|
2302
|
+
if (!desc) {
|
|
2303
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
|
2304
|
+
}
|
|
2305
|
+
const wrap = vue.isRef(desc.value) // check computed props
|
|
2306
|
+
? {
|
|
2307
|
+
get() {
|
|
2308
|
+
return desc.value.value;
|
|
2309
|
+
},
|
|
2310
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2311
|
+
set(val) {
|
|
2312
|
+
desc.value.value = val;
|
|
2313
|
+
}
|
|
2314
|
+
}
|
|
2315
|
+
: {
|
|
2316
|
+
get() {
|
|
2317
|
+
return desc.get && desc.get();
|
|
2318
|
+
}
|
|
2319
|
+
};
|
|
2320
|
+
Object.defineProperty(i18n, prop, wrap);
|
|
2321
|
+
});
|
|
2322
|
+
app.config.globalProperties.$i18n = i18n;
|
|
2323
|
+
globalExportMethods.forEach(method => {
|
|
2324
|
+
const desc = Object.getOwnPropertyDescriptor(composer, method);
|
|
2325
|
+
if (!desc || !desc.value) {
|
|
2326
|
+
throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
|
2327
|
+
}
|
|
2328
|
+
Object.defineProperty(app.config.globalProperties, `$${method}`, desc);
|
|
2329
|
+
});
|
|
2330
|
+
}
|
|
2331
|
+
|
|
2332
|
+
// register message compiler at vue-i18n
|
|
2333
|
+
coreBase.registerMessageCompiler(coreBase.compileToFunction);
|
|
2334
|
+
// register message resolver at vue-i18n
|
|
2335
|
+
coreBase.registerMessageResolver(coreBase.resolveValue);
|
|
2336
|
+
// register fallback locale at vue-i18n
|
|
2337
|
+
coreBase.registerLocaleFallbacker(coreBase.fallbackWithLocaleChain);
|
|
2338
|
+
// NOTE: experimental !!
|
|
2339
|
+
{
|
|
2340
|
+
const target = shared.getGlobalThis();
|
|
2341
|
+
target.__INTLIFY__ = true;
|
|
2342
|
+
coreBase.setDevToolsHook(target.__INTLIFY_DEVTOOLS_GLOBAL_HOOK__);
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
exports.DatetimeFormat = DatetimeFormat;
|
|
2346
|
+
exports.I18nInjectionKey = I18nInjectionKey;
|
|
2347
|
+
exports.NumberFormat = NumberFormat;
|
|
2348
|
+
exports.Translation = Translation;
|
|
2349
|
+
exports.VERSION = VERSION;
|
|
2350
|
+
exports.castToVueI18n = castToVueI18n;
|
|
2351
|
+
exports.createI18n = createI18n;
|
|
2352
|
+
exports.useI18n = useI18n;
|
|
2353
|
+
exports.vTDirective = vTDirective;
|