[英]ngx-translate with multiple translations at once and dynamic text on ts file
I would like to implement dynamic elements into multiple translations with ngx-translate, in order to fuse this solution (multiple translations) : 我想使用ngx-translate将动态元素实现为多种翻译,以便融合此解决方案 (多种翻译):
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);
});
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. 虽然我知道对
TranslationService
上的get
函数所做的更改将多个翻译字符串作为输入,但我有一个特定的用例,在翻译后,我需要为每个代码运行代码(即,我添加了一个.map
到this.translate.get(...
然后在其中运行一些代码。
Hope this helps. 希望这可以帮助。
根据TranslateService
get
方法的类型注释,您可以将interpolateParams
指定为第二个参数。
TranslateService.get(key: string | string[], interpolateParams?: Object): Observable<any>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.