Skip to content

Commit

Permalink
feat(TranslateService): added new merging option when setting transla…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
Pawlu Frendo authored and ocombe committed Aug 13, 2016
1 parent 2480795 commit 4a8fb83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,12 @@ export class TranslateService {
* @param lang
* @param translations
*/
public setTranslation(lang: string, translations: Object): void {
this.translations[lang] = translations;
public setTranslation(lang: string, translations: Object, shouldMerge: boolean = false): void {
if (shouldMerge && this.translations[lang]) {
Object.assign(this.translations[lang], translations);
} else {
this.translations[lang] = translations;
}
this.updateLangs();
}

Expand Down
12 changes: 12 additions & 0 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ export function main() {
});
});

it("shouldn't override the translations if you set the translations twice ", (done: Function) => {
translate.setTranslation('en', {"TEST": "This is a test"}, true);
translate.setTranslation('en', {"TEST2": "This is a test"}, true);
translate.use('en');

translate.get('TEST').subscribe((res: string) => {
expect(res).toEqual('This is a test');
expect(connection).not.toBeDefined();
done();
});
});

it("shouldn't do a request to the backend if you set the translation yourself", (done: Function) => {
translate.setTranslation('en', {"TEST": "This is a test"});
translate.use('en');
Expand Down

0 comments on commit 4a8fb83

Please sign in to comment.