Skip to content

Commit 9bd0951

Browse files
committed
fix: made interface backward compatible
1 parent d09b267 commit 9bd0951

File tree

4 files changed

+80
-37
lines changed

4 files changed

+80
-37
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,38 @@ import { TestBed } from "@angular/core/testing";
44
import { provideTranslateService, TranslateService, Translation } from "@ngx-translate/core";
55
import {
66
provideTranslateHttpLoader,
7+
provideTranslateMultiHttpLoader,
78
TranslateHttpLoader,
89
TranslateHttpLoaderConfig,
10+
TranslateMultiHttpLoaderConfig,
911
} from "../public-api";
1012
import { MarkerInterceptor } from "../test-helper/marker-interceptor";
1113

1214
describe("TranslateHttpLoader (HttpClient)", () => {
1315
let translate: TranslateService;
1416
let http: HttpTestingController;
1517

16-
const prepare = (config: Partial<TranslateHttpLoaderConfig> = {}) => {
18+
const prepareMulti = (config: Partial<TranslateMultiHttpLoaderConfig> = {}) => {
19+
TestBed.configureTestingModule({
20+
providers: [
21+
MarkerInterceptor,
22+
{
23+
provide: HTTP_INTERCEPTORS,
24+
useExisting: MarkerInterceptor,
25+
multi: true,
26+
},
27+
provideHttpClient(withInterceptorsFromDi()),
28+
provideHttpClientTesting(),
29+
provideTranslateService(),
30+
provideTranslateMultiHttpLoader(config),
31+
],
32+
});
33+
34+
translate = TestBed.inject(TranslateService);
35+
http = TestBed.inject(HttpTestingController);
36+
};
37+
38+
const prepareSingle = (config: Partial<TranslateHttpLoaderConfig> = {}) => {
1739
TestBed.configureTestingModule({
1840
providers: [
1941
MarkerInterceptor,
@@ -38,29 +60,29 @@ describe("TranslateHttpLoader (HttpClient)", () => {
3860
});
3961

4062
it("should be able to provide TranslateHttpLoader", () => {
41-
prepare();
63+
prepareSingle();
4264
expect(TranslateHttpLoader).toBeDefined();
4365
expect(translate.currentLoader).toBeDefined();
4466
expect(translate.currentLoader instanceof TranslateHttpLoader).toBeTruthy();
4567
});
4668

4769
describe("Config", () => {
4870
it("uses prefix", () => {
49-
prepare({ prefix: "XXXX/" });
71+
prepareSingle({ prefix: "XXXX/" });
5072
translate.use("en");
5173
http.expectOne("XXXX/en.json").flush({});
5274
expect(true).toBeTruthy(); // http.expectOne() is not detected by jasmine...
5375
});
5476

5577
it("uses suffix", () => {
56-
prepare({ suffix: ".XXXX" });
78+
prepareSingle({ suffix: ".XXXX" });
5779
translate.use("en");
5880
http.expectOne("/assets/i18n/en.XXXX").flush({});
5981
expect(true).toBeTruthy(); // http.expectOne() is not detected by jasmine...
6082
});
6183

6284
it("uses cache buster", () => {
63-
prepare({ enforceLoading: true });
85+
prepareSingle({ enforceLoading: true });
6486
translate.use("en");
6587
http.expectOne((req) =>
6688
req.url.startsWith("/assets/i18n/en.json?enforceLoading="),
@@ -70,7 +92,7 @@ describe("TranslateHttpLoader (HttpClient)", () => {
7092
});
7193

7294
it("should be able to get translations", () => {
73-
prepare();
95+
prepareSingle();
7496

7597
translate.use("en");
7698

@@ -92,7 +114,7 @@ describe("TranslateHttpLoader (HttpClient)", () => {
92114
});
93115

94116
it("should trigger MarkerInterceptor when loading translations", () => {
95-
prepare();
117+
prepareSingle();
96118

97119
translate.use("en").subscribe();
98120

@@ -103,7 +125,7 @@ describe("TranslateHttpLoader (HttpClient)", () => {
103125
});
104126

105127
it("should be able to reload a lang", () => {
106-
prepare();
128+
prepareSingle();
107129

108130
translate.use("en");
109131

@@ -124,7 +146,7 @@ describe("TranslateHttpLoader (HttpClient)", () => {
124146
});
125147

126148
it("should be able to reset a lang", (done: DoneFn) => {
127-
prepare();
149+
prepareSingle();
128150

129151
translate.use("en");
130152
spyOn(http, "expectOne").and.callThrough();

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

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

6+
export interface TranslateHttpLoaderConfig {
7+
prefix?: string;
8+
suffix?: string;
9+
showLog?: boolean;
10+
enforceLoading: boolean;
11+
useHttpBackend: boolean;
12+
}
13+
614
export interface TranslateHttpLoaderResource {
715
prefix: string;
816
suffix?: string;
917
showLog?: boolean;
1018
}
1119

12-
export interface TranslateHttpLoaderConfig {
13-
/**
14-
* @deprecated Use `resources` instead. `prefix` and `suffix` will be removed in a future version.
15-
*/
16-
prefix?: string;
17-
/**
18-
* @deprecated Use `resources` instead. `prefix` and `suffix` will be removed in a future version.
19-
*/
20-
suffix?: string;
20+
export interface TranslateMultiHttpLoaderConfig {
2121
showLog?: boolean;
2222
resources: (string | TranslateHttpLoaderResource)[];
2323
enforceLoading: boolean;
2424
useHttpBackend: boolean;
2525
}
2626

27-
export const TRANSLATE_HTTP_LOADER_CONFIG = new InjectionToken<Partial<TranslateHttpLoaderConfig>>(
28-
"TRANSLATE_HTTP_LOADER_CONFIG",
29-
);
27+
export const TRANSLATE_HTTP_LOADER_CONFIG = new InjectionToken<
28+
Partial<TranslateMultiHttpLoaderConfig>
29+
>("TRANSLATE_HTTP_LOADER_CONFIG");
3030

3131
@Injectable()
3232
export class TranslateHttpLoader implements TranslateLoader {
3333
private http: HttpClient;
34-
private config: TranslateHttpLoaderConfig;
34+
private config: TranslateMultiHttpLoaderConfig;
3535

3636
constructor() {
3737
this.config = {
38-
prefix: "/assets/i18n/",
39-
suffix: ".json",
4038
resources: [],
4139
enforceLoading: false,
4240
useHttpBackend: false,
41+
showLog: false,
4342
...inject(TRANSLATE_HTTP_LOADER_CONFIG),
4443
};
4544

@@ -54,18 +53,15 @@ export class TranslateHttpLoader implements TranslateLoader {
5453
public getTranslation(lang: string): Observable<TranslationObject> {
5554
const cacheBuster = this.config.enforceLoading ? `?enforceLoading=${Date.now()}` : "";
5655

57-
if ((!this.config.resources || this.config.resources.length <= 0) && this.config.prefix)
58-
this.config.resources = [{ prefix: this.config.prefix, suffix: this.config.suffix }];
59-
6056
const requests = this.config.resources.map((resource) => {
6157
let path: string;
6258

6359
if (typeof resource === "string") path = `${resource}${lang}.json`;
6460
else path = `${resource.prefix}${lang}${resource.suffix ?? ".json"}`;
6561

6662
return this.http.get<TranslationObject>(`${path}${cacheBuster}`).pipe(
67-
catchError((err) => {
68-
if (this.config.showLog || (resource as TranslateHttpLoaderResource).showLog) {
63+
catchError((err: HttpErrorResponse) => {
64+
if (this.config.showLog) {
6965
console.error(`Error loading translation for ${lang}:`, err);
7066
}
7167
return of({});
@@ -80,7 +76,32 @@ export class TranslateHttpLoader implements TranslateLoader {
8076
}
8177

8278
export function provideTranslateHttpLoader(
83-
config: Partial<TranslateHttpLoaderConfig> = {},
79+
config: Partial<TranslateHttpLoaderConfig | TranslateMultiHttpLoaderConfig> = {},
80+
): Provider[] {
81+
// If config already has resources, it's a multi-config, pass it through
82+
if ("resources" in config && config.resources) {
83+
return provideTranslateMultiHttpLoader(config as Partial<TranslateMultiHttpLoaderConfig>);
84+
}
85+
86+
// Otherwise, convert single config to multi-config
87+
const singleConfig = config as Partial<TranslateHttpLoaderConfig>;
88+
const multiConfig: Partial<TranslateMultiHttpLoaderConfig> = {
89+
showLog: singleConfig.showLog ?? false,
90+
enforceLoading: singleConfig.enforceLoading ?? false,
91+
useHttpBackend: singleConfig.useHttpBackend ?? false,
92+
resources: [
93+
{
94+
prefix: singleConfig.prefix ?? "/assets/i18n/",
95+
suffix: singleConfig.suffix ?? ".json",
96+
},
97+
],
98+
};
99+
100+
return provideTranslateMultiHttpLoader(multiConfig);
101+
}
102+
103+
export function provideTranslateMultiHttpLoader(
104+
config: Partial<TranslateMultiHttpLoaderConfig> = {},
84105
): Provider[] {
85106
const useBackend = config.useHttpBackend ?? false;
86107

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"multi": {
3-
"loader": "Dieser Text wurde aus /i18n/another/de.json geladen"
4-
}
2+
"multi": {
3+
"loader": "Dieser Text wurde aus /i18n/another/de.json geladen"
4+
}
55
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"multi": {
3-
"loader": "This text was loaded from /i18n/another/en.json"
4-
}
2+
"multi": {
3+
"loader": "This text was loaded from /i18n/another/en.json"
4+
}
55
}

0 commit comments

Comments
 (0)