Ionic ngx translate


#1

Hi, I am currently using ngx translate which works very well in html with “{{‘try’ | translate}}” and the file en.json “” try “:” test “” however I can not to use in the TS, for an alert!

On the doc (https://ionicframework.com/docs/developer-resources/ng2-translate/) I tried those he says all down on that but I can not … could you help me?

Thanks youCapture


#2
constructor(translate: TranslateService) {
}

showTest() {
    let myTitle: string = this.translate.instant('try'); // try = your key 
   
    let confirm: this.alertCtrl.create({
              title: myTitle,
              ...

#3

Thanks for your help ! “Property ‘translate’ does not exist on type ‘TryPage’”
Capture


#4

That works for me:
Page.ts
import { TranslateService } from ‘@ngx-translate/core’;

test= ‘’;
constructor(private translate: TranslateService) {
this.translate.get(‘test’).subscribe((result) => {
this.test = result;
});
}

Page.module
import { TranslateModule } from ‘@ngx-translate/core’;

imports: [

TranslateModule


#5

Did you declare it in your constructor? Don’t forget the import too

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

constructor(translate: TranslateService) {
}

#6

Yeah i did but still this error …


#7

I get the error … but the alert is not in the constructor


#8

Maybe a keyword like private is missing?

Full answer:

import { TranslateService } from '@ngx-translate/core';
  
constructor(private translate: TranslateService) {
}

showTest() {
  let myTitle: string = this.translate.instant('try'); // try = your key 

  let confirm: this.alertCtrl.create({
          title: myTitle,
          ...

#9

Yeeees! it works ! thank you very much it’s nice!


#10

Cool cool cool :slight_smile: