@@ -431,7 +431,12 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
431
431
: __FEATURE_LEGACY_API__ && isBoolean ( options . legacy )
432
432
? options . legacy
433
433
: __FEATURE_LEGACY_API__
434
- const __globalInjection = /* #__PURE__*/ ! ! options . globalInjection
434
+ // prettier-ignore
435
+ const __globalInjection = /* #__PURE__*/ ! __BRIDGE__
436
+ ? ! ! options . globalInjection
437
+ : isBoolean ( options . globalInjection )
438
+ ? options . globalInjection
439
+ : true
435
440
const __instances = new Map < ComponentInternalInstance , VueI18n | Composer > ( )
436
441
const __global = createGlobal ( options , __legacyMode , VueI18nLegacy )
437
442
const symbol : InjectionKey < I18n > | string = /* #__PURE__*/ makeSymbol (
@@ -570,6 +575,9 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
570
575
throw createI18nError ( I18nErrorCodes . BRIDGE_SUPPORT_VUE_2_ONLY )
571
576
}
572
577
578
+ if ( ! __legacyMode && __globalInjection ) {
579
+ injectGlobalFieldsForBridge ( Vue , i18n , __global as Composer )
580
+ }
573
581
Vue . mixin ( defineMixinBridge ( i18n , _legacyVueI18n ) )
574
582
}
575
583
} )
@@ -1091,3 +1099,27 @@ function injectGlobalFields(app: App, composer: Composer): void {
1091
1099
Object . defineProperty ( app . config . globalProperties , `$${ method } ` , desc )
1092
1100
} )
1093
1101
}
1102
+
1103
+ function injectGlobalFieldsForBridge (
1104
+ Vue : any , // eslint-disable-line @typescript-eslint/no-explicit-any
1105
+ i18n : any , // eslint-disable-line @typescript-eslint/no-explicit-any
1106
+ composer : Composer
1107
+ ) : void {
1108
+ // The composition mode in vue-i18n-bridge is `$18n` is the VueI18n instance.
1109
+ // so we need to tell composer to change the locale.
1110
+ // If we don't do, things like `$t` that are injected will not be reacted.
1111
+ i18n . watchLocale ( composer )
1112
+
1113
+ // define fowardcompatible vue-i18n-next inject fields with `globalInjection`
1114
+ Vue . prototype . $t = function ( ...args : unknown [ ] ) {
1115
+ return Reflect . apply ( composer . t , composer , [ ...args ] )
1116
+ }
1117
+
1118
+ Vue . prototype . $d = function ( ...args : unknown [ ] ) {
1119
+ return Reflect . apply ( composer . d , composer , [ ...args ] )
1120
+ }
1121
+
1122
+ Vue . prototype . $n = function ( ...args : unknown [ ] ) {
1123
+ return Reflect . apply ( composer . n , composer , [ ...args ] )
1124
+ }
1125
+ }
0 commit comments