Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ngx-translate usage in typescript

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' ?

like image 923
NavCore Avatar asked Aug 24 '17 20:08

NavCore


People also ask

What is the use of NGX translate?

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.

What is the use of translate in Angular?

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.

What is the use of translate pipe in Angular?

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.


1 Answers

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') 
like image 168
GuyFromChennai Avatar answered Sep 22 '22 16:09

GuyFromChennai