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

Load translations with promise, to allow preloading #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Load translations with promise, to allow loading before app is initia…
…lized
baso10 committed Jun 24, 2020
commit dd3fb07d7b4621cb27f57811580fb63ae4f064d5
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
@@ -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);
}
}