31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { TranslateLoader } from '@ngx-translate/core';
|
|
import { map } from 'rxjs/operators';
|
|
import { Observable } from 'rxjs';
|
|
|
|
interface T {
|
|
[key: string]: string | T;
|
|
}
|
|
|
|
export class PruningTranslationLoader implements TranslateLoader {
|
|
constructor(private _http: HttpClient, private _prefix: string, private _suffix: string) {}
|
|
|
|
getTranslation(lang: string): Observable<T> {
|
|
return this._http.get(`${this._prefix}${lang}${this._suffix}`).pipe(map(result => this._process(result as T)));
|
|
}
|
|
|
|
private _process(object: T): T {
|
|
return (
|
|
Object.keys(object)
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
.filter(key => object.hasOwnProperty(key) && object[key] !== '')
|
|
.reduce(
|
|
(result: T, key) => (
|
|
(result[key] = typeof object[key] === 'object' ? this._process(object[key] as T) : object[key]), result
|
|
),
|
|
{},
|
|
)
|
|
);
|
|
}
|
|
}
|