您可以使用ngx-translate-core 进行翻译(阅读the doc 进行安装)。
在您的组件中,您可以尝试这样的事情:
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
import { Component } from '@angular/core';
@Component({
selector: 'app-contact',
templateUrl: './contact.component.html'
})
export class YourComponent {
settings: any;
constructor(private translateService: TranslateService) {
// we will set the default lang to 'fr' but this part is generally done
// in your app.component.
this.translateService.setDefaultLang('fr');
this.translateService.use('fr');
// we launch manually a table settings here with the default lang setted
this.initTableSettings();
// listening on the lang changements
this.translateService.onLangChange.subscribe((event: LangChangeEvent) => {
this.translateService.use(event.lang);
// every time the languages will change, we reload the settings
this.initTableSettings();
});
}
initTableSettings(): void {
this.settings = {
actions: {
add: false,
edit: false,
delete: false
},
columns: {
date: {
title: this.translateService.instant('column_date')
},
sent: {
title: this.translateService.instant('column_sent')
},
billed: {
title: this.translateService.instant('column_billed')
}
}
};
}
}
在您的 i18n 文件中(此处为 fr.json):
{
"column_date": "<< your traduction in french here >>",
"column_sent": "<< your traduction in french here >>",
"column_billed": "<< your traduction in french here >>"
}
您可以在文档中看到如何安装和配置 Angular 的 TranslateService,基本上如何在您的应用模块中导入该服务,将您的 i18n 文件放在哪里等等。