Skip to content

Commit

Permalink
fix(TranslateService): Don't throw when trying to use get too early
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Jan 17, 2016
1 parent 98f90c8 commit 2af0b91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,19 @@ export class TranslateService {
}

var getParsedResult = (translations: any, key: any) => {
if(!translations) {
return key;
}
var res: string;

if(key instanceof Array) {
let result: any = {};
for (var k of key) {
result[k] = getParsedResult(translations, k);
}
return result;
}
var res: string = this.parser.interpolate(translations[key], interpolateParams);

if(translations) {
res = this.parser.interpolate(translations[key], interpolateParams);
}

if(typeof res === 'undefined' && this.defaultLang && this.defaultLang !== this.currentLang) {
let translations: any = this.parser.flattenObject(this.translations[this.defaultLang]);
Expand All @@ -191,7 +193,12 @@ export class TranslateService {
return getParsedResult(this.parser.flattenObject(res), key);
});
} else {
let translations: any = this.parser.flattenObject(this.translations[this.currentLang]);
let translations: any;

if(this.translations[this.currentLang]) {
translations = this.parser.flattenObject(this.translations[this.currentLang]);
}

return Observable.of(getParsedResult(translations, key));
}
}
Expand Down
8 changes: 7 additions & 1 deletion tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function main() {
let injector: Injector;
let backend: MockBackend;
let translate: TranslateService;
var connection: MockConnection; // this will be set when a new connection is emitted from the backend.
let connection: MockConnection; // this will be set when a new connection is emitted from the backend.

var prepareStaticTranslate = (lang: string = 'en') => {
// this will load translate json files from src/public/i18n
Expand Down Expand Up @@ -93,6 +93,12 @@ export function main() {
mockBackendResponse('{}');
});

it("should return the key when you haven't defined any translation", () => {
translate.get('TEST').subscribe((res: string) => {
expect(res).toEqual('TEST');
});
});

it('should be able to get translations with params', () => {
prepareStaticTranslate();

Expand Down

0 comments on commit 2af0b91

Please sign in to comment.