1

I used ng2-translate to translate headings and texts in my angular2 web app. But I am now confused when I trying to translate texts that pass from .ts file.
for example, I can translate texts in html file as following.

<ion-row id="login-row">
      <ion-col>
        <ion-list>
          <ion-item>
            <ion-label stacked>{{ 'USERNAME' | translate }}</ion-label>
            <ion-input type="text" [(ngModel)]="username"></ion-input>
          </ion-item>
          <ion-item>
            <ion-label stacked>{{ 'PASSWORD' | translate }}</ion-label>
            <ion-input type="password" [(ngModel)]="password"></ion-input>
          </ion-item>
        </ion-list>
      </ion-col>
    </ion-row>

But if I have .ts file text as following. Then How I can translate those texts.

    doCheckbox() {
    let alert = this.alertCtrl.create();
    alert.setTitle('Please Select a Location');

     alert.addInput({
       type: 'radio',
       label: 'Option 1',
       value: 'opt1',
       checked: true
     });

     alert.addInput({
       type: 'radio',
       label: 'Option 2',
       value: 'opt2'
     });

    alert.addButton({
      text: 'OK',
      handler: data => {
        this.testRadioOpen = false;
        this.testRadioResult = data;
      }
    });
    alert.present();
  }

in above example, I want to translate texts like

'Please Select a Location', 'Option 1','Option 2', 'OK'...

If anyone has an idea to overcome this problem, help me. Thanks.

Dilanka Rathnayake
  • 660
  • 2
  • 11
  • 32

1 Answers1

1

Finally found the answer. ng2-translate API has given the answer (https://github.com/ocombe/ng2-translate#api)

ex:

this.translate.get("SPANISH")
        .subscribe((data: string) => {
          this.selectedLanguage = data;
        });

get() method returns the translated string or object (if we pass a string array)

you have to import ng2-translate as import {TranslateService} from 'ng2-translate;'and define it in your constructor.

Dilanka Rathnayake
  • 660
  • 2
  • 11
  • 32