Skip to content

Commit 828512d

Browse files
committed
feat(http-loader): show / hide log
1 parent a49a8fa commit 828512d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

projects/http-loader/src/lib/http-loader.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { HttpBackend, HttpClient } from "@angular/common/http";
22
import { inject, Injectable, InjectionToken, Provider } from "@angular/core";
33
import { mergeDeep, TranslateLoader, TranslationObject } from "@ngx-translate/core";
4-
import { forkJoin, map, Observable } from "rxjs";
4+
import { catchError, forkJoin, map, Observable, of } from "rxjs";
55

66
export interface TranslateHttpLoaderResource {
77
prefix: string;
88
suffix?: string;
9+
showLog?: boolean;
910
}
1011

1112
export interface TranslateHttpLoaderConfig {
1213
prefix: string;
1314
suffix: string;
15+
showLog?: boolean;
1416
ressources: (string | TranslateHttpLoaderResource)[];
1517
enforceLoading: boolean;
1618
useHttpBackend: boolean;
@@ -53,17 +55,36 @@ export class TranslateHttpLoader implements TranslateLoader {
5355
if (typeof resource === "string") path = `${resource}${lang}.json`;
5456
else path = `${resource.prefix}${lang}${resource.suffix ?? ".json"}`;
5557

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+
});
5870

5971
return forkJoin(requests).pipe(
6072
map((response) => response.reduce((acc, curr) => mergeDeep(acc, curr), {})),
6173
) as Observable<TranslationObject>;
6274
}
6375

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+
);
6788
}
6889
}
6990

projects/test-app/src/app/app.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ export const appConfig: ApplicationConfig = {
1515
prefix: "./i18n/",
1616
suffix: ".json",
1717
enforceLoading: true,
18-
ressources: [{ prefix: "./i18n/another/", suffix: ".json" }, { prefix: "./i18n/" }],
18+
ressources: [
19+
{ prefix: "./i18n/another/", suffix: ".json" },
20+
{ prefix: "./i18n/awld" },
21+
],
1922
}),
2023
}),
2124
],

0 commit comments

Comments
 (0)