How to translate mat-paginator in Angular 4?

后端 未结 7 2281
无人及你
无人及你 2020-12-02 22:10

Do you have any ideas how can I translate \"Items per page\" in Angular\'s mat-paginator tag? The mat-paginator is an element from Material Design.

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 22:52

    You can hack your way into MatPaginator._intl and put your string there using ngx-translate.

    forkJoin({
      itemsPerPageLabel: this.translate.get('paginator.itemsPerPageLabel'),
      nextPageLabel: this.translate.get('paginator.nextPageLabel'),
      previousPageLabel: this.translate.get('paginator.previousPageLabel'),
      firstPageLabel: this.translate.get('paginator.firstPageLabel'),
      lastPageLabel: this.translate.get('paginator.lastPageLabel'),
    }).subscribe(values => {
      this.paginator._intl.itemsPerPageLabel = values.itemsPerPageLabel;
      this.paginator._intl.nextPageLabel = values.nextPageLabel;
      this.paginator._intl.previousPageLabel = values.previousPageLabel;
      this.paginator._intl.firstPageLabel = values.firstPageLabel;
      this.paginator._intl.lastPageLabel = values.lastPageLabel;
    
      // 1 – 10 of 100
      // https://github.com/angular/components/blob/master/src/material/paginator/paginator-intl.ts#L41
      this.paginator._intl.getRangeLabel = (page: number, pageSize: number, length: number): string => {
        length = Math.max(length, 0);
        const startIndex = page * pageSize;
        const endIndex = startIndex < length ? Math.min(startIndex + pageSize, length) : startIndex + pageSize;
        return this.translate.instant('paginator.getRangeLabel', {
          startIndex: startIndex + 1,
          endIndex,
          length,
        });
      };
    
      // Otherwise, the paginator won't be shown as translated.
      this.dataSource.paginator = this.paginator;
    });
    

提交回复
热议问题