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); } }