Skip to content

Commit

Permalink
fix(Core): Ignore missing translation files, but show error in console.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Ignore missing translation files, but show error in console.

fix #3
  • Loading branch information
denniske committed Jan 29, 2019
1 parent 0ef8984 commit bc78df8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-translate/multi-http-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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';


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

public getTranslation(lang: string): Observable<any> {
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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand All @@ -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');
});
});

0 comments on commit bc78df8

Please sign in to comment.