How to use async and await with ng2-translate?


#1

I have used ng2-translate module for making the translation in my app. But as translate.get method returns observable I always need to subscribe it. To avoid this I want to make one common function in provider which will listen to message constant and returns translated value.
But it returns ‘undefined’, it doesn’t wait for making a translation, I think I can use async and await function. How can I do it?

Here is my code:

public translateString(messageConst):any{ this.translate.get(messageConst).subscribe(value => { return value; }); }


#2

Did you try .toPromise() on subscriber
Like
await this.translate.get(messageConst).toPromise()


#3

yes, I tried it but didn’t work.


#4

Can you show your code sample for the call