How to use MatPaginatorIntl?

前端 未结 9 1147
盖世英雄少女心
盖世英雄少女心 2020-12-02 17:59

I\'m using MatPaginator component and I\'m trying to figure out how to translate those labels (documentation isn\'t clear enough about this).

I\'ve found this articl

9条回答
  •  既然无缘
    2020-12-02 18:38

    Additionally you can use injected services by marking the Intl to be an injectable itself. See example (ignore specifics of DoneSubject and LocalizeService as those are custom implementations):

        import { Injectable, OnDestroy } from '@angular/core';
        import { MatPaginatorIntl } from '@angular/material';
        import { LocalizeService } from 'app/general';
        import { DoneSubject } from 'app/rx';
        import { takeUntil } from 'rxjs/operators';
    
        @Injectable()
        export class MatPaginatorIntlLoc extends MatPaginatorIntl implements OnDestroy {
          constructor(private readonly localizer: LocalizeService) {
            super();
    
            localizer.locale$.pipe(takeUntil(this.done$)).subscribe(() => {
              this.itemsPerPageLabel = localizer.translate('mat paginator label: items per page');
              this.nextPageLabel = localizer.translate('mat paginator label: next page');
              this.previousPageLabel = localizer.translate('mat paginator label: previous page');
              this.firstPageLabel = localizer.translate('mat paginator label: first page');
              this.lastPageLabel = localizer.translate('mat paginator label: last page');
            });
          }
    
          private readonly done$ = new DoneSubject();
    
          ngOnDestroy() { this.done$.done(); }
    
          getRangeLabel = (page: number, pageSize: number, length: number) => this.localizer
            .translate('mat paginator label: x of y', [
              length > 0 && pageSize > 0 ? (page * pageSize + 1) + ' - ' + Math.min((page + 1) * pageSize, Math.max(length, 0)) : 0,
              Math.max(length, 0),
            ]);
        }
    

    And don't forget to provide it in your module:

        providers: [
            ...
            { provide: MatPaginatorIntl, useClass: MatPaginatorIntlLoc },
            ...
          ]
    

提交回复
热议问题