33import VueI18n from './index'
44import { isPlainObject , warn } from './util'
55
6- export default {
7- computed : {
8- $t ( ) : Function {
9- if ( ! this . $i18n ) {
10- throw Error ( `Failed in $t due to not find VueI18n instance` )
11- }
12- // add dependency tracking !!
13- const locale : Locale = this . $i18n . locale
14- const messages : LocaleMessages = this . $i18n . messages
15- return ( key : string , ...args : any ) : TranslateResult => {
16- return this . $i18n . _t ( key , locale , messages , this , ...args )
17- }
18- } ,
19-
20- $tc ( ) : Function {
21- if ( ! this . $i18n ) {
22- throw Error ( `Failed in $tc due to not find VueI18n instance` )
23- }
24- // add dependency tracking !!
25- const locale : Locale = this . $i18n . locale
26- const messages : LocaleMessages = this . $i18n . messages
27- return ( key : string , choice ? : number , ...args : any ) : TranslateResult => {
28- return this . $i18n . _tc ( key , locale , messages , this , choice , ...args )
29- }
30- } ,
6+ const $t = ( vm : any ) : Function => {
7+ // add dependency tracking !!
8+ const locale : Locale = vm . $i18n . locale
9+ const messages : LocaleMessages = vm . $i18n . messages
10+ return ( key : string , ...args : any ) : TranslateResult => {
11+ return vm . $i18n . _t ( key , locale , messages , vm , ...args )
12+ }
13+ }
14+ const $tc = ( vm : any ) : Function => {
15+ // add dependency tracking !!
16+ const locale : Locale = vm . $i18n . locale
17+ const messages : LocaleMessages = vm . $i18n . messages
18+ return ( key : string , choice ? : number , ...args : any ) : TranslateResult => {
19+ return vm . $i18n . _tc ( key , locale , messages , vm , choice , ...args )
20+ }
21+ }
22+ const $te = ( vm : any ) : Function => {
23+ // add dependency tracking !!
24+ const locale : Locale = vm . $i18n . locale
25+ const messages : LocaleMessages = vm . $i18n . messages
26+ return ( key : string , ...args : any ) : boolean => {
27+ return vm . $i18n . _te ( key , locale , messages , ...args )
28+ }
29+ }
3130
32- $te ( ) : Function {
33- if ( ! this . $i18n ) {
34- throw Error ( `Failed in $te due to not find VueI18n instance` )
35- }
36- // add dependency tracking !!
37- const locale : Locale = this . $i18n . locale
38- const messages : LocaleMessages = this . $i18n . messages
39- return ( key : string , ...args : any ) : boolean => {
40- return this . $i18n . _te ( key , locale , messages , ...args )
41- }
42- }
43- } ,
31+ function defineComputed ( vm : any , options : any ) : void {
32+ options . computed = options . computed || { }
33+ options . computed . $t = ( ) => $t ( vm )
34+ options . computed . $tc = ( ) => $tc ( vm )
35+ options . computed . $te = ( ) => $te ( vm )
36+ }
4437
38+ export default {
4539 beforeCreate ( ) : void {
4640 const options : any = this . $options
4741 if ( options . i18n ) {
4842 if ( options . i18n instanceof VueI18n ) {
4943 this . $i18n = options . i18n
44+ defineComputed ( this , options )
5045 } else if ( isPlainObject ( options . i18n ) ) {
5146 // component local i18n
5247 if ( this . $root && this . $root . $i18n && this . $root . $i18n instanceof VueI18n ) {
5348 options . i18n . root = this . $root . $i18n
5449 }
5550 this . $i18n = new VueI18n ( options . i18n )
51+ defineComputed ( this , options )
5652 if ( options . i18n . sync ) {
5753 this . _localeWatcher = this . $i18n . watchLocale ( )
5854 }
@@ -64,10 +60,13 @@ export default {
6460 } else if ( this . $root && this . $root . $i18n && this . $root . $i18n instanceof VueI18n ) {
6561 // root i18n
6662 this . $i18n = this . $root . $i18n
63+ defineComputed ( this , options )
6764 }
6865 } ,
6966
70- destroyed ( ) : void {
67+ beforeDestroy ( ) : void {
68+ if ( ! this . $i18n ) { return }
69+
7170 if ( this . _localeWatcher ) {
7271 this . $i18n . unwatchLocale ( )
7372 delete this . _localeWatcher
0 commit comments