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'));
Considering that the code is in async
function, it can be
this.errorString = await translate.get('ERROR').toPromise();
Otherwise subscribe(...)
should be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With