@@ -41,6 +41,7 @@ export default class VueI18n {
4141 const messages : LocaleMessages = options . messages || { }
4242 const dateTimeFormats = options . dateTimeFormats || { }
4343 const numberFormats = options . numberFormats || { }
44+
4445 this . _vm = null
4546 this . _formatter = options . formatter || new BaseFormatter ( )
4647 this . _missing = options . missing || null
@@ -160,7 +161,12 @@ export default class VueI18n {
160161 return ! val && ! isNull ( this . _root ) && this . _fallbackRoot
161162 }
162163
163- _interpolate ( message : LocaleMessageObject , key : Path , values : any ) : any {
164+ _interpolate (
165+ message : LocaleMessageObject ,
166+ key : Path ,
167+ interpolateMode : string ,
168+ values : any
169+ ) : any {
164170 if ( ! message ) { return null }
165171
166172 const pathRet : PathValue = getPathValue ( message , key )
@@ -197,26 +203,38 @@ export default class VueI18n {
197203 // them with its translation
198204 const matches : any = ret . match ( / ( @ : [ \w | . ] + ) / g)
199205 for ( const idx in matches ) {
200- const link = matches [ idx ]
206+ const link : string = matches [ idx ]
201207 // Remove the leading @:
202- const linkPlaceholder = link . substr ( 2 )
208+ const linkPlaceholder : string = link . substr ( 2 )
203209 // Translate the link
204- const translatedstring = this . _interpolate ( message , linkPlaceholder , values )
205- // Replace the link with the translated string
206- ret = ret . replace ( link , translatedstring )
210+ const translated : any = this . _interpolate ( message , linkPlaceholder , interpolateMode , values )
211+ if ( interpolateMode === 'raw' ) {
212+ return translated
213+ }
214+ // Replace the link with the translated
215+ ret = ret . replace ( link , translated )
207216 }
208217 }
209218
210- return ! values ? ret : this . _format ( ret , values )
219+ return ! values ? ret : this . _render ( ret , interpolateMode , values )
211220 }
212221
213- _format ( message : string , ...values : any ) : string {
214- return this . _formatter . format ( message , ...values )
222+ _render ( message : string , interpolateMode : string , values : any ) : any {
223+ const ret = this . _formatter . format ( message , values )
224+ // if interpolateMode is **not** 'string' ('row'),
225+ // return the compiled data (e.g. ['foo', VNode, 'bar']) with formatter
226+ return interpolateMode === 'string' ? ret . join ( '' ) : ret
215227 }
216228
217- _translate ( messages : LocaleMessages , locale : Locale , fallback : Locale , key : Path , args : any ) : any {
218- let res : any = null
219- res = this . _interpolate ( messages [ locale ] , key , args )
229+ _translate (
230+ messages : LocaleMessages ,
231+ locale : Locale ,
232+ fallback : Locale ,
233+ key : Path ,
234+ interpolateMode : string ,
235+ args : any
236+ ) : any {
237+ let res : any = this . _interpolate ( messages [ locale ] , key , interpolateMode , args )
220238 if ( ! isNull ( res ) ) { return res }
221239
222240 res = this . _interpolate ( messages [ fallback ] , key , args )
@@ -236,7 +254,7 @@ export default class VueI18n {
236254 const parsedArgs = parseArgs ( ...values )
237255 const locale : Locale = parsedArgs . locale || _locale
238256
239- const ret : any = this . _translate ( messages , locale , this . fallbackLocale , key , parsedArgs . params )
257+ const ret : any = this . _translate ( messages , locale , this . fallbackLocale , key , 'string' , parsedArgs . params )
240258 if ( this . _isFallbackRoot ( ret ) ) {
241259 if ( process . env . NODE_ENV !== 'production' && ! this . _silentTranslationWarn ) {
242260 warn ( `Fall back to translate the keypath '${ key } ' with root locale.` )
@@ -252,7 +270,46 @@ export default class VueI18n {
252270 return this . _t ( key , this . locale , this . messages , null , ...values )
253271 }
254272
255- _tc ( key : Path , _locale : Locale , messages : LocaleMessages , host : any , choice ?: number , ...values : any ) : any {
273+ _i ( key : Path , locale : Locale , messages : LocaleMessages , host : any , ...values : any ) : any {
274+ const ret : any =
275+ this . _translate ( messages , locale , this . fallbackLocale , key , 'raw' , values )
276+ if ( this . _isFallbackRoot ( ret ) ) {
277+ if ( process . env . NODE_ENV !== 'production' && ! this . _silentTranslationWarn ) {
278+ warn ( `Fall back to interpolate the keypath '${ key } ' with root locale.` )
279+ }
280+ if ( ! this . _root ) { throw Error ( 'unexpected error' ) }
281+ return this . _root . i ( key , ...values )
282+ } else {
283+ return this . _warnDefault ( locale , key , ret , host )
284+ }
285+ }
286+
287+ i ( key : Path , ...values : any ) : TranslateResult {
288+ if ( ! key ) { return '' }
289+
290+ let locale : Locale = this . locale
291+ let index : number = 0
292+ if ( typeof values [ 0 ] === 'string' ) {
293+ locale = values [ 0 ]
294+ index = 1
295+ }
296+
297+ const params : Array < any > = []
298+ for (let i = index; i < values . length ; i ++ ) {
299+ params . push ( values [ i ] )
300+ }
301+
302+ return this . _i ( key , locale , this . messages , null , ...params )
303+ }
304+
305+ _tc (
306+ key : Path ,
307+ _locale : Locale ,
308+ messages : LocaleMessages ,
309+ host : any ,
310+ choice ?: number ,
311+ ...values : any
312+ ) : any {
256313 if ( ! key ) { return '' }
257314 if ( choice !== undefined ) {
258315 return fetchChoice ( this . _t ( key , _locale , messages , host , ...values ) , choice )
0 commit comments