1

I'm using this @ngx-translate/core and @ngx-translate/http-loader i18n service and it works fine in templates (.html)

{{'title'|translate}}

Now I want to translate in my component typescript file (.ts) but I don't know how to use it.

I inject the translateService in contructor

constructor(private translate: TranslateService) {}

.component.ts

//update employee
editClick(item){
console.log(item);
this.emp=item;
this.ModalTitle="Edit Employee";  <-- Need to translate this using pipe
this.ActivateAddEditEmpComp=true;
}

//delete employee
deleteClick(item){
if(confirm('Are you sure??')){  <-- Need to translate this using pipe
  this.service.deleteEmployee(item.EmployeeId).subscribe(data=>{
    alert(data.toString());
    this.refreshEmpList();
  })
}
}
Chellappan வ
  • 23,645
  • 3
  • 29
  • 60
Anonymous
  • 11
  • 4

2 Answers2

1

First import translate service

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

Injext in the constructor:

constructor(
    public translate: TranslateService,
  )

And you can create a function or however you wanted to use inline you can do something like this.

translationMsg(key) {
    this.translate.get('key').subscribe((data) => {
      return data;
    });
  }

For your use case you can do this

this.ModalTitle = this.translationMsg('Edit_Employee');
Manish Patidar
  • 526
  • 4
  • 14
1

you can use instant or you can use the get method and later can subscribe.

this.ModalTitle = this.translate.instant('EDIT_EMPLOYEE') 

Second Approach :

this.translate.get('EDIT_EMPLOYEE').subscribe((data) => {
  this.ModalTitle = data;
});
konowo7640
  • 23
  • 1
  • 6