diff --git a/package.json b/package.json index f36c0f1..e73d484 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "@angular/platform-browser-dynamic": "^6.0.0", "@ngx-translate/core": ">=10.0.0", "core-js": "^2.5.4", - "deepmerge": "^2.1.1", + "deepmerge": "2.1.1", "rxjs": "^6.1.0", "zone.js": "^0.8.26" }, diff --git a/projects/ngx-translate/multi-http-loader/package.json b/projects/ngx-translate/multi-http-loader/package.json index cda1c37..aa712f9 100644 --- a/projects/ngx-translate/multi-http-loader/package.json +++ b/projects/ngx-translate/multi-http-loader/package.json @@ -19,7 +19,7 @@ "url": "https://github.com/denniske/ngx-translate-multi-http-loader/issues" }, "dependencies": { - "deepmerge": "^2.1.1" + "deepmerge": "2.1.1" }, "peerDependencies": { "@ngx-translate/core": ">=10.0.0", diff --git a/projects/ngx-translate/multi-http-loader/src/lib/multi-http-loader.ts b/projects/ngx-translate/multi-http-loader/src/lib/multi-http-loader.ts index d8fd3b9..5399b94 100644 --- a/projects/ngx-translate/multi-http-loader/src/lib/multi-http-loader.ts +++ b/projects/ngx-translate/multi-http-loader/src/lib/multi-http-loader.ts @@ -1,7 +1,7 @@ import {HttpClient} from "@angular/common/http"; import {TranslateLoader} from "@ngx-translate/core"; -import {Observable, forkJoin} from "rxjs"; -import {map} from "rxjs/operators"; +import {Observable, forkJoin, of} from "rxjs"; +import {catchError, map} from "rxjs/operators"; import merge from 'deepmerge'; @@ -18,7 +18,11 @@ export class MultiTranslateHttpLoader implements TranslateLoader { public getTranslation(lang: string): Observable { const requests = this.resources.map(resource => { - return this.http.get(resource.prefix + lang + resource.suffix); + const path = resource.prefix + lang + resource.suffix; + return this.http.get(path).pipe(catchError(res => { + console.error("Could not find translation file:", path); + return of({}); + })); }); return forkJoin(requests).pipe(map(response => merge.all(response))); } diff --git a/projects/ngx-translate/multi-http-loader/tests/multi-http-loader.spec.ts b/projects/ngx-translate/multi-http-loader/tests/multi-http-loader.spec.ts index 3bc2bd6..9e1a05b 100644 --- a/projects/ngx-translate/multi-http-loader/tests/multi-http-loader.spec.ts +++ b/projects/ngx-translate/multi-http-loader/tests/multi-http-loader.spec.ts @@ -140,15 +140,7 @@ describe('MultiTranslateHttpLoader - Multiple Translation Files', () => { it('should be able to get translations from multiple files', () => { translate.use('en'); - // this will request the translation from the backend because we use a static files loader for TranslateService - translate.get('TEST').subscribe((res: string) => { - expect(res).toEqual('This is a test (core)'); - }); - translate.get('TEST-SHARED').subscribe((res: string) => { - expect(res).toEqual('This is a test (shared)'); - }); - - // mock response after the xhr request, otherwise it will be undefined + // mock response, otherwise it will be undefined http.expectOne('/assets/i18n/core/en.json').flush({ "TEST": "This is a test (core)", "TEST2": "This is another test (core)", @@ -164,18 +156,30 @@ describe('MultiTranslateHttpLoader - Multiple Translation Files', () => { } }); - // this will request the translation from downloaded translations without making a request to the backend - translate.get('TEST2').subscribe((res: string) => { - expect(res).toEqual('This is another test (core)'); - }); - translate.get('TEST2-SHARED').subscribe((res: string) => { - expect(res).toEqual('This is another test (shared)'); + expect(translate.instant('TEST2')).toEqual('This is another test (core)'); + expect(translate.instant('TEST2-SHARED')).toEqual('This is another test (shared)'); + expect(translate.instant('DEEP')).toEqual({ + "some": "thing", + "another": "something" }); - translate.get('DEEP').subscribe((res: any) => { - expect(res).toEqual({ - "some": "thing", - "another": "something" - }); + }); + + it('should be able to get translations from multiple files even if some are missing', () => { + translate.use('en'); + + // mock response, otherwise it will be undefined + http.expectOne('/assets/i18n/core/en.json').flush({ + "TEST": "This is a test (core)", + "TEST2": "This is another test (core)", + "DEEP": { + "some": "thing" + } }); + http.expectOne('/assets/i18n/shared/en.json').error(new ErrorEvent('network error')); + + expect(translate.instant('TEST')).toEqual('This is a test (core)'); + expect(translate.instant('TEST2')).toEqual('This is another test (core)'); + expect(translate.instant('TEST-SHARED')).toEqual('TEST-SHARED'); + expect(translate.instant('TEST2-SHARED')).toEqual('TEST2-SHARED'); }); });