typescript AG网格:使用ngx平移(Angular )平移标题名称

wljmcqd8  于 2023-01-31  发布在  TypeScript
关注(0)|答案(4)|浏览(125)

我们正在使用Angular来可视化一个AG网格。我们希望AG网格的标题能被翻译成用户的语言。
Ag网格代码:

<ag-grid-angular class="ag-theme-material" [rowData]="productionOrders">
  <ag-grid-column [headerName]="'ORDERS.GRID.EntryCode' | translate" field="entry"></ag-grid-column>
  <ag-grid-column [headerName]="ORDERS.GRID.EntryDescription" field="entryDescription"></ag-grid-column>
</ag-grid-angular>

同样的方法,我们可以在我们的html页面本身翻译一个值:

<span>{{ 'ORDERS.Status' | translate}}</span>

我注意到,当翻译被加载时,ag grid并没有注意到翻译是什么时候加载的,但是html页面上的值本身得到了翻译。
额外信息:ngx-translate的翻译管道是一个"不纯"管道,这意味着它的值可以更改(例如,当加载所有翻译时)
同样,使用不带翻译的headerName时,它不会更新:
Ag网格代码:
一个二个一个一个
标头名称从未更新为"test-2"

bvhaajcl

bvhaajcl1#

演示.组件. html

<ag-grid-angular style="width: 100%; height: 100%;" class="ag-theme-material" [rowData]="rowData"
    [columnDefs]="columnDefs" (gridReady)="onGridReady($event)" [pagination]="true">
</ag-grid-angular>

演示组件.ts

import { Component } from '@angular/core';
import { ColDef, GridApi } from 'ag-grid-community';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.scss']
})
export class DemoComponent {
  private gridApi: GridApi = null;

  public columnDefs: ColDef[] = [
    { headerName: "Code", field: 'code', sortable: true, resizable: true, headerValueGetter: this.localizeHeader.bind(this) },
    { headerName: 'Version', field: 'version', sortable: true, resizable: true, headerValueGetter: this.localizeHeader.bind(this) },
    { headerName: 'IsEnabled', field: 'isEnabled', sortable: true, resizable: true, headerValueGetter: this.localizeHeader.bind(this) }
  ];

  public rowData: any[] = [];

  constructor(private translateService: TranslateService) {
    this.translateService.onLangChange.subscribe(() => {
      this.gridApi.refreshHeader();
    })
  }

  public onGridReady(parameters: any): void {
    this.gridApi = parameters.api;
  }

  public localizeHeader(parameters: ICellRendererParams): string {
    let headerIdentifier = parameters.colDef.field;
    return this.translateService.instant(headerIdentifier);
  }
}
o7jaxewo

o7jaxewo2#

标题值获取器

使用headerValueGetter代替colDef.headerName以允许动态标头名称。

private translator: TranslateService;
...
colDef.headerValueGetter : this.localizeHeader.bind(this)
....
localizeHeader(params){
    let headerIdentifier = params.colDef.field; // params.column.getColId();
    this.translator.get(headerIdentifier).map((res: string) => {
        return res;
    });
}

文件中的样本

{ 
    headerName: "Country", 
    field: "country", 
    width: 120, 
    enableRowGroup: true, 
    enablePivot: true, 
    headerValueGetter: countryHeaderValueGetter 
},

function countryHeaderValueGetter(params) {
    switch (params.location) {
        case 'csv': return 'CSV Country';
        case 'clipboard': return 'CLIP Country';
        case 'toolPanel': return 'TP Country';
        case 'columnDrop': return 'CD Country';
        case 'header': return 'H Country';
        default: return 'Should never happen!';
    }
}
5anewei6

5anewei63#

如果你想使用TurboYang的异步翻译加载解决方案,只需添加以下内容:

private destroy = new Subject<void>();

  constructor(private toastService: ToastService, private translate: TranslateService) {
    translate.onLangChange.pipe(takeUntil(this.destroy)).subscribe(() => {
      this.gridApi.refreshHeader();
    });
    translate.onDefaultLangChange.pipe(takeUntil(this.destroy)).subscribe(() => {
      this.gridApi.refreshHeader();
    });
  }

  ngOnDestroy(): void {
    this.destroy.next();
    this.destroy.complete();
  }
yhuiod9q

yhuiod9q4#

使用中的功能

getTr(key: string) {
    return this.translate.instant(key);
  }

列定义中的用法

{
    field: 'name', headerName: this.trService.getTr('NAME')
  },

相关问题