Commits
Alexandre Vryghem authored 07a2e333ca5
1 1 | import { TranslateLoader } from '@ngx-translate/core'; |
2 2 | import { HttpClient } from '@angular/common/http'; |
3 3 | import { TransferState } from '@angular/platform-browser'; |
4 4 | import { NGX_TRANSLATE_STATE, NgxTranslateState } from './ngx-translate-state'; |
5 5 | import { hasValue } from '../app/shared/empty.util'; |
6 6 | import { map } from 'rxjs/operators'; |
7 7 | import { of as observableOf, Observable } from 'rxjs'; |
8 + | import { environment } from '../environments/environment'; |
8 9 | |
9 10 | /** |
10 11 | * A TranslateLoader for ngx-translate to retrieve i18n messages from the TransferState, or download |
11 12 | * them if they're not available there |
12 13 | */ |
13 14 | export class TranslateBrowserLoader implements TranslateLoader { |
14 15 | constructor( |
15 16 | protected transferState: TransferState, |
16 17 | protected http: HttpClient, |
17 18 | protected prefix?: string, |
26 27 | * @param lang the language code |
27 28 | */ |
28 29 | getTranslation(lang: string): Observable<any> { |
29 30 | // Get the ngx-translate messages from the transfer state, to speed up the initial page load |
30 31 | // client side |
31 32 | const state = this.transferState.get<NgxTranslateState>(NGX_TRANSLATE_STATE, {}); |
32 33 | const messages = state[lang]; |
33 34 | if (hasValue(messages)) { |
34 35 | return observableOf(messages); |
35 36 | } else { |
37 + | const translationHash: string = environment.production ? `.${(process.env.languageHashes as any)[lang + '.json5']}` : ''; |
36 38 | // If they're not available on the transfer state (e.g. when running in dev mode), retrieve |
37 39 | // them using HttpClient |
38 - | return this.http.get('' + this.prefix + lang + this.suffix, { responseType: 'text' }).pipe( |
40 + | return this.http.get(`${this.prefix}${lang}${translationHash}${this.suffix}`, { responseType: 'text' }).pipe( |
39 41 | map((json: any) => JSON.parse(json)) |
40 42 | ); |
41 43 | } |
42 44 | } |
43 45 | } |