From dd3fb07d7b4621cb27f57811580fb63ae4f064d5 Mon Sep 17 00:00:00 2001 From: baso10 Date: Sun, 8 Oct 2017 17:49:57 +0200 Subject: [PATCH] Load translations with promise, to allow loading before app is initialized --- .../http-loader/src/lib/http-loader.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/projects/ngx-translate/http-loader/src/lib/http-loader.ts b/projects/ngx-translate/http-loader/src/lib/http-loader.ts index 0afd4a9..0e5c81a 100644 --- a/projects/ngx-translate/http-loader/src/lib/http-loader.ts +++ b/projects/ngx-translate/http-loader/src/lib/http-loader.ts @@ -9,6 +9,24 @@ export class TranslateHttpLoader implements TranslateLoader { * Gets the translations from the server */ public getTranslation(lang: string): Observable { - return this.http.get(`${this.prefix}${lang}${this.suffix}`); + if (this.loadedTranslations != null && this.loadedTranslations[lang] != null) { + return Observable.of(this.loadedTranslations[lang]); + } + return Observable.fromPromise(this.preLoad(lang)); + } + + /** + * Gets the translations from the server as Promise + * @param lang + * @returns Promise + */ + public preLoad(lang: string): Promise { + return this.http.get(`${this.prefix}${lang}${this.suffix}`) + .toPromise() + .then(result => { + this.loadedTranslations[lang] = result; + return result; + }) + .catch(() => null); } }