import { Inject , Injectable , Pipe , PipeTransform } from '@angular/core' ; import * as i18n from 'i18next' ; import { I18NEXT_NAMESPACE , I18NEXT_SCOPE , I18NEXT_SERVICE , } from './I18NEXT_TOKENS' ; import { ITranslationService } from './ITranslationService' ; import { PipeOptions } from './models' ; @ Injectable ( ) @ Pipe ( { name : 'i18next' } ) export class I18NextPipe implements PipeTransform { constructor ( @ Inject ( I18NEXT_SERVICE ) protected translateI18Next , @ Inject ( I18NEXT_NAMESPACE ) protected ns , @ Inject ( I18NEXT_SCOPE ) protected scope ) { } public transform ( key , options ? ) { options = this . prepareOptions ( options ) ; let i18nOpts = this . translateI18Next . options ; if ( options . prependScope === undefined || options . prependScope === true ) { if ( this . scope ) { key = this . prependScope ( key , this . scope , i18nOpts . keySeparator , i18nOpts . nsSeparator ) ; } } if ( options . prependNamespace === undefined || options . prependNamespace === true ) { if ( this . ns ) { key = this . prependNamespace ( key , this . ns , i18nOpts . nsSeparator ) ; } } let result = this . translateI18Next . t ( key , options ) ; if ( options . format ) { if ( result ) { result = this . translateI18Next . format ( result , options . format , this . translateI18Next . language ) ; } } return result ; } private prependScope ( key , scope , keySeparator , nsSeparator ) { const nsSep = nsSeparator || '' ; const keySep = keySeparator || '' ; if ( typeof key === 'string' ) { key = [ key ] ; } if ( typeof scope === 'string' ) { scope = [ scope ] ; } let keysWithScope = [ ] ; for ( let i = 0 ; i < key . length ; i ++ ) { const k = key [ i ] ; if ( ! this . keyContainsNsSeparator ( k , nsSep ) ) { keysWithScope . push ( ... scope . map ( ( sc ) => this . joinStrings ( keySep , sc , k ) ) ) ; } keysWithScope . push ( k ) ; } return keysWithScope ; } private prependNamespace ( key , ns , nsSeparator ) { const nsSep = nsSeparator || '' ; if ( typeof key === 'string' ) { key = [ key ] ; } if ( typeof ns === 'string' ) { ns = [ ns ] ; } let keysWithNamespace = [ ] ; for ( let i = 0 ; i < key . length ; i ++ ) { const k = key [ i ] ; if ( ! this . keyContainsNsSeparator ( k , nsSep ) ) { keysWithNamespace . push ( ... ns . map ( ( n ) => this . joinStrings ( nsSep , n , k ) ) ) ; } keysWithNamespace . push ( k ) ; } return keysWithNamespace ; } private joinStrings ( separator , ... str ) { return [ ... str ] . join ( separator ) ; } private keyContainsNsSeparator ( key , nsSeparator ) { return key . indexOf ( nsSeparator ) !== - 1 ; } private prepareOptions ( options ? ) { options = options || { } ; if ( options . context != null ) options . context = options . context . toString ( ) ; return options ; } }