import { TranslateService as TranslateServiceExtension , TranslateStore , TranslateLoader , TranslateCompiler , TranslateParser , MissingTranslationHandler , USE_DEFAULT_LANG , USE_STORE , USE_EXTEND } from "@ngx-translate/core" ; import { TranslationResolver , RxTranslation } from "@rxweb/translate" ; import { Inject , Injectable } from '@angular/core' ; import { RequestState } from './request.state' ; import { CUSTOM_LOADER } from '../const/app.const' ; import { Observable } from 'rxjs' ; @ Injectable ( ) export class TranslateService extends TranslateServiceExtension { private changeLangFunc : Function ; private extendExtension : boolean ; private isInternal : boolean = false ; isolateSubscriptions : { [ key ] : Observable < any > ; } = { } ; constructor ( public store , public currentLoader , public compiler , public parser , public missingTranslationHandler , @ Inject ( USE_DEFAULT_LANG ) useDefaultLang = true , @ Inject ( USE_STORE ) isolate = false , @ Inject ( USE_EXTEND ) extend = false , @ Inject ( "singleton" ) singleton = false , private rxTranslation , private requestState , private translationResolver , @ Inject ( CUSTOM_LOADER ) public customLoader ? ) { super ( store , currentLoader , compiler , parser , missingTranslationHandler , useDefaultLang , isolate , extend , null ) ; this . defineProperty ( ) ; this . extendExtension = extend ; if ( singleton ) { this . translationResolver . ngxTranslate = this ; } this [ "updateLangs" ] = ( lang ) => { this . updateLanguages ( lang ) ; } ; this . translationResolver . resolver = this . storeResolve . bind ( this ) ; } languageCode : string ; changeLanguage ( lang , onComplete ) { if ( this . translationResolver . activeTranslationsLength > 1 ) { if ( lang != this . languageCode && ! this . translationResolver . pending ) { this . languageCode = lang ; this . rxTranslation . change ( lang , ( ) => { onComplete ( ) ; } ) ; } else if ( this . translationResolver . pending ) { let time = setTimeout ( ( ) => { clearTimeout ( time ) ; this . changeLanguage ( lang , onComplete ) ; } , 5 ) ; } else onComplete ( ) ; } else { onComplete ( ) ; } } get translations ( ) { return this . store . translations ; } defineProperty ( ) { Object . defineProperty ( this , "pending" , { get : ( ) => this . requestState . pending , set : ( value ) => this . requestState . pending = value , enumerable : true , configurable : true } ) ; Object . defineProperty ( this , "loadingTranslations" , { get : ( ) => this . requestState . loadingTranslations , set : ( value ) => this . requestState . loadingTranslations = value , enumerable : true , configurable : true } ) ; } setDefaultLang ( language ) { this . updateLanguages ( language ) ; return super . setDefaultLang ( language ) ; } useByTranslationName ( translationName , languageCode ) { return super . use ( `${ translationName }_${ languageCode }` ) . subscribe ( t => { } ) ; } use ( language ) { this . updateLanguages ( language ) ; return super . use ( language ) ; } private updateLanguages ( language ) { if ( language && ( ! this . translationResolver . activeLanguage || this . translationResolver . activeLanguage != language ) ) if ( ! this . isInternal ) this . translationResolver . activeLanguage = language ; if ( ( language && this . translationResolver . allowedLanguages . indexOf ( language ) === - 1 ) ) { this . translationResolver . allowedLanguages . push ( language ) ; this . addLangs ( [ language ] ) ; } } private storeResolve ( name , data ) { data = this . compiler . compileTranslations ( data , this . currentLang ) ; this . translations [ name ] = data ; return data ; } }