Skip to content

Commit

Permalink
fix(TranslateService): addLangs should append langs, not replace them
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Aug 20, 2016
1 parent 2f5bd04 commit 98bb430
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
9 changes: 7 additions & 2 deletions bundles/ng2-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,15 @@ System.registerDynamic("src/translate.service", ["@angular/core", "rxjs/Observab
return this.langs;
};
TranslateService.prototype.addLangs = function(langs) {
Object.assign(this.langs, langs);
var _this = this;
langs.forEach(function(lang) {
if (_this.langs.indexOf(lang) === -1) {
_this.langs.push(lang);
}
});
};
TranslateService.prototype.updateLangs = function() {
Object.assign(this.langs, Object.keys(this.translations));
this.addLangs(Object.keys(this.translations));
};
TranslateService.prototype.getParsedResult = function(translations, key, interpolateParams) {
var res;
Expand Down
8 changes: 6 additions & 2 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,18 @@ export class TranslateService {
* Add available langs
*/
public addLangs(langs: Array<string>): void {
Object.assign(this.langs, langs);
langs.forEach((lang: string) => {
if(this.langs.indexOf(lang) === -1) {
this.langs.push(lang);
}
});
}

/**
* Update the list of available langs
*/
private updateLangs(): void {
Object.assign(this.langs, Object.keys(this.translations));
this.addLangs(Object.keys(this.translations));
}

/**
Expand Down
15 changes: 13 additions & 2 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,20 @@ export function main() {
});

it('should be able to add new langs', () => {
translate.addLangs(['en', 'pl']);
translate.addLangs(['pl', 'es']);
expect(translate.getLangs()).toEqual(['pl', 'es']);
translate.addLangs(['fr']);
expect(translate.getLangs()).toEqual(['pl', 'es', 'fr']);

expect(translate.getLangs()).toEqual(['en', 'pl']);
// this will request the translation from the backend because we use a static files loader for TranslateService
translate.use('en').subscribe((res: string) => {
expect(translate.getLangs()).toEqual(['pl', 'es', 'fr', 'en']);
translate.addLangs(['de']);
expect(translate.getLangs()).toEqual(['pl', 'es', 'fr', 'en', 'de']);
});

// mock response after the xhr request, otherwise it will be undefined
mockBackendResponse(connection, '{"TEST": "This is a test"}');
});

it('should be able to get the browserLang', () => {
Expand Down

0 comments on commit 98bb430

Please sign in to comment.