UNPKG

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