import Emitter , { globalEmitter } from '../share/emitter' ; import enUS from './lang/en-US' ; import zhCN from './lang/zh-CN' ; type LangItem = { [ x ] : string ; } ; type Langs = { [ x ] : LangItem ; } ; class I18n { private langs : Langs = { enUS , zhCN } ; private current : string = 'enUS' ; constructor ( ) { this . setUp ( ) ; } setUp ( ) { if ( typeof window === 'undefined' ) { return ; } let locale = 'enUS' ; if ( navigator . language ) { const it = navigator . language . split ( '-' ) ; locale = it [ 0 ] ; if ( it . length !== 1 ) { locale += it [ it . length - 1 ] . toUpperCase ( ) ; } } if ( navigator . browserLanguage ) { const it = navigator . browserLanguage . split ( '-' ) ; locale = it [ 0 ] ; if ( it [ 1 ] ) { locale += it [ 1 ] . toUpperCase ( ) ; } } if ( this . current !== locale && this . isAvailable ( locale ) ) { this . current = locale ; globalEmitter . emit ( globalEmitter . EVENT_LANG_CHANGE , this , locale , this . langs [ locale ] ) ; } } isAvailable ( langName ) { return typeof this . langs [ langName ] !== 'undefined' ; } add ( langName , lang ) { this . langs [ langName ] = lang ; } setCurrent ( langName ) { if ( ! this . isAvailable ( langName ) ) { throw new Error ( `Language ${ langName } is not exists` ) ; } if ( this . current !== langName ) { this . current = langName ; globalEmitter . emit ( globalEmitter . EVENT_LANG_CHANGE , this , langName , this . langs [ langName ] ) ; } } get ( key , placeholders ? ) { let str = this . langs [ this . current ] [ key ] || '' ; if ( placeholders ) { Object . keys ( placeholders ) . forEach ( k => { str = str . replace ( new RegExp ( `\\{${ k }\\}` , 'g' ) , placeholders [ k ] ) ; } ) ; } return str ; } getCurrent ( ) { return this . current ; } } const i18n = new I18n ( ) ; export default i18n ;