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