Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 75 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,96 @@
import { HttpClient } from '@angular/common/http';
import { TranslateLoader } from '@ngx-translate/core';
import * as gettext from 'gettext-parser';
import { Observable } from 'rxjs';
import { Observable, forkJoin } from 'rxjs';
import { map } from 'rxjs/operators';

export class TranslatePoHttpLoader implements TranslateLoader {
export abstract class AbstractPoHttpLoader implements TranslateLoader {

/**
/**
* Translation domain
*/
public domain = '';
public domain = '';

public abstract getTranslation(lang: string): Observable<any>;
/**
* Parse po file
* @param contents
* @returns {any}
*/
public parse(contents: string): any {
let translations: { [key: string]: string } = {};

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: string = po.translations[this.domain][key].msgstr.pop();
if (key.length > 0 && translation.length > 0) {
translations[key] = translation;
}
});

return translations;
}

}

export interface ITranslationResource {
prefix: string;
suffix: string;
}

export class TranslatePoHttpLoader extends AbstractPoHttpLoader {

constructor(
protected _http: HttpClient,
protected _prefix: string = 'i18n',
protected _suffix: string = '.po'
) {
}
constructor(
protected _http: HttpClient,
protected _prefix: string = 'i18n',
protected _suffix: string = '.po'
) {
super();
}

/**
* Gets the translations from file
* @param lang
* @returns {any}
*/
public getTranslation(lang: string): Observable<any> {
return this._http
.get(`${this._prefix}/${lang}${this._suffix}`, { responseType: 'text' })
.pipe(map((contents: string) => this.parse(contents)));
}
public getTranslation(lang: string): Observable<any> {
return this._http
.get(`${this._prefix}/${lang}${this._suffix}`, { responseType: 'text' })
.pipe(
map(contents => this.parse(contents))
);
}

/**
* Parse po file
* @param contents
* @returns {any}
*/
public parse(contents: string): any {
let translations: { [key: string]: string } = {};
}

const po = gettext.po.parse(contents, 'utf-8');
if (!po.translations.hasOwnProperty(this.domain)) {
return translations;
}
export class MultiTranslatePoHttpLoader extends AbstractPoHttpLoader {

Object.keys(po.translations[this.domain])
.forEach(key => {
const translation: string = po.translations[this.domain][key].msgstr.pop();
if (key.length > 0 && translation.length > 0) {
translations[key] = translation;
}
});
constructor(
protected _http: HttpClient,
protected resources: ITranslationResource[]
) {
super();
}

return translations;
}
/**
* Gets the translations from file/s
* @param lang
* @returns {any}
*/
public getTranslation(lang: string): Observable<any> {
const requests = this.resources.map(resource => {
return this._http.get(`${resource.prefix}/${lang}${resource.suffix}`, { responseType: 'text' });
});
return forkJoin(requests).pipe(
map(translationList => translationList.reduce((translationsAcc, currentTranslation) => translationsAcc + currentTranslation, '')),
map(contents => this.parse(contents))
);
}

}