Skip to content

Commit 6d0b09e

Browse files
authored
improvmeent: global injection for vue-i18n-bridge (#764)
1 parent 5ec2e64 commit 6d0b09e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

packages/vue-i18n-core/src/composer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
19521952
set: val => {
19531953
_locale.value = val
19541954
if (__BRIDGE__) {
1955-
if (__legacy) {
1955+
if (__legacy && !_isGlobal) {
19561956
__legacy.locale = val
19571957
}
19581958
}
@@ -1966,7 +1966,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
19661966
set: val => {
19671967
_fallbackLocale.value = val
19681968
if (__BRIDGE__) {
1969-
if (__legacy) {
1969+
if (__legacy && !_isGlobal) {
19701970
__legacy.fallbackLocale = val
19711971
}
19721972
}
@@ -2337,7 +2337,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23372337
if (_inheritLocale) {
23382338
_locale.value = val
23392339
if (__BRIDGE__) {
2340-
if (__legacy) {
2340+
if (__legacy && !_isGlobal) {
23412341
__legacy.locale = val
23422342
}
23432343
}
@@ -2349,7 +2349,7 @@ export function createComposer(options: any = {}, VueI18nLegacy?: any): any {
23492349
if (_inheritLocale) {
23502350
_fallbackLocale.value = val
23512351
if (__BRIDGE__) {
2352-
if (__legacy) {
2352+
if (__legacy && !_isGlobal) {
23532353
__legacy.fallbackLocale = val
23542354
}
23552355
}

packages/vue-i18n-core/src/i18n.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,12 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
431431
: __FEATURE_LEGACY_API__ && isBoolean(options.legacy)
432432
? options.legacy
433433
: __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
435440
const __instances = new Map<ComponentInternalInstance, VueI18n | Composer>()
436441
const __global = createGlobal(options, __legacyMode, VueI18nLegacy)
437442
const symbol: InjectionKey<I18n> | string = /* #__PURE__*/ makeSymbol(
@@ -570,6 +575,9 @@ export function createI18n(options: any = {}, VueI18nLegacy?: any): any {
570575
throw createI18nError(I18nErrorCodes.BRIDGE_SUPPORT_VUE_2_ONLY)
571576
}
572577

578+
if (!__legacyMode && __globalInjection) {
579+
injectGlobalFieldsForBridge(Vue, i18n, __global as Composer)
580+
}
573581
Vue.mixin(defineMixinBridge(i18n, _legacyVueI18n))
574582
}
575583
})
@@ -1091,3 +1099,27 @@ function injectGlobalFields(app: App, composer: Composer): void {
10911099
Object.defineProperty(app.config.globalProperties, `$${method}`, desc)
10921100
})
10931101
}
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

Comments
 (0)