1- import { ComponentPublicInstance , ComponentOptions } from 'vue'
1+ import {
2+ App ,
3+ getCurrentInstance ,
4+ ComponentInternalInstance ,
5+ ComponentPublicInstance ,
6+ ComponentOptions
7+ } from 'vue'
28import { Path } from './path'
39import { Locale } from './runtime/context'
410import { Composer } from './composer'
@@ -41,11 +47,66 @@ type LegacyMixin = {
4147 $n ( value : number , args : { [ key : string ] : string } ) : NumberFormatResult
4248}
4349
50+ const legacyInstances = new Map < ComponentInternalInstance , VueI18n > ( )
51+
52+ function getLegacyInstance (
53+ key : ComponentInternalInstance | null ,
54+ legacyDefault : VueI18n
55+ ) : VueI18n {
56+ return key ? legacyInstances . get ( key ) || legacyDefault : legacyDefault
57+ }
58+
4459// supports compatibility for vue-i18n legacy mixin
45- export function getMixin (
46- vueI18n : VueI18n ,
60+ export function defineMixin (
61+ app : App ,
62+ legacyGlobal : VueI18n ,
4763 composer : Composer
4864) : ComponentOptions {
65+ // inject Legacy APIs for globally
66+ Object . defineProperty ( app . config . globalProperties , '$i18n' , {
67+ get : ( ) => {
68+ const instance = getCurrentInstance ( )
69+ return instance
70+ ? legacyInstances . get ( instance ) || legacyGlobal
71+ : legacyGlobal
72+ }
73+ } )
74+
75+ Object . defineProperty ( app . config . globalProperties , '$t' , {
76+ value : ( ...args : unknown [ ] ) : TranslateResult => {
77+ const vueI18n = getLegacyInstance ( getCurrentInstance ( ) , legacyGlobal )
78+ return vueI18n . t ( ...args )
79+ }
80+ } )
81+
82+ Object . defineProperty ( app . config . globalProperties , '$tc' , {
83+ value : ( ...args : unknown [ ] ) : TranslateResult => {
84+ const vueI18n = getLegacyInstance ( getCurrentInstance ( ) , legacyGlobal )
85+ return vueI18n . tc ( ...args )
86+ }
87+ } )
88+
89+ Object . defineProperty ( app . config . globalProperties , '$te' , {
90+ value : ( key : Path , locale ?: Locale ) : boolean => {
91+ const vueI18n = getLegacyInstance ( getCurrentInstance ( ) , legacyGlobal )
92+ return vueI18n . te ( key , locale )
93+ }
94+ } )
95+
96+ Object . defineProperty ( app . config . globalProperties , '$d' , {
97+ value : ( ...args : unknown [ ] ) : DateTimeFormatResult => {
98+ const vueI18n = getLegacyInstance ( getCurrentInstance ( ) , legacyGlobal )
99+ return vueI18n . d ( ...args )
100+ }
101+ } )
102+
103+ Object . defineProperty ( app . config . globalProperties , '$n' , {
104+ value : ( ...args : unknown [ ] ) : NumberFormatResult => {
105+ const vueI18n = getLegacyInstance ( getCurrentInstance ( ) , legacyGlobal )
106+ return vueI18n . n ( ...args )
107+ }
108+ } )
109+
49110 return {
50111 beforeCreate ( this : ComponentPublicInstance < LegacyMixin > ) {
51112 const options = this . $options
@@ -57,34 +118,22 @@ export function getMixin(
57118 optionsI18n . __i18n = options . __i18n
58119 }
59120 optionsI18n . __root = composer
60- this . $i18n = createVueI18n ( optionsI18n )
121+ const instance = getCurrentInstance ( )
122+ if ( instance ) {
123+ legacyInstances . set ( instance , createVueI18n ( optionsI18n ) )
124+ }
61125 } else if ( options . __i18n ) {
62- this . $i18n = createVueI18n ( { __i18n : options . __i18n , __root : composer } )
63- } else if ( this . $root && this . $root . proxy ) {
64- // root i18n
65- // TODO: should resolve type inference
66- // eslint-disable-next-line @typescript-eslint/no-explicit-any
67- const instance : any = this . $root . proxy
68- this . $i18n = instance . $i18n || vueI18n
69- } else if ( this . $parent && this . $parent . proxy ) {
70- // parent i18n
71- // TODO: should resolve type inference
72- // eslint-disable-next-line @typescript-eslint/no-explicit-any
73- const instance : any = this . $parent . proxy
74- this . $i18n = instance . $i18n || vueI18n
75- } else {
76- this . $i18n = vueI18n
126+ const instance = getCurrentInstance ( )
127+ if ( instance ) {
128+ legacyInstances . set (
129+ instance ,
130+ createVueI18n ( {
131+ __i18n : options . __i18n ,
132+ __root : composer
133+ } )
134+ )
135+ }
77136 }
78-
79- // define vue-i18n legacy APIs
80- this . $t = ( ...args : unknown [ ] ) : TranslateResult => this . $i18n . t ( ...args )
81- this . $tc = ( ...args : unknown [ ] ) : TranslateResult => this . $i18n . tc ( ...args )
82- this . $te = ( key : Path , locale ?: Locale ) : boolean =>
83- this . $i18n . te ( key , locale )
84- this . $d = ( ...args : unknown [ ] ) : DateTimeFormatResult =>
85- this . $i18n . d ( ...args )
86- this . $n = ( ...args : unknown [ ] ) : NumberFormatResult =>
87- this . $i18n . n ( ...args )
88137 }
89138 }
90139}
0 commit comments