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

Commit

Permalink
Load translations with promise, to allow loading before app is initia…
Browse files Browse the repository at this point in the history
…lized
  • Loading branch information
baso10 committed Jun 24, 2020
1 parent 4697b43 commit dd3fb07
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion projects/ngx-translate/http-loader/src/lib/http-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@ export class TranslateHttpLoader implements TranslateLoader {
* Gets the translations from the server
*/
public getTranslation(lang: string): Observable<Object> {
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<any>
*/
public preLoad(lang: string): Promise<any> {
return this.http.get(`${this.prefix}${lang}${this.suffix}`)
.toPromise()
.then(result => {
this.loadedTranslations[lang] = result;
return result;
})
.catch(() => null);
}
}

0 comments on commit dd3fb07

Please sign in to comment.