1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | Object.defineProperty(exports, '__esModule', { value: true });
|
9 |
|
10 | var coreBase = require('@intlify/core-base');
|
11 | var compositionApi = require('@vue/composition-api');
|
12 | var shared = require('@intlify/shared');
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const VERSION = '9.2.0-beta.6';
|
23 |
|
24 | let code = coreBase.CompileErrorCodes.__EXTEND_POINT__;
|
25 | const inc = () => code++;
|
26 | const I18nErrorCodes = {
|
27 |
|
28 | UNEXPECTED_RETURN_TYPE: code,
|
29 |
|
30 | INVALID_ARGUMENT: inc(),
|
31 |
|
32 | MUST_BE_CALL_SETUP_TOP: inc(),
|
33 | NOT_INSLALLED: inc(),
|
34 | NOT_AVAILABLE_IN_LEGACY_MODE: inc(),
|
35 |
|
36 | REQUIRED_VALUE: inc(),
|
37 | INVALID_VALUE: inc(),
|
38 |
|
39 | CANNOT_SETUP_VUE_DEVTOOLS_PLUGIN: inc(),
|
40 | NOT_INSLALLED_WITH_PROVIDE: inc(),
|
41 |
|
42 | UNEXPECTED_ERROR: inc(),
|
43 |
|
44 | NOT_COMPATIBLE_LEGACY_VUE_I18N: inc(),
|
45 |
|
46 | BRIDGE_SUPPORT_VUE_2_ONLY: inc(),
|
47 |
|
48 | __EXTEND_POINT__: inc()
|
49 | };
|
50 | function createI18nError(code, ...args) {
|
51 | return coreBase.createCompileError(code, null, undefined);
|
52 | }
|
53 |
|
54 | const SetPluralRulesSymbol = shared.makeSymbol('__setPluralRules');
|
55 | shared.makeSymbol('__intlifyMeta');
|
56 | const LegacyInstanceSymbol = shared.makeSymbol('__legacyVueI18n');
|
57 |
|
58 |
|
59 |
|
60 | function isLegacyVueI18n(VueI18n) {
|
61 | if (VueI18n == null || VueI18n.version == null) {
|
62 | return false;
|
63 | }
|
64 | return (Number(VueI18n.version.split('.')[0]) || -1) >= 8;
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | function handleFlatJson(obj) {
|
70 |
|
71 | if (!shared.isObject(obj)) {
|
72 | return obj;
|
73 | }
|
74 | for (const key in obj) {
|
75 |
|
76 | if (!shared.hasOwn(obj, key)) {
|
77 | continue;
|
78 | }
|
79 |
|
80 | if (!key.includes('.')) {
|
81 |
|
82 | if (shared.isObject(obj[key])) {
|
83 | handleFlatJson(obj[key]);
|
84 | }
|
85 | }
|
86 |
|
87 | else {
|
88 |
|
89 | const subKeys = key.split('.');
|
90 | const lastIndex = subKeys.length - 1;
|
91 | let currentObj = obj;
|
92 | for (let i = 0; i < lastIndex; i++) {
|
93 | if (!(subKeys[i] in currentObj)) {
|
94 | currentObj[subKeys[i]] = {};
|
95 | }
|
96 | currentObj = currentObj[subKeys[i]];
|
97 | }
|
98 |
|
99 | currentObj[subKeys[lastIndex]] = obj[key];
|
100 | delete obj[key];
|
101 |
|
102 | if (shared.isObject(currentObj[subKeys[lastIndex]])) {
|
103 | handleFlatJson(currentObj[subKeys[lastIndex]]);
|
104 | }
|
105 | }
|
106 | }
|
107 | return obj;
|
108 | }
|
109 | function getLocaleMessages(locale, options) {
|
110 | const { messages, __i18n, messageResolver, flatJson } = options;
|
111 |
|
112 | const ret = shared.isPlainObject(messages)
|
113 | ? messages
|
114 | : shared.isArray(__i18n)
|
115 | ? {}
|
116 | : { [locale]: {} };
|
117 |
|
118 | if (shared.isArray(__i18n)) {
|
119 | __i18n.forEach(({ locale, resource }) => {
|
120 | if (locale) {
|
121 | ret[locale] = ret[locale] || {};
|
122 | deepCopy(resource, ret[locale]);
|
123 | }
|
124 | else {
|
125 | deepCopy(resource, ret);
|
126 | }
|
127 | });
|
128 | }
|
129 |
|
130 | if (messageResolver == null && flatJson) {
|
131 | for (const key in ret) {
|
132 | if (shared.hasOwn(ret, key)) {
|
133 | handleFlatJson(ret[key]);
|
134 | }
|
135 | }
|
136 | }
|
137 | return ret;
|
138 | }
|
139 | const isNotObjectOrIsArray = (val) => !shared.isObject(val) || shared.isArray(val);
|
140 |
|
141 | function deepCopy(src, des) {
|
142 |
|
143 | if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
|
144 | throw createI18nError(I18nErrorCodes.INVALID_VALUE);
|
145 | }
|
146 | for (const key in src) {
|
147 | if (shared.hasOwn(src, key)) {
|
148 | if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
|
149 |
|
150 |
|
151 |
|
152 | des[key] = src[key];
|
153 | }
|
154 | else {
|
155 |
|
156 | deepCopy(src[key], des[key]);
|
157 | }
|
158 | }
|
159 | }
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 | let composerID = 0;
|
165 | function defineCoreMissingHandler(missing) {
|
166 | return ((ctx, locale, key, type) => {
|
167 | return missing(locale, key, compositionApi.getCurrentInstance() || undefined, type);
|
168 | });
|
169 | }
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function createComposer(options = {}, VueI18nLegacy) {
|
177 | const { __root } = options;
|
178 | const _isGlobal = __root === undefined;
|
179 | let _inheritLocale = shared.isBoolean(options.inheritLocale)
|
180 | ? options.inheritLocale
|
181 | : true;
|
182 | const _locale = compositionApi.ref(
|
183 |
|
184 | __root && _inheritLocale
|
185 | ? __root.locale.value
|
186 | : shared.isString(options.locale)
|
187 | ? options.locale
|
188 | : coreBase.DEFAULT_LOCALE);
|
189 | const _fallbackLocale = compositionApi.ref(
|
190 |
|
191 | __root && _inheritLocale
|
192 | ? __root.fallbackLocale.value
|
193 | : shared.isString(options.fallbackLocale) ||
|
194 | shared.isArray(options.fallbackLocale) ||
|
195 | shared.isPlainObject(options.fallbackLocale) ||
|
196 | options.fallbackLocale === false
|
197 | ? options.fallbackLocale
|
198 | : _locale.value);
|
199 | const _messages = compositionApi.ref(getLocaleMessages(_locale.value, options));
|
200 |
|
201 | const _datetimeFormats = compositionApi.ref(shared.isPlainObject(options.datetimeFormats)
|
202 | ? options.datetimeFormats
|
203 | : { [_locale.value]: {} })
|
204 | ;
|
205 |
|
206 | const _numberFormats = compositionApi.ref(shared.isPlainObject(options.numberFormats)
|
207 | ? options.numberFormats
|
208 | : { [_locale.value]: {} })
|
209 | ;
|
210 |
|
211 |
|
212 | let _missingWarn = __root
|
213 | ? __root.missingWarn
|
214 | : shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
|
215 | ? options.missingWarn
|
216 | : true;
|
217 |
|
218 | let _fallbackWarn = __root
|
219 | ? __root.fallbackWarn
|
220 | : shared.isBoolean(options.fallbackWarn) || shared.isRegExp(options.fallbackWarn)
|
221 | ? options.fallbackWarn
|
222 | : true;
|
223 |
|
224 | let _fallbackRoot = __root
|
225 | ? __root.fallbackRoot
|
226 | : shared.isBoolean(options.fallbackRoot)
|
227 | ? options.fallbackRoot
|
228 | : true;
|
229 |
|
230 | let _fallbackFormat = !!options.fallbackFormat;
|
231 |
|
232 | let _missing = shared.isFunction(options.missing) ? options.missing : null;
|
233 | let _runtimeMissing = shared.isFunction(options.missing)
|
234 | ? defineCoreMissingHandler(options.missing)
|
235 | : null;
|
236 |
|
237 | let _postTranslation = shared.isFunction(options.postTranslation)
|
238 | ? options.postTranslation
|
239 | : null;
|
240 | let _warnHtmlMessage = shared.isBoolean(options.warnHtmlMessage)
|
241 | ? options.warnHtmlMessage
|
242 | : true;
|
243 | let _escapeParameter = !!options.escapeParameter;
|
244 |
|
245 |
|
246 | const _modifiers = __root
|
247 | ? __root.modifiers
|
248 | : shared.isPlainObject(options.modifiers)
|
249 | ? options.modifiers
|
250 | : {};
|
251 |
|
252 | let _pluralRules = options.pluralRules || (__root && __root.pluralRules);
|
253 |
|
254 | let __legacy;
|
255 | {
|
256 | if (!isLegacyVueI18n(VueI18nLegacy)) {
|
257 | createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N);
|
258 | }
|
259 | const legacyOptions = {
|
260 | locale: _locale.value,
|
261 | fallbackLocale: _fallbackLocale.value,
|
262 | messages: _messages.value,
|
263 | dateTimeFormats: _datetimeFormats.value,
|
264 | numberFormats: _numberFormats.value,
|
265 | modifiers: _modifiers,
|
266 | missing: _missing,
|
267 | fallbackRoot: _fallbackRoot,
|
268 | postTranslation: _postTranslation,
|
269 | pluralizationRules: _pluralRules,
|
270 | escapeParameterHtml: _escapeParameter,
|
271 | sync: _inheritLocale,
|
272 | silentFallbackWarn: shared.isBoolean(_fallbackWarn)
|
273 | ? !_fallbackWarn
|
274 | : _fallbackWarn,
|
275 | silentTranslationWarn: shared.isBoolean(_missingWarn)
|
276 | ? !_missingWarn
|
277 | : _missingWarn,
|
278 | formatFallbackMessages: shared.isBoolean(_fallbackFormat)
|
279 | ? !_fallbackFormat
|
280 | : _fallbackFormat,
|
281 | warnHtmlInMessage: shared.isBoolean(_warnHtmlMessage)
|
282 | ? _warnHtmlMessage
|
283 | ? 'warn'
|
284 | : 'off'
|
285 | : 'off'
|
286 | };
|
287 | __legacy = new VueI18nLegacy(legacyOptions);
|
288 | }
|
289 |
|
290 |
|
291 | let _context;
|
292 | function getCoreContext() {
|
293 | const ctxOptions = {
|
294 | version: VERSION,
|
295 | locale: _locale.value,
|
296 | fallbackLocale: _fallbackLocale.value,
|
297 | messages: _messages.value,
|
298 | modifiers: _modifiers,
|
299 | pluralRules: _pluralRules,
|
300 | missing: _runtimeMissing === null ? undefined : _runtimeMissing,
|
301 | missingWarn: _missingWarn,
|
302 | fallbackWarn: _fallbackWarn,
|
303 | fallbackFormat: _fallbackFormat,
|
304 | unresolving: true,
|
305 | postTranslation: _postTranslation === null ? undefined : _postTranslation,
|
306 | warnHtmlMessage: _warnHtmlMessage,
|
307 | escapeParameter: _escapeParameter,
|
308 | messageResolver: options.messageResolver,
|
309 | __meta: { framework: 'vue' }
|
310 | };
|
311 | {
|
312 | ctxOptions.datetimeFormats = _datetimeFormats.value;
|
313 | ctxOptions.numberFormats = _numberFormats.value;
|
314 | ctxOptions.__datetimeFormatters = shared.isPlainObject(_context)
|
315 | ? _context.__datetimeFormatters
|
316 | : undefined;
|
317 | ctxOptions.__numberFormatters = shared.isPlainObject(_context)
|
318 | ? _context.__numberFormatters
|
319 | : undefined;
|
320 | }
|
321 | return coreBase.createCoreContext(ctxOptions);
|
322 | }
|
323 | _context = getCoreContext();
|
324 | coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
325 |
|
326 | function trackReactivityValues() {
|
327 | return [
|
328 | _locale.value,
|
329 | _fallbackLocale.value,
|
330 | _messages.value,
|
331 | _datetimeFormats.value,
|
332 | _numberFormats.value
|
333 | ]
|
334 | ;
|
335 | }
|
336 |
|
337 | const locale = compositionApi.computed({
|
338 | get: () => _locale.value,
|
339 | set: val => {
|
340 | _locale.value = val;
|
341 | {
|
342 | if (__legacy) {
|
343 | __legacy.locale = val;
|
344 | }
|
345 | }
|
346 | _context.locale = _locale.value;
|
347 | }
|
348 | });
|
349 |
|
350 | const fallbackLocale = compositionApi.computed({
|
351 | get: () => _fallbackLocale.value,
|
352 | set: val => {
|
353 | _fallbackLocale.value = val;
|
354 | {
|
355 | if (__legacy) {
|
356 | __legacy.fallbackLocale = val;
|
357 | }
|
358 | }
|
359 | _context.fallbackLocale = _fallbackLocale.value;
|
360 | coreBase.updateFallbackLocale(_context, _locale.value, val);
|
361 | }
|
362 | });
|
363 |
|
364 | const messages = compositionApi.computed(() => _messages.value);
|
365 |
|
366 | const datetimeFormats = compositionApi.computed(() => _datetimeFormats.value);
|
367 |
|
368 | const numberFormats = compositionApi.computed(() => _numberFormats.value);
|
369 |
|
370 | function getPostTranslationHandler() {
|
371 | return shared.isFunction(_postTranslation) ? _postTranslation : null;
|
372 | }
|
373 |
|
374 | function setPostTranslationHandler(handler) {
|
375 | _postTranslation = handler;
|
376 | _context.postTranslation = handler;
|
377 | }
|
378 |
|
379 | function getMissingHandler() {
|
380 | return _missing;
|
381 | }
|
382 |
|
383 | function setMissingHandler(handler) {
|
384 | if (handler !== null) {
|
385 | _runtimeMissing = defineCoreMissingHandler(handler);
|
386 | }
|
387 | _missing = handler;
|
388 | _context.missing = _runtimeMissing;
|
389 | }
|
390 | function wrapWithDeps(fn, argumentParser, warnType, fallbackSuccess, fallbackFail, successCondition) {
|
391 | trackReactivityValues();
|
392 |
|
393 | let ret;
|
394 | {
|
395 | ret = fn(_context);
|
396 | }
|
397 | if (shared.isNumber(ret) && ret === coreBase.NOT_REOSLVED) {
|
398 | const [key, arg2] = argumentParser();
|
399 | return __root && _fallbackRoot
|
400 | ? fallbackSuccess(__root)
|
401 | : fallbackFail(key);
|
402 | }
|
403 | else if (successCondition(ret)) {
|
404 | return ret;
|
405 | }
|
406 | else {
|
407 |
|
408 | throw createI18nError(I18nErrorCodes.UNEXPECTED_RETURN_TYPE);
|
409 | }
|
410 | }
|
411 |
|
412 | function t(...args) {
|
413 | 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));
|
414 | }
|
415 |
|
416 | function rt(...args) {
|
417 | const [arg1, arg2, arg3] = args;
|
418 | if (arg3 && !shared.isObject(arg3)) {
|
419 | throw createI18nError(I18nErrorCodes.INVALID_ARGUMENT);
|
420 | }
|
421 | return t(...[arg1, arg2, shared.assign({ resolvedMessage: true }, arg3 || {})]);
|
422 | }
|
423 |
|
424 | function d(...args) {
|
425 | 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));
|
426 | }
|
427 |
|
428 | function n(...args) {
|
429 | 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));
|
430 | }
|
431 | function setPluralRules(rules) {
|
432 | _pluralRules = rules;
|
433 | _context.pluralRules = _pluralRules;
|
434 | }
|
435 |
|
436 | function te(key, locale) {
|
437 | const targetLocale = shared.isString(locale) ? locale : _locale.value;
|
438 | const message = getLocaleMessage(targetLocale);
|
439 | return _context.messageResolver(message, key) !== null;
|
440 | }
|
441 | function resolveMessages(key) {
|
442 | let messages = null;
|
443 | const locales = coreBase.fallbackWithLocaleChain(_context, _fallbackLocale.value, _locale.value);
|
444 | for (let i = 0; i < locales.length; i++) {
|
445 | const targetLocaleMessages = _messages.value[locales[i]] || {};
|
446 | const messageValue = _context.messageResolver(targetLocaleMessages, key);
|
447 | if (messageValue != null) {
|
448 | messages = messageValue;
|
449 | break;
|
450 | }
|
451 | }
|
452 | return messages;
|
453 | }
|
454 |
|
455 | function tm(key) {
|
456 | const messages = resolveMessages(key);
|
457 |
|
458 | return messages != null
|
459 | ? messages
|
460 | : __root
|
461 | ? __root.tm(key) || {}
|
462 | : {};
|
463 | }
|
464 |
|
465 | function getLocaleMessage(locale) {
|
466 | return (_messages.value[locale] || {});
|
467 | }
|
468 |
|
469 | function setLocaleMessage(locale, message) {
|
470 | _messages.value[locale] = message;
|
471 | {
|
472 | __legacy && __legacy.setLocaleMessage(locale, message);
|
473 | }
|
474 | _context.messages = _messages.value;
|
475 | }
|
476 |
|
477 | function mergeLocaleMessage(locale, message) {
|
478 | _messages.value[locale] = _messages.value[locale] || {};
|
479 | {
|
480 | __legacy && __legacy.mergeLocaleMessage(locale, message);
|
481 | }
|
482 | deepCopy(message, _messages.value[locale]);
|
483 | _context.messages = _messages.value;
|
484 | }
|
485 |
|
486 | function getDateTimeFormat(locale) {
|
487 | return _datetimeFormats.value[locale] || {};
|
488 | }
|
489 |
|
490 | function setDateTimeFormat(locale, format) {
|
491 | _datetimeFormats.value[locale] = format;
|
492 | {
|
493 | __legacy && __legacy.setDateTimeFormat(locale, format);
|
494 | }
|
495 | _context.datetimeFormats = _datetimeFormats.value;
|
496 | coreBase.clearDateTimeFormat(_context, locale, format);
|
497 | }
|
498 |
|
499 | function mergeDateTimeFormat(locale, format) {
|
500 | _datetimeFormats.value[locale] = shared.assign(_datetimeFormats.value[locale] || {}, format);
|
501 | {
|
502 | __legacy && __legacy.mergeDateTimeFormat(locale, format);
|
503 | }
|
504 | _context.datetimeFormats = _datetimeFormats.value;
|
505 | coreBase.clearDateTimeFormat(_context, locale, format);
|
506 | }
|
507 |
|
508 | function getNumberFormat(locale) {
|
509 | return _numberFormats.value[locale] || {};
|
510 | }
|
511 |
|
512 | function setNumberFormat(locale, format) {
|
513 | _numberFormats.value[locale] = format;
|
514 | {
|
515 | __legacy && __legacy.setNumberFormat(locale, format);
|
516 | }
|
517 | _context.numberFormats = _numberFormats.value;
|
518 | coreBase.clearNumberFormat(_context, locale, format);
|
519 | }
|
520 |
|
521 | function mergeNumberFormat(locale, format) {
|
522 | _numberFormats.value[locale] = shared.assign(_numberFormats.value[locale] || {}, format);
|
523 | {
|
524 | __legacy && __legacy.mergeNumberFormat(locale, format);
|
525 | }
|
526 | _context.numberFormats = _numberFormats.value;
|
527 | coreBase.clearNumberFormat(_context, locale, format);
|
528 | }
|
529 |
|
530 | composerID++;
|
531 |
|
532 | if (__root) {
|
533 | compositionApi.watch(__root.locale, (val) => {
|
534 | if (_inheritLocale) {
|
535 | _locale.value = val;
|
536 | {
|
537 | if (__legacy) {
|
538 | __legacy.locale = val;
|
539 | }
|
540 | }
|
541 | _context.locale = val;
|
542 | coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
543 | }
|
544 | });
|
545 | compositionApi.watch(__root.fallbackLocale, (val) => {
|
546 | if (_inheritLocale) {
|
547 | _fallbackLocale.value = val;
|
548 | {
|
549 | if (__legacy) {
|
550 | __legacy.fallbackLocale = val;
|
551 | }
|
552 | }
|
553 | _context.fallbackLocale = val;
|
554 | coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
555 | }
|
556 | });
|
557 | }
|
558 |
|
559 | const composer = {
|
560 | id: composerID,
|
561 | locale,
|
562 | fallbackLocale,
|
563 | get inheritLocale() {
|
564 | return _inheritLocale;
|
565 | },
|
566 | set inheritLocale(val) {
|
567 | _inheritLocale = val;
|
568 | {
|
569 | if (__legacy) {
|
570 | __legacy._sync = val;
|
571 | }
|
572 | }
|
573 | if (val && __root) {
|
574 | _locale.value = __root.locale.value;
|
575 | _fallbackLocale.value = __root.fallbackLocale.value;
|
576 | {
|
577 | if (__legacy) {
|
578 | __legacy.locale = __root.locale.value;
|
579 | __legacy.fallbackLocale = __root.fallbackLocale.value;
|
580 | }
|
581 | }
|
582 | coreBase.updateFallbackLocale(_context, _locale.value, _fallbackLocale.value);
|
583 | }
|
584 | },
|
585 | get availableLocales() {
|
586 | return Object.keys(_messages.value).sort();
|
587 | },
|
588 | messages,
|
589 | get modifiers() {
|
590 | return _modifiers;
|
591 | },
|
592 | get pluralRules() {
|
593 | return _pluralRules || {};
|
594 | },
|
595 | get isGlobal() {
|
596 | return _isGlobal;
|
597 | },
|
598 | get missingWarn() {
|
599 | return _missingWarn;
|
600 | },
|
601 | set missingWarn(val) {
|
602 | _missingWarn = val;
|
603 | _context.missingWarn = _missingWarn;
|
604 | },
|
605 | get fallbackWarn() {
|
606 | return _fallbackWarn;
|
607 | },
|
608 | set fallbackWarn(val) {
|
609 | _fallbackWarn = val;
|
610 | _context.fallbackWarn = _fallbackWarn;
|
611 | },
|
612 | get fallbackRoot() {
|
613 | return _fallbackRoot;
|
614 | },
|
615 | set fallbackRoot(val) {
|
616 | _fallbackRoot = val;
|
617 | },
|
618 | get fallbackFormat() {
|
619 | return _fallbackFormat;
|
620 | },
|
621 | set fallbackFormat(val) {
|
622 | _fallbackFormat = val;
|
623 | _context.fallbackFormat = _fallbackFormat;
|
624 | },
|
625 | get warnHtmlMessage() {
|
626 | return _warnHtmlMessage;
|
627 | },
|
628 | set warnHtmlMessage(val) {
|
629 | _warnHtmlMessage = val;
|
630 | _context.warnHtmlMessage = val;
|
631 | },
|
632 | get escapeParameter() {
|
633 | return _escapeParameter;
|
634 | },
|
635 | set escapeParameter(val) {
|
636 | _escapeParameter = val;
|
637 | _context.escapeParameter = val;
|
638 | },
|
639 | t,
|
640 | getLocaleMessage,
|
641 | setLocaleMessage,
|
642 | mergeLocaleMessage,
|
643 | getPostTranslationHandler,
|
644 | setPostTranslationHandler,
|
645 | getMissingHandler,
|
646 | setMissingHandler,
|
647 | [SetPluralRulesSymbol]: setPluralRules
|
648 | };
|
649 | {
|
650 | composer.datetimeFormats = datetimeFormats;
|
651 | composer.numberFormats = numberFormats;
|
652 | composer.rt = rt;
|
653 | composer.te = te;
|
654 | composer.tm = tm;
|
655 | composer.d = d;
|
656 | composer.n = n;
|
657 | composer.getDateTimeFormat = getDateTimeFormat;
|
658 | composer.setDateTimeFormat = setDateTimeFormat;
|
659 | composer.mergeDateTimeFormat = mergeDateTimeFormat;
|
660 | composer.getNumberFormat = getNumberFormat;
|
661 | composer.setNumberFormat = setNumberFormat;
|
662 | composer.mergeNumberFormat = mergeNumberFormat;
|
663 | }
|
664 | {
|
665 | composer[LegacyInstanceSymbol] = __legacy;
|
666 | }
|
667 | return composer;
|
668 | }
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 | function defineMixin(i18n, VueI18n // eslint-disable-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
|
676 | ) {
|
677 | return {
|
678 | beforeCreate() {
|
679 | const options = this.$options;
|
680 | if (options.__VUE18N__INSTANCE__) {
|
681 | return;
|
682 | }
|
683 | options.i18n = options.i18n || (options.__i18n ? {} : null);
|
684 | this._i18nBridgeRoot = i18n;
|
685 | if (i18n.mode === 'composition') {
|
686 | this._i18n = i18n;
|
687 | return;
|
688 | }
|
689 | if (options.i18n) {
|
690 | if (options.i18n instanceof VueI18n) {
|
691 |
|
692 | if (options.__i18n) {
|
693 | try {
|
694 | const localeMessages = options.i18n && options.i18n.messages
|
695 | ? options.i18n.messages
|
696 | : {};
|
697 | options.__i18n.forEach(resource => deepCopy(localeMessages, JSON.parse(resource)));
|
698 | Object.keys(localeMessages).forEach((locale) => {
|
699 | options.i18n.mergeLocaleMessage(locale, localeMessages[locale]);
|
700 | });
|
701 | }
|
702 | catch (e) {
|
703 | }
|
704 | }
|
705 | this._i18n = options.i18n;
|
706 | this._i18nWatcher = this._i18n.watchI18nData();
|
707 | }
|
708 | else if (shared.isPlainObject(options.i18n)) {
|
709 | const rootI18n = this.$root &&
|
710 | this.$root.$i18n &&
|
711 | this.$root.$i18n instanceof VueI18n
|
712 | ? this.$root.$i18n
|
713 | : null;
|
714 |
|
715 | if (rootI18n) {
|
716 | options.i18n.root = this.$root;
|
717 | options.i18n.formatter = rootI18n.formatter;
|
718 | options.i18n.fallbackLocale = rootI18n.fallbackLocale;
|
719 | options.i18n.formatFallbackMessages =
|
720 | rootI18n.formatFallbackMessages;
|
721 | options.i18n.silentTranslationWarn = rootI18n.silentTranslationWarn;
|
722 | options.i18n.silentFallbackWarn = rootI18n.silentFallbackWarn;
|
723 | options.i18n.pluralizationRules = rootI18n.pluralizationRules;
|
724 | options.i18n.preserveDirectiveContent =
|
725 | rootI18n.preserveDirectiveContent;
|
726 | }
|
727 |
|
728 | if (options.__i18n) {
|
729 | try {
|
730 | const localeMessages = options.i18n && options.i18n.messages
|
731 | ? options.i18n.messages
|
732 | : {};
|
733 | options.__i18n.forEach(resource => deepCopy(localeMessages, JSON.parse(resource)));
|
734 | options.i18n.messages = localeMessages;
|
735 | }
|
736 | catch (e) {
|
737 | }
|
738 | }
|
739 | const { sharedMessages } = options.i18n;
|
740 | if (sharedMessages && shared.isPlainObject(sharedMessages)) {
|
741 | deepCopy(options.i18n.messages, sharedMessages);
|
742 | }
|
743 | this._i18n = new VueI18n(options.i18n);
|
744 | this._i18nWatcher = this._i18n.watchI18nData();
|
745 | if (options.i18n.sync === undefined || !!options.i18n.sync) {
|
746 | this._localeWatcher = this.$i18n.watchLocale();
|
747 | }
|
748 | if (rootI18n) {
|
749 | rootI18n.onComponentInstanceCreated(this._i18n);
|
750 | }
|
751 | }
|
752 | else ;
|
753 | }
|
754 | else if (this.$root &&
|
755 | this.$root.$i18n &&
|
756 | this.$root.$i18n instanceof VueI18n) {
|
757 |
|
758 | this._i18n = this.$root.$i18n;
|
759 | }
|
760 | else if (options.parent &&
|
761 | options.parent.$i18n &&
|
762 | options.parent.$i18n instanceof VueI18n) {
|
763 |
|
764 | this._i18n = options.parent.$i18n;
|
765 | }
|
766 | },
|
767 | beforeMount() {
|
768 | const options = this.$options;
|
769 | if (options.__VUE18N__INSTANCE__) {
|
770 | return;
|
771 | }
|
772 | options.i18n = options.i18n || (options.__i18n ? {} : null);
|
773 | if (options.i18n) {
|
774 | if (options.i18n instanceof VueI18n) {
|
775 |
|
776 | this._i18n.subscribeDataChanging(this);
|
777 | this._subscribing = true;
|
778 | }
|
779 | else if (shared.isPlainObject(options.i18n)) {
|
780 | this._i18n.subscribeDataChanging(this);
|
781 | this._subscribing = true;
|
782 | }
|
783 | else ;
|
784 | }
|
785 | else if (this.$root &&
|
786 | this.$root.$i18n &&
|
787 | this.$root.$i18n instanceof VueI18n) {
|
788 | this._i18n.subscribeDataChanging(this);
|
789 | this._subscribing = true;
|
790 | }
|
791 | else if (options.parent &&
|
792 | options.parent.$i18n &&
|
793 | options.parent.$i18n instanceof VueI18n) {
|
794 | this._i18n.subscribeDataChanging(this);
|
795 | this._subscribing = true;
|
796 | }
|
797 | },
|
798 | beforeDestroy() {
|
799 | const options = this.$options;
|
800 | if (options.__VUE18N__INSTANCE__) {
|
801 | return;
|
802 | }
|
803 | if (this._i18nBridgeRoot) {
|
804 | delete this._i18nBridgeRoot;
|
805 | return;
|
806 | }
|
807 | if (i18n.mode === 'composition') {
|
808 | delete this._i18n;
|
809 | return;
|
810 | }
|
811 | if (!this._i18n) {
|
812 | return;
|
813 | }
|
814 | const self = this;
|
815 | this.$nextTick(() => {
|
816 | if (self._subscribing) {
|
817 | self._i18n.unsubscribeDataChanging(self);
|
818 | delete self._subscribing;
|
819 | }
|
820 | if (self._i18nWatcher) {
|
821 | self._i18nWatcher();
|
822 | self._i18n.destroyVM();
|
823 | delete self._i18nWatcher;
|
824 | }
|
825 | if (self._localeWatcher) {
|
826 | self._localeWatcher();
|
827 | delete self._localeWatcher;
|
828 | }
|
829 | });
|
830 | }
|
831 | };
|
832 | }
|
833 |
|
834 |
|
835 | let _legacyVueI18n = null;
|
836 | let _legacyI18n = null;
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 | const I18nInjectionKey =
|
847 | shared.makeSymbol('global-vue-i18n');
|
848 |
|
849 | function createI18n(options = {}, VueI18nLegacy) {
|
850 | if (_legacyI18n) {
|
851 | return _legacyI18n;
|
852 | }
|
853 | {
|
854 | _legacyVueI18n = VueI18nLegacy;
|
855 | }
|
856 |
|
857 | const __legacyMode = shared.isBoolean(options.legacy)
|
858 | ? options.legacy
|
859 | : true;
|
860 | !!options.globalInjection;
|
861 | const __instances = new Map();
|
862 | const __global = createGlobal(options, __legacyMode, VueI18nLegacy);
|
863 | shared.makeSymbol('');
|
864 | function __getInstance(component) {
|
865 | return __instances.get(component) || null;
|
866 | }
|
867 | function __setInstance(component, instance) {
|
868 | __instances.set(component, instance);
|
869 | }
|
870 | function __deleteInstance(component) {
|
871 | __instances.delete(component);
|
872 | }
|
873 | {
|
874 |
|
875 | const i18n = __global[LegacyInstanceSymbol];
|
876 | Object.defineProperty(i18n, 'global', {
|
877 | get() {
|
878 | return __global;
|
879 | }
|
880 | });
|
881 | Object.defineProperty(i18n, 'mode', {
|
882 | get() {
|
883 | return __legacyMode ? 'legacy' : 'composition';
|
884 | }
|
885 | });
|
886 | Object.defineProperty(i18n, '__instances', {
|
887 | get() {
|
888 | return __instances;
|
889 | }
|
890 | });
|
891 | Object.defineProperty(i18n, 'install', {
|
892 |
|
893 | value: (Vue) => {
|
894 | const version = (Vue && Vue.version && Number(Vue.version.split('.')[0])) || -1;
|
895 | if (version !== 2) {
|
896 | throw createI18nError(I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY);
|
897 | }
|
898 | Vue.mixin(defineMixin(i18n, _legacyVueI18n));
|
899 | }
|
900 | });
|
901 | const methodMap = {
|
902 | __getInstance,
|
903 | __setInstance,
|
904 | __deleteInstance
|
905 | };
|
906 | Object.keys(methodMap).forEach(key => Object.defineProperty(i18n, key, { value: methodMap[key] })
|
907 | );
|
908 | _legacyI18n = i18n;
|
909 | return i18n;
|
910 | }
|
911 | }
|
912 |
|
913 | function useI18n(options = {}) {
|
914 | const instance = compositionApi.getCurrentInstance();
|
915 | if (instance == null) {
|
916 | throw createI18nError(I18nErrorCodes.MUST_BE_CALL_SETUP_TOP);
|
917 | }
|
918 | {
|
919 | if (_legacyVueI18n == null || _legacyI18n == null) {
|
920 | throw createI18nError(I18nErrorCodes.NOT_INSLALLED);
|
921 | }
|
922 | }
|
923 | const i18n = getI18nInstance(instance);
|
924 | const global = getGlobalComposer(i18n);
|
925 | const componentOptions = getComponentOptions(instance);
|
926 | const scope = getScope(options, componentOptions);
|
927 | if (scope === 'global') {
|
928 | adjustI18nResources(global, options, componentOptions);
|
929 | return global;
|
930 | }
|
931 | if (scope === 'parent') {
|
932 | let composer = getComposer(i18n, instance);
|
933 | if (composer == null) {
|
934 | composer = global;
|
935 | }
|
936 | return composer;
|
937 | }
|
938 |
|
939 | if (i18n.mode === 'legacy') {
|
940 | throw createI18nError(I18nErrorCodes.NOT_AVAILABLE_IN_LEGACY_MODE);
|
941 | }
|
942 | const i18nInternal = i18n;
|
943 | let composer = i18nInternal.__getInstance(instance);
|
944 | if (composer == null) {
|
945 | const composerOptions = shared.assign({}, options);
|
946 | if ('__i18n' in componentOptions) {
|
947 | composerOptions.__i18n = componentOptions.__i18n;
|
948 | }
|
949 | if (global) {
|
950 | composerOptions.__root = global;
|
951 | }
|
952 | composer = createComposer(composerOptions, _legacyVueI18n);
|
953 | setupLifeCycle(i18nInternal, instance, composer);
|
954 | i18nInternal.__setInstance(instance, composer);
|
955 | }
|
956 | return composer;
|
957 | }
|
958 | function createGlobal(options, legacyMode, VueI18nLegacy // eslint-disable-line @typescript-eslint/no-explicit-any
|
959 | ) {
|
960 | {
|
961 | if (!isLegacyVueI18n(VueI18nLegacy)) {
|
962 | throw createI18nError(I18nErrorCodes.NOT_COMPATIBLE_LEGACY_VUE_I18N);
|
963 | }
|
964 | return createComposer(options, VueI18nLegacy);
|
965 | }
|
966 | }
|
967 | function getI18nInstance(instance) {
|
968 | {
|
969 | const vm = instance.proxy;
|
970 |
|
971 | if (vm == null) {
|
972 | throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
973 | }
|
974 | const i18n = vm._i18nBridgeRoot;
|
975 |
|
976 | if (!i18n) {
|
977 | throw createI18nError(I18nErrorCodes.NOT_INSLALLED);
|
978 | }
|
979 | return i18n;
|
980 | }
|
981 | }
|
982 |
|
983 | function getComponentOptions(instance) {
|
984 | return instance.proxy.$options;
|
985 | }
|
986 |
|
987 | function getScope(options, componentOptions) {
|
988 |
|
989 | return shared.isEmptyObject(options)
|
990 | ? ('__i18n' in componentOptions)
|
991 | ? 'local'
|
992 | : 'global'
|
993 | : !options.useScope
|
994 | ? 'local'
|
995 | : options.useScope;
|
996 | }
|
997 | function getGlobalComposer(i18n) {
|
998 |
|
999 | return i18n.global;
|
1000 | }
|
1001 | function adjustI18nResources(global, options, componentOptions // eslint-disable-line @typescript-eslint/no-explicit-any
|
1002 | ) {
|
1003 | let messages = shared.isObject(options.messages) ? options.messages : {};
|
1004 | if ('__i18nGlobal' in componentOptions) {
|
1005 | messages = getLocaleMessages(global.locale.value, {
|
1006 | messages,
|
1007 | __i18n: componentOptions.__i18nGlobal
|
1008 | });
|
1009 | }
|
1010 |
|
1011 | const locales = Object.keys(messages);
|
1012 | if (locales.length) {
|
1013 | locales.forEach(locale => {
|
1014 | global.mergeLocaleMessage(locale, messages[locale]);
|
1015 | });
|
1016 | }
|
1017 | {
|
1018 |
|
1019 | if (shared.isObject(options.datetimeFormats)) {
|
1020 | const locales = Object.keys(options.datetimeFormats);
|
1021 | if (locales.length) {
|
1022 | locales.forEach(locale => {
|
1023 | global.mergeDateTimeFormat(locale, options.datetimeFormats[locale]);
|
1024 | });
|
1025 | }
|
1026 | }
|
1027 |
|
1028 | if (shared.isObject(options.numberFormats)) {
|
1029 | const locales = Object.keys(options.numberFormats);
|
1030 | if (locales.length) {
|
1031 | locales.forEach(locale => {
|
1032 | global.mergeNumberFormat(locale, options.numberFormats[locale]);
|
1033 | });
|
1034 | }
|
1035 | }
|
1036 | }
|
1037 | }
|
1038 | function getComposer(i18n, target) {
|
1039 | let composer = null;
|
1040 | const root = target.root;
|
1041 | let current = target.parent;
|
1042 | while (current != null) {
|
1043 | const i18nInternal = i18n;
|
1044 | if (i18n.mode === 'composition') {
|
1045 | composer = i18nInternal.__getInstance(current);
|
1046 | }
|
1047 | else {
|
1048 | {
|
1049 | const vueI18n = i18nInternal.__getInstance(current);
|
1050 | if (vueI18n != null) {
|
1051 | composer = vueI18n
|
1052 | .__composer;
|
1053 | }
|
1054 | }
|
1055 | }
|
1056 | if (composer != null) {
|
1057 | break;
|
1058 | }
|
1059 | if (root === current) {
|
1060 | break;
|
1061 | }
|
1062 | current = current.parent;
|
1063 | }
|
1064 | return composer;
|
1065 | }
|
1066 | function setupLifeCycle(i18n, target, composer) {
|
1067 | {
|
1068 |
|
1069 |
|
1070 | const vm = target.proxy;
|
1071 | if (vm == null) {
|
1072 | throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
1073 | }
|
1074 |
|
1075 | const _i18n = composer[LegacyInstanceSymbol];
|
1076 | if (_i18n === i18n) {
|
1077 | throw createI18nError(I18nErrorCodes.UNEXPECTED_ERROR);
|
1078 | }
|
1079 | vm._i18n = _i18n;
|
1080 | vm._i18n_bridge = true;
|
1081 | vm._i18nWatcher = vm._i18n.watchI18nData();
|
1082 | if (vm._i18n._sync) {
|
1083 | vm._localeWatcher = vm._i18n.watchLocale();
|
1084 | }
|
1085 | let subscribing = false;
|
1086 | compositionApi.onBeforeMount(() => {
|
1087 | vm._i18n.subscribeDataChanging(vm);
|
1088 | subscribing = true;
|
1089 | }, target);
|
1090 | compositionApi.onUnmounted(() => {
|
1091 | if (subscribing) {
|
1092 | vm._i18n.unsubscribeDataChanging(vm);
|
1093 | subscribing = false;
|
1094 | }
|
1095 | if (vm._i18nWatcher) {
|
1096 | vm._i18nWatcher();
|
1097 | vm._i18n.destroyVM();
|
1098 | delete vm._i18nWatcher;
|
1099 | }
|
1100 | if (vm._localeWatcher) {
|
1101 | vm._localeWatcher();
|
1102 | delete vm._localeWatcher;
|
1103 | }
|
1104 | delete vm._i18n_bridge;
|
1105 | delete vm._i18n;
|
1106 | }, target);
|
1107 | }
|
1108 | }
|
1109 |
|
1110 |
|
1111 | coreBase.registerMessageCompiler(coreBase.compileToFunction);
|
1112 |
|
1113 | coreBase.registerMessageResolver(coreBase.resolveValue);
|
1114 |
|
1115 | coreBase.registerLocaleFallbacker(coreBase.fallbackWithLocaleChain);
|
1116 |
|
1117 | exports.I18nInjectionKey = I18nInjectionKey;
|
1118 | exports.VERSION = VERSION;
|
1119 | exports.createI18n = createI18n;
|
1120 | exports.useI18n = useI18n;
|