11/* @flow */
22
33import { install , Vue } from './install'
4- import { warn , isNull , parseArgs , fetchChoice } from './util'
4+ import { warn , isNull , parseArgs , fetchChoice , isPlainObject } from './util'
55import BaseFormatter from './format'
66import getPathValue from './path'
77
@@ -16,13 +16,13 @@ export default class VueI18n {
1616 _root : ?I18n
1717 _sync : ?boolean
1818 _fallbackRoot : boolean
19- _fallbackLocale : string
19+ _fallbackLocale : Locale
2020 _missing : ?MissingHandler
2121 _exist : Function
2222 _watcher : any
2323
2424 constructor ( options : I18nOptions = { } ) {
25- const locale : string = options . locale || 'en-US'
25+ const locale : Locale = options . locale || 'en-US'
2626 const messages : Messages = options . messages || { }
2727 this . _vm = null
2828 this . _fallbackLocale = options . fallbackLocale || 'en-US'
@@ -32,15 +32,15 @@ export default class VueI18n {
3232 this . _sync = options . sync || false
3333 this . _fallbackRoot = options . fallbackRoot || false
3434
35- this . _exist = ( message : Object , key : string ) : boolean => {
35+ this . _exist = ( message : Object , key : Path ) : boolean => {
3636 if ( ! message || ! key ) { return false }
3737 return ! isNull ( getPathValue ( message , key ) )
3838 }
3939
4040 this . _resetVM ( { locale, messages } )
4141 }
4242
43- _resetVM ( data : { locale : string , messages : Messages } ) : void {
43+ _resetVM ( data : { locale : Locale , messages : Messages } ) : void {
4444 const silent = Vue . config . silent
4545 Vue . config . silent = true
4646 this . _vm = new Vue ( { data } )
@@ -70,19 +70,19 @@ export default class VueI18n {
7070 get messages ( ) : Messages { return this . _vm . $data . messages }
7171 set messages ( messages : Messages ) : void { this . _vm . $set ( this . _vm , 'messages' , messages ) }
7272
73- get locale ( ) : string { return this . _vm . $data . locale }
74- set locale ( locale : string ) : void { this . _vm . $set ( this . _vm , 'locale' , locale ) }
73+ get locale ( ) : Locale { return this . _vm . $data . locale }
74+ set locale ( locale : Locale ) : void { this . _vm . $set ( this . _vm , 'locale' , locale ) }
7575
76- get fallbackLocale ( ) : string { return this . _fallbackLocale }
77- set fallbackLocale ( locale : string ) : void { this . _fallbackLocale = locale }
76+ get fallbackLocale ( ) : Locale { return this . _fallbackLocale }
77+ set fallbackLocale ( locale : Locale ) : void { this . _fallbackLocale = locale }
7878
7979 get missing ( ) : ?MissingHandler { return this . _missing }
8080 set missing ( handler : MissingHandler ) : void { this . _missing = handler }
8181
8282 get formatter ( ) : Formatter { return this . _formatter }
8383 set formatter ( formatter : Formatter ) : void { this . _formatter = formatter }
8484
85- _warnDefault ( locale : string , key : string , result : ?any , vm : ?any ) : ?string {
85+ _warnDefault ( locale : Locale , key : Path , result : ?any , vm : ?any ) : ?string {
8686 if ( ! isNull ( result ) ) { return result }
8787 if ( this . missing ) {
8888 this . missing . apply ( null , [ locale , key , vm ] )
@@ -101,43 +101,57 @@ export default class VueI18n {
101101 return ! val && ! isNull ( this . _root ) && this . _fallbackRoot
102102 }
103103
104- _interpolate ( message : Messages , key : string , args : any ) : any {
104+ _interpolate ( message : MessageObject , key : Path , args : any ) : any {
105105 if ( ! message ) { return null }
106106
107- let val : PathValue = getPathValue ( message , key )
108- if ( Array . isArray ( val ) ) { return val }
109- if ( isNull ( val ) ) { val = message [ key ] }
110- if ( isNull ( val ) ) { return null }
111- if ( typeof val !== 'string' ) {
112- warn ( `Value of key '${ key } ' is not a string!` )
113- return null
107+ const pathRet : PathValue = getPathValue ( message , key )
108+ if ( Array . isArray ( pathRet ) ) { return pathRet }
109+
110+ let ret : mixed
111+ if ( isNull ( pathRet ) ) {
112+ if ( isPlainObject ( message ) ) {
113+ ret = message [ key ]
114+ if ( typeof ret !== 'string' ) {
115+ warn ( `Value of key '${ key } ' is not a string!` )
116+ return null
117+ }
118+ } else {
119+ return null
120+ }
121+ } else {
122+ if ( typeof pathRet === 'string' ) {
123+ ret = pathRet
124+ } else {
125+ warn ( `Value of key '${ key } ' is not a string!` )
126+ return null
127+ }
114128 }
115129
116130 // Check for the existance of links within the translated string
117- if ( val . indexOf ( '@:' ) >= 0 ) {
131+ if ( ret . indexOf ( '@:' ) >= 0 ) {
118132 // Match all the links within the local
119133 // We are going to replace each of
120134 // them with its translation
121- const matches : any = val . match ( / ( @ : [ \w | . ] + ) / g)
135+ const matches : any = ret . match ( / ( @ : [ \w | . ] + ) / g)
122136 for ( const idx in matches ) {
123137 const link = matches [ idx ]
124138 // Remove the leading @:
125139 const linkPlaceholder = link . substr ( 2 )
126140 // Translate the link
127141 const translatedstring = this . _interpolate ( message , linkPlaceholder , args )
128142 // Replace the link with the translated string
129- val = val . replace ( link , translatedstring )
143+ ret = ret . replace ( link , translatedstring )
130144 }
131145 }
132146
133- return ! args ? val : this . _format ( val , args )
147+ return ! args ? ret : this . _format ( ret , args )
134148 }
135149
136- _format ( val : any , ...args : any ) : any {
137- return this . _formatter . format ( val , ...args )
150+ _format ( message : string , ...args : any ) : string {
151+ return this . _formatter . format ( message , ...args )
138152 }
139153
140- _translate ( messages : Messages , locale : string , fallback : string , key : string , args : any ) : any {
154+ _translate ( messages : Messages , locale : Locale , fallback : Locale , key : Path , args : any ) : any {
141155 let res : any = null
142156 res = this . _interpolate ( messages [ locale ] , key , args )
143157 if ( ! isNull ( res ) ) { return res }
@@ -153,11 +167,11 @@ export default class VueI18n {
153167 }
154168 }
155169
156- _t ( key : string , _locale : string , messages : Messages , host : any , ...args : any ) : any {
170+ _t ( key : Path , _locale : Locale , messages : Messages , host : any , ...args : any ) : any {
157171 if ( ! key ) { return '' }
158172
159173 const parsedArgs = parseArgs ( ...args )
160- const locale = parsedArgs . locale || _locale
174+ const locale : Locale = parsedArgs . locale || _locale
161175
162176 const ret : any = this . _translate ( messages , locale , this . fallbackLocale , key , parsedArgs . params )
163177 if ( this . _isFallbackRoot ( ret ) ) {
@@ -171,11 +185,11 @@ export default class VueI18n {
171185 }
172186 }
173187
174- t ( key : string , ...args : any ) : string {
188+ t ( key : Path , ...args : any ) : TranslateResult {
175189 return this . _t ( key , this . locale , this . messages , null , ...args )
176190 }
177191
178- _tc ( key : string , _locale : string , messages : Messages , host : any , choice ?: number , ...args : any ) : any {
192+ _tc ( key : Path , _locale : Locale , messages : Messages , host : any , choice ?: number , ...args : any ) : any {
179193 if ( ! key ) { return '' }
180194 if ( choice !== undefined ) {
181195 return fetchChoice ( this . _t ( key , _locale , messages , host , ...args ) , choice )
@@ -184,16 +198,16 @@ export default class VueI18n {
184198 }
185199 }
186200
187- tc ( key : string , choice ?: number , ...args : any ) : any {
201+ tc ( key : Path , choice ?: number , ...args : any ) : TranslateResult {
188202 return this . _tc ( key , this . locale , this . messages , null , choice , ...args )
189203 }
190204
191- _te ( key : string , _locale : string , messages : Messages , ...args : any ) : boolean {
192- const locale = parseArgs ( ...args ) . locale || _locale
205+ _te ( key : Path , _locale : Locale , messages : Messages , ...args : any ) : boolean {
206+ const locale : Locale = parseArgs ( ...args ) . locale || _locale
193207 return this . _exist ( messages [ locale ] , key )
194208 }
195209
196- te ( key : string , ...args : any ) : boolean {
210+ te ( key : Path , ...args : any ) : boolean {
197211 return this . _te ( key , this . locale , this . messages , ...args )
198212 }
199213}
0 commit comments