Angular Ngx-translate Usage In Typescript


Answer :

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

From the doc on github:

get(key: string|Array, interpolateParams?: Object): Observable: Gets the translated value of a key (or an array of keys) or the key if the value was not found

try in your controller/class:

constructor(private translate: TranslateService) {     let foo:string = this.translate.get('myKey'); } 

To translate in Typscript file ,do follow code

Import in header

 import { TranslateService } from '@ngx-translate/core'; 

In constructor declare as

public translate: TranslateService 

Suppose the JSON file looks like this

{     "menu":{         "Home": "Accueil"             } } 

Declare the below code in constructor.

Note: Key stands for your main key value that used in language.json file (Here it is "menu")

 this.translate.get('menu').subscribe((data:any)=> {        console.log(data);       }); 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable