import { LanguageInfo , LanguageData } from './models' ; import { TranslationDictionary } from './models-dict' ; import * as languages from './languages' ; import * as progresses from './progresses' ; export * from './models' ; export * from './models-dict' ; type LanguageID = keyof typeof languages ; const languageList = ( Object . keys ( languages ) as LanguageID [ ] ) . sort ( ) . map ( id => { const lang = languages [ id ] ; lang . info . progress = progresses [ id ] ; return lang ; } ) ; export const languageInfos = languageList . map ( lang => lang . info ) ; export function getLanguageIdByCode ( languageCode ) { let code = languageCode ; let info = languageInfos . find ( i => i . languageCodes . includes ( code ) ) ; if ( ! info && code . includes ( '-' ) ) { code = code . replace ( /-.+/ , '' ) ; info = languageInfos . find ( i => i . languageCodes . includes ( code ) ) ; } if ( info ) { return info . languageID ; } return 'English' ; } function clone < T > ( obj ) { return JSON . parse ( JSON . stringify ( obj ) ) ; } function mergeDict < T extends any > ( target , source ) { Object . keys ( source ) . forEach ( k => { const vt = target [ k ] ; const vs = source [ k ] ; if ( typeof vs === 'string' && typeof vt === 'string' ) { target [ k ] = vs ; } else { mergeDict ( vt , vs ) ; } } ) ; return target ; } param languageID export function getLanguageDictByID ( languageID ) { const dict = clone ( languages . English . dict ) as TranslationDictionary ; if ( languageID === 'English' ) { return dict ; } const translation = languageList . find ( l => l . info . languageID === languageID ) ; if ( translation ) { mergeDict ( dict , translation . dict ) ; } return dict ; }