Skip to content

Commit a49a8fa

Browse files
committed
feat(http-loader): ressources
1 parent 8022bd6 commit a49a8fa

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { HttpBackend, HttpClient } from "@angular/common/http";
22
import { inject, Injectable, InjectionToken, Provider } from "@angular/core";
3-
import { TranslateLoader, TranslationObject } from "@ngx-translate/core";
4-
import { Observable } from "rxjs";
3+
import { mergeDeep, TranslateLoader, TranslationObject } from "@ngx-translate/core";
4+
import { forkJoin, map, Observable } from "rxjs";
5+
6+
export interface TranslateHttpLoaderResource {
7+
prefix: string;
8+
suffix?: string;
9+
}
510

611
export interface TranslateHttpLoaderConfig {
712
prefix: string;
813
suffix: string;
14+
ressources: (string | TranslateHttpLoaderResource)[];
915
enforceLoading: boolean;
1016
useHttpBackend: boolean;
1117
}
@@ -23,6 +29,7 @@ export class TranslateHttpLoader implements TranslateLoader {
2329
this.config = {
2430
prefix: "/assets/i18n/",
2531
suffix: ".json",
32+
ressources: [],
2633
enforceLoading: false,
2734
useHttpBackend: false,
2835
...inject(TRANSLATE_HTTP_LOADER_CONFIG),
@@ -39,9 +46,24 @@ export class TranslateHttpLoader implements TranslateLoader {
3946
public getTranslation(lang: string): Observable<TranslationObject> {
4047
const cacheBuster = this.config.enforceLoading ? `?enforceLoading=${Date.now()}` : "";
4148

42-
return this.http.get(
49+
if (this.config.ressources.length > 0) {
50+
const requests = this.config.ressources.map((resource) => {
51+
let path: string;
52+
53+
if (typeof resource === "string") path = `${resource}${lang}.json`;
54+
else path = `${resource.prefix}${lang}${resource.suffix ?? ".json"}`;
55+
56+
return this.http.get(`${path}${cacheBuster}`);
57+
}) as Observable<TranslationObject>[];
58+
59+
return forkJoin(requests).pipe(
60+
map((response) => response.reduce((acc, curr) => mergeDeep(acc, curr), {})),
61+
) as Observable<TranslationObject>;
62+
}
63+
64+
return this.http.get<TranslationObject>(
4365
`${this.config.prefix}${lang}${this.config.suffix}${cacheBuster}`,
44-
) as Observable<TranslationObject>;
66+
);
4567
}
4668
}
4769

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const appConfig: ApplicationConfig = {
1515
prefix: "./i18n/",
1616
suffix: ".json",
1717
enforceLoading: true,
18+
ressources: [{ prefix: "./i18n/another/", suffix: ".json" }, { prefix: "./i18n/" }],
1819
}),
1920
}),
2021
],

projects/test-app/src/app/components/page-content/page-content.component.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<div class="title">
2-
<h1>{{ "demo.title" | translate }}</h1>
2+
<h1>
3+
{{ "demo.title" | translate }}
4+
</h1>
35
</div>
46
<div>
57
<h2>Simple translations without parameters</h2>
@@ -10,3 +12,8 @@ <h2>Simple translations without parameters</h2>
1012
</div>
1113

1214
<app-standalone-component />
15+
16+
<div class="multi-loader">
17+
<h2>Http-loader: Ressources</h2>
18+
<p>{{ "multi.loader" | translate }}</p>
19+
</div>

projects/test-app/src/styles.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ p,
6666
background-color: var(--mat-sys-surface-dim);
6767
border-radius: var(--mat-sys-corner-small);
6868
padding: var(--space) var(--space-small);
69+
70+
em {
71+
font-size: small;
72+
}
6973
}
7074
}
7175

0 commit comments

Comments
 (0)