/** Angular Imports */ import { Injectable } from '@angular/core' ; /** Translation Imports */ import { TranslateService , LangChangeEvent } from '@ngx-translate/core' ; /** Custom Services */ import { Logger } from '../logger/logger.service' ; /** Other Imports */ import { includes } from 'lodash' ; import * as enUS from '../../../translations/en-US.json' ; import * as frFR from '../../../translations/fr-FR.json' ; /** Initialize Logger */ const log = new Logger ( 'I18nService' ) ; param { string } s return { string } export function extract ( s ) { return s ; } @ Injectable ( ) export class I18nService { /** Key to store current language of application in local storage. */ private languageStorageKey = 'mifosXLanguage' ; defaultLanguage : string ; supportedLanguages : string [ ] ; constructor ( private translateService ) { } param { ! string } defaultLanguage param { Array . < String > } supportedLanguages init ( defaultLanguage , supportedLanguages ) { this . defaultLanguage = defaultLanguage ; this . supportedLanguages = supportedLanguages ; this . language = '' ; this . translateService . onLangChange . subscribe ( ( event ) => { localStorage . setItem ( this . languageStorageKey , event . lang ) ; } ) ; } param { string } language set language ( language ) { language = language || localStorage . getItem ( this . languageStorageKey ) || this . translateService . getBrowserCultureLang ( ) ; let isSupportedLanguage = includes ( this . supportedLanguages , language ) ; if ( language && ! isSupportedLanguage ) { language = language . split ( '-' ) [ 0 ] ; language = this . supportedLanguages . find ( supportedLanguage => supportedLanguage . startsWith ( language ) ) || '' ; isSupportedLanguage = Boolean ( language ) ; } if ( ! isSupportedLanguage ) { language = this . defaultLanguage ; } log . debug ( `Language set to ${ language }` ) ; localStorage . setItem ( this . languageStorageKey , language ) ; this . translateService . setDefaultLang ( language ) ; } return { string } get language ( ) : string { return this . translateService . currentLang ; } }