I have a navigation bar, there have multiple languages, in the app.component
constructor(public translate: TranslateService) {
translate.addLangs(['en', 'fr']);
translate.setDefaultLang('en');
const browserLang = translate.getBrowserLang();
translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
}
and i have another home component, in the ngOnInit i have a table , table header is dynamic.
not working
ngOnInit() {
this.cols = [
{ field: 'productID', header: this.translateService.instant('productID') },
}
But it is working well in button click:
buttonClicked(){
this.cols = [
{ field: 'productID', header: this.translateService.instant('productID') },
}
console.log(this.translateService.instant('productID'));
}
another solution I have found is to do the below subscribe method in each component and get the translated value which key needs to translate
constructor( private translateService:TranslateService) {
this.translateService.use('fr');
this.translateService.get(
['productID',],
)
.subscribe(val => {
console.log( val['productID']);
});
}
sample : looking for better solution https://stackblitz.com/edit/ngx-translate-example-h6uypw?file=src/app/home/home.component.ts