import { Inject , Injectable } from '@angular/core' ; import * as i18n from 'i18next' ; import { I18NEXT_ERROR_HANDLING_STRATEGY } from './I18NEXT_TOKENS' ; import { I18NextErrorHandlingStrategy } from './I18NextErrorHandlingStrategies' ; import { I18NextEvents } from './I18NextEvents' ; import { I18NextLoadResult } from './I18NextLoadResult' ; import { ITranslationEvents } from './ITranslationEvents' ; import { ITranslationService } from './ITranslationService' ; const i18next = i18n . default ; @ Injectable ( ) export class I18NextService implements ITranslationService { events : ITranslationEvents = new I18NextEvents ( ) ; get language ( ) { return i18next . language ; } get languages ( ) { return i18next . languages ; } get options ( ) { return i18next . options ; } get modules ( ) { return i18next . modules ; } get services ( ) { return i18next . services ; } get store ( ) { return i18next . store ; } get resolvedLanguage ( ) { return i18next . resolvedLanguage ; } get isInitialized ( ) { return i18next . isInitialized ; } constructor ( @ Inject ( I18NEXT_ERROR_HANDLING_STRATEGY ) private errorHandlingStrategy ) { } public use < T extends i18n . Module > ( module ) { i18next . use . call ( i18next , module ) ; return this ; } init ( options ) { this . subscribeEvents ( ) ; return new Promise < I18NextLoadResult > ( ( resolve , reject ) => { i18next . init . call ( i18next , Object . assign ( { } , options ?? { } ) , this . errorHandlingStrategy . handle ( resolve , reject ) ) ; } ) ; } t ( key , optionsOrDefault ? , options ? ) { const hasDefault = optionsOrDefault && typeof optionsOrDefault === 'string' ; if ( hasDefault ) { return i18next . t . call ( i18next , key , optionsOrDefault , options ) ; } else { return i18next . t . call ( i18next , key , < string | undefined > optionsOrDefault , undefined ) ; } } public format ( value , format ? , lng ? ) { return i18next . format . call ( i18next , value , format , lng ) ; } public exists ( key , options ) { return i18next . exists . call ( i18next , key , options ) ; } getFixedT ( lng , ns ? , keyPrefix ? ) ; getFixedT ( lng , ns , keyPrefix ? ) ; getFixedT ( lng , ns ? , keyPrefix ? ) { return i18next . getFixedT . call ( i18next , lng , ns , keyPrefix ) ; } public setDefaultNamespace ( ns ) { i18next . setDefaultNamespace . call ( i18next , ns ) ; } public dir ( lng ? ) { return i18next . dir . call ( i18next , lng ) ; } public changeLanguage ( lng ) { return new Promise < i18n . TFunction > ( ( resolve , reject ) => { return i18next . changeLanguage . call ( i18next , lng , this . errorHandlingStrategy . handle ( resolve , reject ) ) ; } ) ; } public loadNamespaces ( namespaces ) { return new Promise < I18NextLoadResult > ( ( resolve , reject ) => { i18next . loadNamespaces . call ( i18next , namespaces , this . errorHandlingStrategy . handle ( resolve , reject ) ) ; } ) ; } public loadLanguages ( lngs ) { return new Promise < void > ( ( resolve , reject ) => { i18next . loadLanguages . call ( i18next , lngs , this . errorHandlingStrategy . handle ( resolve , reject ) ) ; } ) ; } public loadResources ( callback ? ) { i18next . loadResources . call ( i18next , callback ) ; } public getDataByLanguage ( lng ) { return i18next . getDataByLanguage . call ( i18next , lng ) ; } public async reloadResources ( ... params ) { await i18next . reloadResources . apply ( i18next , params ) ; } public getResource ( lng , ns , key , options ) { return i18next . getResource . call ( i18next , lng , ns , key , options ) ; } public addResource ( lng , ns , key , value , options ) { return i18next . addResource . call ( i18next , lng , ns , key , value , options ) ; } public addResources ( lng , ns , resources ) { return i18next . addResources . call ( i18next , lng , ns , resources ) ; } public addResourceBundle ( lng , ns , resources , deep , overwrite ) { return i18next . addResourceBundle . call ( i18next , lng , ns , resources , deep , overwrite ) ; } public hasResourceBundle ( lng , ns ) { return i18next . hasResourceBundle . call ( i18next , lng , ns ) ; } public getResourceBundle ( lng , ns ) { return i18next . getResourceBundle . call ( i18next , lng , ns ) ; } public removeResourceBundle ( lng , ns ) { return i18next . removeResourceBundle . call ( i18next , lng , ns ) ; } private subscribeEvents ( ) { i18next . on . call ( i18next , 'initialized' , ( options ) => { this . events . initialized . next ( options ) ; } ) ; i18next . on . call ( i18next , 'loaded' , ( loaded ) => this . events . loaded . next ( loaded ) ) ; i18next . on . call ( i18next , 'failedLoading' , ( lng , ns , msg ) => this . events . failedLoading . next ( { lng , ns , msg } ) ) ; i18next . on . call ( i18next , 'languageChanged' , ( lng ) => { this . events . languageChanged . next ( lng ) ; } ) ; i18next . on . call ( i18next , 'missingKey' , ( lngs , namespace , key , res ) => this . events . missingKey . next ( { lngs , namespace , key , res } ) ) ; i18next . on . call ( i18next , 'added' , ( lng , ns ) => this . events . added . next ( { lng , ns } ) ) ; i18next . on . call ( i18next , 'removed' , ( lng , ns ) => this . events . removed . next ( { lng , ns } ) ) ; } }