简体   繁体   English

如何将 i18n 与 Angular mat-table 一起使用

[英]How to use i18n with Angular mat-table

I cannot seem to translate dynamic column titles using i18n and Angular's mat-table.我似乎无法使用 i18n 和 Angular 的 mat-table 翻译动态列标题。

I tried using i18n-title [title]="field.i18n" and i18n-header [header]="field.18n" and i18n-table-header table-header="field.i18n" without any success.我尝试使用 i18n-title [title]="field.i18n" 和 i18n-header [header]="field.18n" 和 i18n-table-header table-header="field.i18n" 没有任何成功。

 <table mat-table [dataSource]="dataSource"> <ng-container *ngFor="let field of tableData" [matColumnDef]="field.name"> <th mat-header-cell *matHeaderCellDef [i18n]="field.i18n"> {{field.name}} </th> <td mat-cell *matCellDef="let element"> {{element[field.dbField]}} </td> </ng-container> <tr mat-header-row *matHeaderRowDef="getDisplayedColumns();"></tr> <tr mat-row *matRowDef="let row; columns: getDisplayedColumns();" ></tr> </table>

I would expect the column title to come from field.name using i18n templates.我希望列标题来自使用 i18n 模板的 field.name。

i18n is not a directive. i18n不是指令。 It doesn't exist at runtime.它在运行时不存在。

It's just an attribute used at compile time to extract and reinsert the static values to translate from/into the template.它只是在编译时用于提取和重新插入静态值以从/转换到模板的属性。

If you're trying to translate a key at runtime with Angular i18n, then too bad, because it's not supported.如果您尝试在运行时使用 Angular i18n 转换密钥,那就太糟糕了,因为它不受支持。

I think you could use the localeId of the app, and create the labels in the component.我认为您可以使用应用程序的localeId ,并在组件中创建标签。 Is not nice but at least it works.不是很好,但至少它有效。

constructor(@Inject(LOCALE_ID) protected localeId: string) {
      console.log(this.localeId);
}

And in your component you can assign the names of the headers depending the locale在您的组件中,您可以根据语言环境分配标题的名称

if (this.localeId == 'de') {
/// Your code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM