Skip to content

Commit

Permalink
feat(TranslateService): the get method can now take an array of keys
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
ocombe committed Jan 12, 2016
1 parent 4549224 commit 79e9476
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ translate.setTranslation('en', {
- `getTranslation(lang: string): Observable<any>`: Gets an object of translations for a given language with the current loader
- `setTranslation(lang: string, translations: Object)`: Manually sets an object of translations for a given language
- `getLangs()`: Returns an array of currently available langs
- `get(key: string, interpolateParams?: Object): Observable<string>`: Gets the translated value of a key
- `get(key: string|Array<string>, interpolateParams?: Object): Observable<string|Object>`: Gets the translated value of a key (or an array of keys)
- `set(key: string, value: string, lang?: string)`:

### TranslatePipe
Expand Down
37 changes: 29 additions & 8 deletions src/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,39 @@ export class TranslateService {
}

/**
* Gets the translated value of a key
* Gets the translated value of a key (or an array of keys)
* @param key
* @param interpolateParams
* @returns {any}
* @returns {any} the translated key, or an object of translated keys
*/
public get(key: string, interpolateParams?: Object): Observable<string> {
public get(key: string|Array<string>, interpolateParams?: Object): Observable<string|any> {
// check if we are loading a new translation to use
if(this.pending) {
return this.pending.map((res: any) => this.parser.interpolate(res[key], interpolateParams) || key);
return this.pending.map((res: any) => {
var result: any,
getTranslation = (key: any) => this.parser.interpolate(res[key], interpolateParams) || key;
if(key instanceof Array) {
result = {};
for (var k of key) {
result[k] = getTranslation(k);
}
} else {
result = getTranslation(key);
}
return result;
});
} else {
return Observable.of(this.translations && this.translations[this.currentLang]
? this.parser.interpolate(this.translations[this.currentLang][key], interpolateParams) : key || key);
var result: any,
getTranslation = (key: any) => this.translations && this.translations[this.currentLang] ? this.parser.interpolate(this.translations[this.currentLang][key], interpolateParams) : key || key;
if(key instanceof Array) {
result = {};
for (var k of key) {
result[k] = getTranslation(k);
}
} else {
result = getTranslation(key);
}
return Observable.of(result);
}
}

Expand All @@ -181,8 +202,8 @@ export class TranslateService {
}

private changeLang(lang: string) {
this.currentLang = lang;
this.onLangChange.next({lang: lang, translations: this.translations[lang]});
this.currentLang = lang;
this.onLangChange.next({lang: lang, translations: this.translations[lang]});
}

}

0 comments on commit 79e9476

Please sign in to comment.