How to use MatPaginatorIntl?

前端 未结 9 1119
盖世英雄少女心
盖世英雄少女心 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:45

    I did some modifications to fix the end index when start index exceed the list length. I also add the translation for first and last page. It is for @angular/material 5.2.4 pagination component.

    import { Injectable } from '@angular/core';
    import { MatPaginatorIntl } from '@angular/material';
    import { TranslateService } from '@ngx-translate/core';
    
    @Injectable()
    export class MatPaginationIntlService extends MatPaginatorIntl {
      translate: TranslateService;
      firstPageLabel = 'First page';
      itemsPerPageLabel = 'Items per page';
      lastPageLabel = 'Last page';
      nextPageLabel = 'Next page';
      previousPageLabel = 'Previous page';
    
      getRangeLabel = (page: number, pageSize: number, length: number): string => {
        const of = this.translate ? this.translate.instant('mat-paginator-intl.of') : 'of';
        if (length === 0 || pageSize === 0) {
          return '0 ' + of + ' ' + length;
        }
        length = Math.max(length, 0);
        const startIndex = ((page * pageSize) > length) ?
          (Math.ceil(length / pageSize) - 1) * pageSize:
          page * pageSize;
    
        const endIndex = Math.min(startIndex + pageSize, length);
        return startIndex + 1 + ' - ' + endIndex + ' ' + of + ' ' + length;
      };
    
      injectTranslateService(translate: TranslateService) {
        this.translate = translate;
    
        this.translate.onLangChange.subscribe(() => {
          this.translateLabels();
        });
    
        this.translateLabels();
      }
    
      translateLabels() {
        this.firstPageLabel = this.translate.instant('mat-paginator-intl.first_page');
        this.itemsPerPageLabel = this.translate.instant('mat-paginator-intl.items_per_page');
        this.lastPageLabel = this.translate.instant('mat-paginator-intl.last_page');
        this.nextPageLabel = this.translate.instant('mat-paginator-intl.next_page');
        this.previousPageLabel = this.translate.instant('mat-paginator-intl.previous_page');
      }
    }
    
    0 讨论(0)
  • 2020-12-02 18:52

    I had the same issue, and then I changed in app.module.ts in the imports statement TranslateModule to TranslateModule.forRoot()

    So it looks like this:

    imports: [
        ...
        TranslateModule.forRoot()
        ...
    ]
    

    Quote from NPM's site: "The forRoot static method is a convention that provides and configures services at the same time. Make sure you only call this method in the root module of your application, most of the time called AppModule. This method allows you to configure the TranslateModule by specifying a loader, a parser and/or a missing translations handler."

    Here is the whole article: https://www.npmjs.com/package/@ngx-translate/core

    Reading this can help resolving many issues with TranslateModule!

    0 讨论(0)
  • 2020-12-02 18:55

    In order to refresh the label, you can fire a change event after the label change:

    translateLabels() {
        this.firstPageLabel = this.translate.instant('mat-paginator-intl.first_page');
        ...
        this.changes.next();
    }
    
    0 讨论(0)
提交回复
热议问题