Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit dd3fb07

Browse files
author
baso10
committed
Load translations with promise, to allow loading before app is initialized
1 parent 4697b43 commit dd3fb07

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

projects/ngx-translate/http-loader/src/lib/http-loader.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ export class TranslateHttpLoader implements TranslateLoader {
99
* Gets the translations from the server
1010
*/
1111
public getTranslation(lang: string): Observable<Object> {
12-
return this.http.get(`${this.prefix}${lang}${this.suffix}`);
12+
if (this.loadedTranslations != null && this.loadedTranslations[lang] != null) {
13+
return Observable.of(this.loadedTranslations[lang]);
14+
}
15+
return Observable.fromPromise(this.preLoad(lang));
16+
}
17+
18+
/**
19+
* Gets the translations from the server as Promise
20+
* @param lang
21+
* @returns Promise<any>
22+
*/
23+
public preLoad(lang: string): Promise<any> {
24+
return this.http.get(`${this.prefix}${lang}${this.suffix}`)
25+
.toPromise()
26+
.then(result => {
27+
this.loadedTranslations[lang] = result;
28+
return result;
29+
})
30+
.catch(() => null);
1331
}
1432
}

0 commit comments

Comments
 (0)