import { isPlatformBrowser } from '@angular/common' ; import { Inject , Injectable , PLATFORM_ID } from '@angular/core' ; import { TranslateService } from '@ngx-translate/core' ; import { Observable } from 'rxjs' ; import { TRANSLATION_CONFIG , TranslationConfig , TranslationService } from '../interfaces/translation-service.interface' ; import { TranslationStorage } from '../interfaces/translation-storage.interface' ; import { TRANSLATION_LANG_DEFAULT } from '../translation.common' ; @ Injectable ( ) export class BaseTranslationService implements TranslationService { constructor ( private translateService , private translationStorage , @ Inject ( PLATFORM_ID ) private platformId , @ Inject ( TRANSLATION_CONFIG ) private translationConfig ) { } init ( config ? ) { if ( ! config ) { config = this . getConfig ( ) ; } this . translateService . addLangs ( config . languages ) ; this . translateService . setDefaultLang ( config . language ) ; this . translationStorage . setLanguage ( config . currentLanguage ) ; return this . translateService . use ( config . currentLanguage ) ; } getConfig ( ) { const languages = this . translationConfig . languages || [ TRANSLATION_LANG_DEFAULT ] ; let language = this . translationConfig . language || null ; if ( ! language ) { if ( isPlatformBrowser ( this . platformId ) ) { language = window . navigator . language . split ( '-' ) . shift ( ) ; } if ( ! language ) { language = languages [ 0 ] ; } } const currentLanguage = this . translationStorage . getLanguage ( ) || language ; return { languages , language , currentLanguage } ; } getLanguage ( ) { return this . translateService . currentLang ; } getLanguages ( ) { return this . translateService . getLangs ( ) ; } setLanguage ( language ) { this . translationStorage . setLanguage ( language ) ; return this . translateService . use ( language ) ; } }