I'm using this @ngx-translate/core i18n service and it works fine in templates (.html) with this syntax:
{{'my.i18n.key'|translate}}
Now I want to translate something in my component typescript file (.ts) but I don't know how to use it.
I can create translate object in my constructor:
constructor(private translate: TranslateService) {}
and now how to translate 'my.i18n.key' ?
What is ngx-translate? ngx-translate is the library for internationalization (i18n) and localization in Angular. It simplifies your Angular application to work for localization. It's easy to set up and use in an Angular application.
NGX-Translate is an internationalization library for Angular. It lets you translate for your content in different languages and switch between them easily. Create a new Angular project using the following NPM command: ng new ngx-translate.
The service also contains a method called translate. It is later called by the pipe to get the translation for a specific key. It always uses the language specified in the 'language'-variable of the service, to lookup the translation. That's it.
To translate something in your typescript file, do the following
constructor(private translate: TranslateService) {}
then use like this wherever you need to translate
this.translate.instant('my.i18n.key')
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