33import VueI18n from './index'
44import { isPlainObject , warn } from './util'
55
6- const $t = ( vm : any ) : Function => {
7- // add dependency tracking !!
8- const locale : Locale = vm . $i18n . locale
9- /* eslint-disable no-unused-vars */
10- const fallback : Locale = vm . $i18n . fallbackLocal
11- /* eslint-enable no-unused-vars */
12- const messages : LocaleMessages = vm . $i18n . vm . messages
13- return ( key : string , ...values : any ) : TranslateResult => {
14- return vm . $i18n . _t ( key , locale , messages , vm , ...values )
15- }
16- }
17- const $tc = ( vm : any ) : Function => {
18- // add dependency tracking !!
19- const locale : Locale = vm . $i18n . locale
20- /* eslint-disable no-unused-vars */
21- const fallback : Locale = vm . $i18n . fallbackLocal
22- /* eslint-enable no-unused-vars */
23- const messages : LocaleMessages = vm . $i18n . vm . messages
24- return ( key : string , choice ? : number , ...values : any ) : TranslateResult => {
25- return vm . $i18n . _tc ( key , locale , messages , vm , choice , ...values )
26- }
27- }
28- const $te = ( vm : any ) : Function => {
29- // add dependency tracking !!
30- const _locale : Locale = vm . $i18n . locale
31- const messages : LocaleMessages = vm . $i18n . vm . messages
32- return ( key : string , locale ? : Locale ) : boolean => {
33- return vm . $i18n . _te ( key , _locale , messages , [ locale ] )
34- }
35- }
36-
37- function defineComputed ( vm : any , options : any ) : void {
38- options . computed = options . computed || { }
39- options . computed . $t = ( ) => $t ( vm )
40- options . computed . $tc = ( ) => $tc ( vm )
41- options . computed . $te = ( ) => $te ( vm )
42- }
43-
446export default {
457 beforeCreate ( ) : void {
468 const options : any = this . $options
@@ -49,7 +11,7 @@ export default {
4911 if ( options . i18n ) {
5012 if ( options . i18n instanceof VueI18n ) {
5113 this . _i18n = options . i18n
52- defineComputed ( this , options )
14+ this . _i18nWatcher = this . _i18n . watchI18nData ( ( ) => this . $forceUpdate ( ) )
5315 } else if ( isPlainObject ( options . i18n ) ) {
5416 // component local i18n
5517 if ( this . $root && this . $root . $i18n && this . $root . $i18n instanceof VueI18n ) {
@@ -69,10 +31,10 @@ export default {
6931 }
7032
7133 this . _i18n = new VueI18n ( options . i18n )
72- defineComputed ( this , options )
34+ this . _i18nWatcher = this . _i18n . watchI18nData ( ( ) => this . $forceUpdate ( ) )
7335
7436 if ( options . i18n . sync === undefined || ! ! options . i18n . sync ) {
75- this . _localeWatcher = this . $i18n . watchLocale ( )
37+ this . _localeWatcher = this . $i18n . watchLocale ( ( ) => this . $forceUpdate ( ) )
7638 }
7739 } else {
7840 if ( process . env . NODE_ENV !== 'production' ) {
@@ -82,15 +44,20 @@ export default {
8244 } else if ( this . $root && this . $root . $i18n && this . $root . $i18n instanceof VueI18n ) {
8345 // root i18n
8446 this . _i18n = this . $root . $i18n
85- defineComputed ( this , options )
47+ this . _i18nWatcher = this . _i18n . watchI18nData ( ( ) => this . $forceUpdate ( ) )
8648 }
8749 } ,
8850
8951 beforeDestroy ( ) : void {
9052 if ( ! this . _i18n ) { return }
9153
54+ if ( this . _i18nWatcher ) {
55+ this . _i18n . unwatchI18nData ( )
56+ delete this . _i18nWatcher
57+ }
58+
9259 if ( this . _localeWatcher ) {
93- this . $i18n . unwatchLocale ( )
60+ this . _i18n . unwatchLocale ( )
9461 delete this . _localeWatcher
9562 }
9663
0 commit comments