|
1 | 1 | import { HttpBackend, HttpClient } from "@angular/common/http"; |
2 | 2 | import { inject, Injectable, InjectionToken, Provider } from "@angular/core"; |
3 | 3 | import { mergeDeep, TranslateLoader, TranslationObject } from "@ngx-translate/core"; |
4 | | -import { forkJoin, map, Observable } from "rxjs"; |
| 4 | +import { catchError, forkJoin, map, Observable, of } from "rxjs"; |
5 | 5 |
|
6 | 6 | export interface TranslateHttpLoaderResource { |
7 | 7 | prefix: string; |
8 | 8 | suffix?: string; |
| 9 | + showLog?: boolean; |
9 | 10 | } |
10 | 11 |
|
11 | 12 | export interface TranslateHttpLoaderConfig { |
12 | 13 | prefix: string; |
13 | 14 | suffix: string; |
| 15 | + showLog?: boolean; |
14 | 16 | ressources: (string | TranslateHttpLoaderResource)[]; |
15 | 17 | enforceLoading: boolean; |
16 | 18 | useHttpBackend: boolean; |
@@ -53,17 +55,36 @@ export class TranslateHttpLoader implements TranslateLoader { |
53 | 55 | if (typeof resource === "string") path = `${resource}${lang}.json`; |
54 | 56 | else path = `${resource.prefix}${lang}${resource.suffix ?? ".json"}`; |
55 | 57 |
|
56 | | - return this.http.get(`${path}${cacheBuster}`); |
57 | | - }) as Observable<TranslationObject>[]; |
| 58 | + return this.http.get<TranslationObject>(`${path}${cacheBuster}`).pipe( |
| 59 | + catchError((err) => { |
| 60 | + if ( |
| 61 | + this.config.showLog || |
| 62 | + (resource as TranslateHttpLoaderResource).showLog |
| 63 | + ) { |
| 64 | + console.error(`Error loading translation for ${lang}:`, err); |
| 65 | + } |
| 66 | + return of({}); |
| 67 | + }), |
| 68 | + ); |
| 69 | + }); |
58 | 70 |
|
59 | 71 | return forkJoin(requests).pipe( |
60 | 72 | map((response) => response.reduce((acc, curr) => mergeDeep(acc, curr), {})), |
61 | 73 | ) as Observable<TranslationObject>; |
62 | 74 | } |
63 | 75 |
|
64 | | - return this.http.get<TranslationObject>( |
65 | | - `${this.config.prefix}${lang}${this.config.suffix}${cacheBuster}`, |
66 | | - ); |
| 76 | + return this.http |
| 77 | + .get<TranslationObject>( |
| 78 | + `${this.config.prefix}${lang}${this.config.suffix}${cacheBuster}`, |
| 79 | + ) |
| 80 | + .pipe( |
| 81 | + catchError((err) => { |
| 82 | + if (this.config.showLog) { |
| 83 | + console.error(`Error loading translation for ${lang}:`, err); |
| 84 | + } |
| 85 | + return of({}); |
| 86 | + }), |
| 87 | + ); |
67 | 88 | } |
68 | 89 | } |
69 | 90 |
|
|
0 commit comments