Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An equivalent of async pipe in typescript

I'm using ng-translate in Ionic2, which provides me with this method for translating strings in the code. Currently, I have to use the service like this:

translate.get('ERROR').subscribe((res: string) => {
   //The string with code 'ERROR' is translated in res
   this.errorString = res;
});
....
//Later on, when error happens:
alert(this.errorString);

I have a lot of strings, alerts, and notifications in many files. Subscribing to a get method's observable for each of them is very tedious. In html, one can easily avoid this buy using the async pipe, or in this case translate pipe, which doesn't require explicit subscription to the observable:

<div>{{ 'ERROR' | translate}}</div>

Is there any way that I can do this with the same level of simplicity for the strings that are in the typescript file? for example ideally, I would like to have a shorthand to suscribe that achieves this:

   alert(idealTranslateFunction('ERROR'));
like image 862
Ari Avatar asked Sep 03 '25 05:09

Ari


1 Answers

Considering that the code is in async function, it can be

this.errorString = await translate.get('ERROR').toPromise();

Otherwise subscribe(...) should be used.

like image 181
Estus Flask Avatar answered Sep 04 '25 20:09

Estus Flask