Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AG-Grid: Translate headerName with ngx-translate (angular)

We are using Angular to visualise an AG grid. We want the headers of the ag grid to be translated in the language of the user.

Ag grid code:

<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>

The same way we can translate a value on our html page itself:

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

I noticed, when the translations are being loaded, ag grid does not notice when the translations are loaded. The value on the html page itself however gets translated.

Extra info: The translate pipe of ngx-translate is an "impure" pipe, which means its value can change (eg. when all translations are loaded)

The same way, when using a headerName without a translation, it does not get updated:

Ag grid code:

<ag-grid-angular class="ag-theme-material" [rowData]="productionOrders">
  <ag-grid-column [headerName]="bindedString" field="entry"></ag-grid-column>
</ag-grid-angular>
this.lazyString = 'test-1';
setTimeout(() => {
  this.lazyString = 'test-2';
}, 3000);

The header name is never updated to "test-2"

like image 706
saibot Avatar asked Mar 05 '23 07:03

saibot


2 Answers

demo.component.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>

demo.component.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);
  }
}
like image 171
TurboYang Avatar answered Mar 07 '23 20:03

TurboYang


Header Value Getters

Use headerValueGetter instead of colDef.headerName to allow dynamic header names.

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;
    });
}

Sample from doc

{ 
    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!';
    }
}
like image 22
un.spike Avatar answered Mar 07 '23 21:03

un.spike