import { HttpClient } from '@angular/common/http' ; import { TranslateLoader } from '@ngx-translate/core' ; import * as gettext from 'gettext-parser' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; export class TranslatePoHttpLoader implements TranslateLoader { /**
     * Translation domain
     */ public domain = '' ; constructor ( protected _http , protected _prefix = 'i18n' , protected _suffix = '.po' ) { } param lang returns { any } public getTranslation ( lang ) { return this . _http . get ( `${ this . _prefix }/${ lang }${ this . _suffix }` , { responseType : 'text' } ) . pipe ( map ( ( contents ) => this . parse ( contents ) ) ) ; } param contents returns { any } public parse ( contents ) { let translations = { } ; const po = gettext . po . parse ( contents , 'utf-8' ) ; if ( ! po . translations . hasOwnProperty ( this . domain ) ) { return translations ; } Object . keys ( po . translations [ this . domain ] ) . forEach ( key => { const translation = po . translations [ this . domain ] [ key ] . msgstr . pop ( ) ; if ( key . length > 0 && translation . length > 0 ) { translations [ key ] = translation ; } } ) ; return translations ; } }