import { Injectable } from '@angular/core' ; import { TranslateService } from '@ngx-translate/core' ; import { Language } from '../../core/language' ; import { Constants } from '../../core/constants' ; import { remote } from 'electron' ; import { Settings } from '../../core/settings' ; @ Injectable ( { providedIn : 'root' } ) export class TranslatorService { private globalEmitter : any = remote . getGlobal ( 'globalEmitter' ) ; private languageChangedListener : any = this . languageChangedHandler . bind ( this ) ; private _selectedLanguage : Language ; constructor ( private translate , private settings ) { this . initialize ( ) ; } public languages : Language [ ] = Constants . languages ; public get selectedLanguage ( ) : Language { return this . _selectedLanguage ; } public set selectedLanguage ( v ) { this . _selectedLanguage = v ; this . settings . language = v . code ; this . globalEmitter . emit ( Constants . languageChangedEvent , v ) ; } public languageChangedHandler ( language ) { this . translate . use ( language . code ) ; } public getAsync ( key , interpolateParams ? ) { return this . translate . get ( key , interpolateParams ) . toPromise ( ) ; } private initialize ( ) { this . translate . setDefaultLang ( this . settings . defaultLanguage ) ; this . _selectedLanguage = this . languages . find ( x => x . code === this . settings . language ) ; this . languageChangedHandler ( this . _selectedLanguage ) ; this . globalEmitter . on ( Constants . languageChangedEvent , this . languageChangedListener ) ; } }