11/* @flow */
22
33import { install , Vue } from './install'
4- import { warn , isNull , parseArgs , fetchChoice , isPlainObject , looseClone } from './util'
4+ import {
5+ warn ,
6+ isNull ,
7+ parseArgs ,
8+ fetchChoice ,
9+ isPlainObject ,
10+ isObject ,
11+ looseClone ,
12+ canUseDateTimeFormat
13+ } from './util'
514import BaseFormatter from './format'
615import getPathValue from './path'
716
@@ -10,6 +19,7 @@ import type { PathValue } from './path'
1019export default class VueI18n {
1120 static install : ( ) => void
1221 static version : string
22+ static availabilities : IntlAvailability
1323
1424 _vm : any
1525 _formatter : Formatter
@@ -21,11 +31,13 @@ export default class VueI18n {
2131 _watcher : any
2232 _i18nWatcher : Function
2333 _silentTranslationWarn : boolean
34+ _dateTimeFormatters : Object
2435
2536 constructor ( options : I18nOptions = { } ) {
2637 const locale : Locale = options . locale || 'en-US'
2738 const fallbackLocale : Locale = options . fallbackLocale || 'en-US'
2839 const messages : LocaleMessages = options . messages || { }
40+ const dateTimeFormats = options . dateTimeFormats || { }
2941 this . _vm = null
3042 this . _formatter = options . formatter || new BaseFormatter ( )
3143 this . _missing = options . missing || null
@@ -37,17 +49,21 @@ export default class VueI18n {
3749 this . _silentTranslationWarn = options . silentTranslationWarn === undefined
3850 ? false
3951 : ! ! options . silentTranslationWarn
52+ this . _dateTimeFormatters = { }
4053
4154 this . _exist = ( message : Object , key : Path ) : boolean => {
4255 if ( ! message || ! key ) { return false }
4356 return ! isNull ( getPathValue ( message , key ) )
4457 }
4558
46- this . _initVM ( { locale, fallbackLocale, messages } )
59+ this . _initVM ( { locale, fallbackLocale, messages, dateTimeFormats } )
4760 }
4861
4962 _initVM ( data : {
50- locale : Locale , fallbackLocale : Locale , messages : LocaleMessages
63+ locale : Locale ,
64+ fallbackLocale : Locale ,
65+ messages : LocaleMessages ,
66+ dateTimeFormats : DateTimeFormats
5167 } ) : void {
5268 const silent = Vue . config . silent
5369 Vue . config . silent = true
@@ -92,6 +108,7 @@ export default class VueI18n {
92108 get vm ( ) : any { return this . _vm }
93109
94110 get messages ( ) : LocaleMessages { return looseClone ( this . _vm . messages ) }
111+ get dateTimeFormats ( ) : DateTimeFormats { return looseClone ( this . _vm . dateTimeFormats ) }
95112
96113 get locale ( ) : Locale { return this . _vm . locale }
97114 set locale ( locale : Locale ) : void {
@@ -256,8 +273,80 @@ export default class VueI18n {
256273 mergeLocaleMessage ( locale : Locale , message : LocaleMessage ) : void {
257274 this. _vm . messages [ locale ] = Vue . util . extend ( this . getLocaleMessage ( locale ) , message )
258275 }
276+
277+ getDateTimeFormat ( locale : Locale ) : DateTimeFormat {
278+ return looseClone ( this . _vm . dateTimeFormats [ locale ] )
279+ }
280+
281+ setDateTimeFormat ( locale : Locale , format : DateTimeFormat ) : void {
282+ this . _vm . dateTimeFormats [ locale ] = format
283+ }
284+
285+ mergeDateTimeFormat ( locale : Locale , format : DateTimeFormat ) : void {
286+ this . _vm . dateTimeFormats [ locale ] = Vue . util . extend ( this . getDateTimeFormat ( locale ) , format )
287+ }
288+
289+ _d ( value : number | Date , _locale : Locale , key : ?string ) : DateTimeFormatResult {
290+ if ( process . env . NODE_ENV !== 'production' && ! VueI18n . availabilities . dateTimeFormat ) {
291+ warn ( 'Cannot format a Date value due to not support Intl.DateTimeFormat.' )
292+ return ''
293+ }
294+
295+ let ret = ''
296+ const dateTimeFormats = this . dateTimeFormats
297+ if ( key ) {
298+ let locale : Locale = _locale
299+ if ( isNull ( dateTimeFormats [ _locale ] [ key ] ) ) {
300+ if ( process . env . NODE_ENV !== 'production' && ! this . _silentTranslationWarn ) {
301+ warn ( `Fall back to the dateTimeFormat of key '${ key } ' with '${ this . fallbackLocale } ' locale.` )
302+ }
303+ locale = this . fallbackLocale
304+ }
305+ const id = `${locale } __$ { key } `
306+ let formatter = this . _dateTimeFormatters [ id ]
307+ const format = dateTimeFormats [ locale ] [ key ]
308+ if ( ! formatter ) {
309+ formatter = this . _dateTimeFormatters [ id ] = Intl . DateTimeFormat ( locale , format )
310+ }
311+ ret = formatter . format ( value )
312+ } else {
313+ ret = Intl . DateTimeFormat ( _locale ) . format ( value )
314+ }
315+
316+ return ret
317+ }
318+
319+ d ( value : number | Date , ...args : any ) : DateTimeFormatResult {
320+ let locale : Locale = this . locale
321+ let key : ?string = null
322+
323+ if ( args . length === 1 ) {
324+ if ( typeof args [ 0 ] === 'string' ) {
325+ key = args [ 0 ]
326+ } else if ( isObject ( args [ 0 ] ) ) {
327+ if ( args [ 0 ] . locale ) {
328+ locale = args [ 0 ] . locale
329+ }
330+ if ( args [ 0 ] . key ) {
331+ key = args [ 0 ] . key
332+ }
333+ }
334+ } else if ( args . length === 2 ) {
335+ if ( typeof args [ 0 ] === 'string' ) {
336+ key = args [ 0 ]
337+ }
338+ if ( typeof args [ 1 ] === 'string' ) {
339+ locale = args [ 1 ]
340+ }
341+ }
342+
343+ return this . _d ( value , locale , key )
344+ }
259345}
260346
347+ VueI18n . availabilities = {
348+ dateTimeFormat : canUseDateTimeFormat
349+ }
261350VueI18n . install = install
262351VueI18n . version = '__VERSION__'
263352
0 commit comments