2

I would like to implement dynamic elements into multiple translations with ngx-translate, in order to fuse this solution (multiple translations) :

this.translate.get(['HOME', 'MY_ACCOUNT', 'CHANGE_PASSWORD']).subscribe(res => {
      showToast(res.HOME,res.MY_ACCOUNT,res.CHANGE_PASSWORD);
});

with that one (dynamic text) :

this.translate.get('HOME', {value: 'test_HOME'}).subscribe(res => {
      showToast(res);
});
apalau
  • 21
  • 1
  • 4

2 Answers2

0

not sure if you came up with your own solution by now, but I did something like:

public translateTableHeadings(stringArr: string[] = []): Observable<string[]> {
        const sub = new Subject<string[]>();

        const obs$ = Observable.from(stringArr);

        obs$
            .map(aString=>
                this.translate.get(aString)
            )
            .toArray()
            .takeUntil(sub)
            .subscribe(translatedStrings=> {
                sub.next(translatedStrings);
                sub.complete();
            });

        return sub.asObservable();
}

While I'm aware of the changes done to the get function on the TranslationService catering for multiple translation strings as an input, I had a specific use case where I needed to run code for each one after it was translated (ie, I added a .map to the this.translate.get(... and ran some code in there.

Hope this helps.

jarodsmk
  • 1,876
  • 2
  • 21
  • 40
0

asper the type annotation of the get method in TranslateService you can specify interpolateParams as the second parameter.

TranslateService.get(key: string | string[], interpolateParams?: Object): Observable<any>
Laszlo
  • 2,225
  • 19
  • 22