This repository was archived by the owner on Nov 8, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Expand file tree Collapse file tree 1 file changed +25
-1
lines changed Original file line number Diff line number Diff line change 11import { HttpClient } from "@angular/common/http" ;
22import { TranslateLoader } from "@ngx-translate/core" ;
33import "rxjs/add/operator/map" ;
4+ import { Observable } from 'rxjs/Rx' ;
45
56export class TranslateHttpLoader implements TranslateLoader {
7+ loadedTranslations : { [ index : string ] : Object ; } = { } ;
8+
69 constructor ( private http : HttpClient , public prefix : string = "/assets/i18n/" , public suffix : string = ".json" ) { }
710
811 /**
@@ -11,6 +14,27 @@ export class TranslateHttpLoader implements TranslateLoader {
1114 * @returns {any }
1215 */
1316 public getTranslation ( lang : string ) : any {
14- return this . http . get ( `${ this . prefix } ${ lang } ${ this . suffix } ` ) ;
17+ if ( this . loadedTranslations != null && this . loadedTranslations [ lang ] != null ) {
18+ return Observable . of ( this . loadedTranslations [ lang ] ) ;
19+ }
20+ return Observable . fromPromise ( this . preLoad ( lang ) ) ;
21+ }
22+
23+ /**
24+ * Gets the translations from the server as Promise
25+ * @param lang
26+ * @returns Promise<any>
27+ */
28+ public preLoad ( lang : string ) : Promise < any > {
29+ return new Promise ( ( resolve , reject ) => {
30+ this . http . get ( `${ this . prefix } ${ lang } ${ this . suffix } ` )
31+ . catch ( ( error : any ) : any => {
32+ resolve ( null ) ;
33+ } )
34+ . subscribe ( ( result ) => {
35+ this . loadedTranslations [ lang ] = result ;
36+ resolve ( result ) ;
37+ } ) ;
38+ } ) ;
1539 }
1640}
You can’t perform that action at this time.
0 commit comments