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 1
1
import { HttpClient } from "@angular/common/http" ;
2
2
import { TranslateLoader } from "@ngx-translate/core" ;
3
3
import "rxjs/add/operator/map" ;
4
+ import { Observable } from 'rxjs/Rx' ;
4
5
5
6
export class TranslateHttpLoader implements TranslateLoader {
7
+ loadedTranslations : { [ index : string ] : Object ; } = { } ;
8
+
6
9
constructor ( private http : HttpClient , public prefix : string = "/assets/i18n/" , public suffix : string = ".json" ) { }
7
10
8
11
/**
@@ -11,6 +14,27 @@ export class TranslateHttpLoader implements TranslateLoader {
11
14
* @returns {any }
12
15
*/
13
16
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
+ } ) ;
15
39
}
16
40
}
You can’t perform that action at this time.
0 commit comments