Skip to content

Commit

Permalink
feat(TranslateService): added a new addLangs method
Browse files Browse the repository at this point in the history
  • Loading branch information
ocombe committed Aug 13, 2016
1 parent 146717f commit 655c607
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Or use the `TranslatePipe` in any template:
- `use(lang: string): Observable<any>`: Changes the lang currently used
- `getTranslation(lang: string): Observable<any>`: Gets an object of translations for a given language with the current loader
- `setTranslation(lang: string, translations: Object, shouldMerge: boolean = false)`: Manually sets an object of translations for a given language, set `shouldMerge` to true if you want to append the translations instead of replacing them
- `addLangs(langs: Array<string>)`: Add new langs to the list
- `getLangs()`: Returns an array of currently available langs
- `get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Gets the translated value of a key (or an array of keys)
- `instant(key: string|Array<string>, interpolateParams?: Object): string|Object`: Gets the instant translated value of a key (or an array of keys). /!\ This method is **synchronous** and the default file loader is asynchronous. You are responsible for knowing when your translations have been loaded and it is safe to use this method. If you are not sure then you should use the `get` method instead.
Expand Down
16 changes: 13 additions & 3 deletions bundles/ng2-translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,25 @@ System.registerDynamic("src/translate.service", ["@angular/core", "rxjs/Observab
});
return this.pending;
};
TranslateService.prototype.setTranslation = function(lang, translations) {
this.translations[lang] = translations;
TranslateService.prototype.setTranslation = function(lang, translations, shouldMerge) {
if (shouldMerge === void 0) {
shouldMerge = false;
}
if (shouldMerge && this.translations[lang]) {
Object.assign(this.translations[lang], translations);
} else {
this.translations[lang] = translations;
}
this.updateLangs();
};
TranslateService.prototype.getLangs = function() {
return this.langs;
};
TranslateService.prototype.addLangs = function(langs) {
Object.assign(this.langs, langs);
};
TranslateService.prototype.updateLangs = function() {
this.langs = Object.keys(this.translations);
Object.assign(this.langs, Object.keys(this.translations));
};
TranslateService.prototype.getParsedResult = function(translations, key, interpolateParams) {
var res;
Expand Down
19 changes: 5 additions & 14 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export class TranslateService {
private pending: any;
private translations: any = {};
private defaultLang: string;
private langs: Array<string>;
private availableLangs: Array<string>;
private langs: Array<string> = [];
private parser: Parser = new Parser();

/**
Expand Down Expand Up @@ -154,27 +153,19 @@ export class TranslateService {
return this.langs;
}

/**
* Returns an array of available langs
* @returns {Array<string>}
*/
public getAvailableLangs(): Array<string> {
return this.availableLangs;
}

/**
* @param langs
* Set available langs
* Add available langs
*/
public setAvailableLangs(langs: Array<string>): void {
this.availableLangs = langs;
public addLangs(langs: Array<string>): void {
Object.assign(this.langs, langs);
}

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

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ export function main() {
mockBackendResponse(connection, '{"TEST": "This is a test"}');
});

it('should be able to set and get array of available languages', () => {
translate.setAvailableLangs(['en', 'pl']);
it('should be able to add new langs', () => {
translate.addLangs(['en', 'pl']);

expect(translate.getAvailableLangs()).toEqual(['en', 'pl']);
expect(translate.getLangs()).toEqual(['en', 'pl']);
});
});

Expand Down

0 comments on commit 655c607

Please sign in to comment.