import { NgModule , ModuleWithProviders } from '@angular/core' ; import { HttpClientModule , HttpClient } from '@angular/common/http' ; import { TranslateModule , TranslateLoader , TranslateService } from '@ngx-translate/core' ; import { TranslateHttpLoader } from '@ngx-translate/http-loader' ; import { availableLanguages , defaultLanguage , sysOptions } from './i18n.config' ; export const translateLoaderFactory = ( http ) => { return new TranslateHttpLoader ( http , './assets/i18n/' , '.json' ) ; } ; @ NgModule ( { imports : [ HttpClientModule , TranslateModule . forRoot ( { loader : { provide : TranslateLoader , useFactory : ( translateLoaderFactory ) , deps : [ HttpClient ] } } ) ] , providers : [ ] , exports : [ TranslateModule ] } ) export class I18nModule { static forRoot ( ) { return { ngModule : I18nModule , providers : [ ] } ; } constructor ( private translate ) { this . initTranslator ( ) ; } initTranslator ( ) { this . translate . setDefaultLang ( defaultLanguage ) ; const browserLanguage = this . translate . getBrowserLang ( ) || defaultLanguage ; const language = this . getSuitableLanguage ( browserLanguage ) ; console . info ( '[i18n] browserLanguage:' , language ) ; this . translate . use ( language ) ; sysOptions . systemLanguage = language ; } private getSuitableLanguage ( language ) { language = language . substring ( 0 , 2 ) . toLowerCase ( ) ; return availableLanguages . some ( ( x ) => x . code === language ) ? language : defaultLanguage ; } }