Skip to content

Commit bc78df8

Browse files
committed
fix(Core): Ignore missing translation files, but show error in console.
BREAKING CHANGE: Ignore missing translation files, but show error in console. fix #3
1 parent 0ef8984 commit bc78df8

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@angular/platform-browser-dynamic": "^6.0.0",
1919
"@ngx-translate/core": ">=10.0.0",
2020
"core-js": "^2.5.4",
21-
"deepmerge": "^2.1.1",
21+
"deepmerge": "2.1.1",
2222
"rxjs": "^6.1.0",
2323
"zone.js": "^0.8.26"
2424
},

projects/ngx-translate/multi-http-loader/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"url": "https://github.com/denniske/ngx-translate-multi-http-loader/issues"
2020
},
2121
"dependencies": {
22-
"deepmerge": "^2.1.1"
22+
"deepmerge": "2.1.1"
2323
},
2424
"peerDependencies": {
2525
"@ngx-translate/core": ">=10.0.0",

projects/ngx-translate/multi-http-loader/src/lib/multi-http-loader.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {HttpClient} from "@angular/common/http";
22
import {TranslateLoader} from "@ngx-translate/core";
3-
import {Observable, forkJoin} from "rxjs";
4-
import {map} from "rxjs/operators";
3+
import {Observable, forkJoin, of} from "rxjs";
4+
import {catchError, map} from "rxjs/operators";
55
import merge from 'deepmerge';
66

77

@@ -18,7 +18,11 @@ export class MultiTranslateHttpLoader implements TranslateLoader {
1818

1919
public getTranslation(lang: string): Observable<any> {
2020
const requests = this.resources.map(resource => {
21-
return this.http.get(resource.prefix + lang + resource.suffix);
21+
const path = resource.prefix + lang + resource.suffix;
22+
return this.http.get(path).pipe(catchError(res => {
23+
console.error("Could not find translation file:", path);
24+
return of({});
25+
}));
2226
});
2327
return forkJoin(requests).pipe(map(response => merge.all(response)));
2428
}

projects/ngx-translate/multi-http-loader/tests/multi-http-loader.spec.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,7 @@ describe('MultiTranslateHttpLoader - Multiple Translation Files', () => {
140140
it('should be able to get translations from multiple files', () => {
141141
translate.use('en');
142142

143-
// this will request the translation from the backend because we use a static files loader for TranslateService
144-
translate.get('TEST').subscribe((res: string) => {
145-
expect(res).toEqual('This is a test (core)');
146-
});
147-
translate.get('TEST-SHARED').subscribe((res: string) => {
148-
expect(res).toEqual('This is a test (shared)');
149-
});
150-
151-
// mock response after the xhr request, otherwise it will be undefined
143+
// mock response, otherwise it will be undefined
152144
http.expectOne('/assets/i18n/core/en.json').flush({
153145
"TEST": "This is a test (core)",
154146
"TEST2": "This is another test (core)",
@@ -164,18 +156,30 @@ describe('MultiTranslateHttpLoader - Multiple Translation Files', () => {
164156
}
165157
});
166158

167-
// this will request the translation from downloaded translations without making a request to the backend
168-
translate.get('TEST2').subscribe((res: string) => {
169-
expect(res).toEqual('This is another test (core)');
170-
});
171-
translate.get('TEST2-SHARED').subscribe((res: string) => {
172-
expect(res).toEqual('This is another test (shared)');
159+
expect(translate.instant('TEST2')).toEqual('This is another test (core)');
160+
expect(translate.instant('TEST2-SHARED')).toEqual('This is another test (shared)');
161+
expect(translate.instant('DEEP')).toEqual({
162+
"some": "thing",
163+
"another": "something"
173164
});
174-
translate.get('DEEP').subscribe((res: any) => {
175-
expect(res).toEqual({
176-
"some": "thing",
177-
"another": "something"
178-
});
165+
});
166+
167+
it('should be able to get translations from multiple files even if some are missing', () => {
168+
translate.use('en');
169+
170+
// mock response, otherwise it will be undefined
171+
http.expectOne('/assets/i18n/core/en.json').flush({
172+
"TEST": "This is a test (core)",
173+
"TEST2": "This is another test (core)",
174+
"DEEP": {
175+
"some": "thing"
176+
}
179177
});
178+
http.expectOne('/assets/i18n/shared/en.json').error(new ErrorEvent('network error'));
179+
180+
expect(translate.instant('TEST')).toEqual('This is a test (core)');
181+
expect(translate.instant('TEST2')).toEqual('This is another test (core)');
182+
expect(translate.instant('TEST-SHARED')).toEqual('TEST-SHARED');
183+
expect(translate.instant('TEST2-SHARED')).toEqual('TEST2-SHARED');
180184
});
181185
});

0 commit comments

Comments
 (0)