How to translate mat-paginator in Angular 4?

后端 未结 7 2181
无人及你
无人及你 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:31

    You can use the MatPaginatorIntl for this. Will Showell made an example that no longer works, so here is an updated version (with Dutch) and step-by-step guidance.

    1. Import the MatPaginatorIntl from @angular/material into your application.
    2. Create a new paginator file for your locale (in this example I use Dutch) and import that: import { getDutchPaginatorIntl } from './app/dutch-paginator-intl'; in main.ts file
    3. Set a provider for the Paginator inside of the main.ts file, so it takes the translations of your local file (instead of English as default language):
        providers: [
           { provide: MatPaginatorIntl, useValue: getDutchPaginatorIntl() }
        ]
    
    1. Inside your paginator-intl file, set the labels for the strings that can be translated and export these. Most important part of that file (see the example for more info):
        paginatorIntl.itemsPerPageLabel = 'Items per pagina:';
        paginatorIntl.firstPageLabel = 'Eerste pagina';
        paginatorIntl.previousPageLabel = 'Vorige pagina';
        paginatorIntl.nextPageLabel = 'Volgende pagina';
        paginatorIntl.lastPageLabel = 'Laatste pagina';
        paginatorIntl.getRangeLabel = dutchRangeLabel;
    

    Example on StackBlitz with the paginator translations file as starting point.


    June 2018 - Update to Angular 6.x
    This updated Example on StackBlitz is upgraded to Angular 6.x to accommodate the latest version of the framework. Only packages have changed, nothing inside the paginator has changed.


    June 2019 - Update to Angular 8.x
    This updated Example on StackBlitz is upgraded to Angular 8.x to accommodate the latest version of the framework. Only packages have changed, nothing inside the paginator has changed.


    February 2020 - Update to Angular 9.x
    This updated Example on StackBlitz is upgraded to Angular 9.x to accommodate the latest version of the framework. Package versions have changed. Major change is the way to import from Angular Material. You cannot import from Material root anymore. You need to specify the import from the module (material/paginator) itself:

    import { MatPaginatorModule, MatPaginatorIntl } from '@angular/material/paginator';
    

    June 2020 - Update to Angular 10.x
    This updated Example on StackBlitz is upgraded to Angular 10.x to accommodate the latest version of the framework. Only packages have changed, nothing inside the paginator has changed.

    0 讨论(0)
  • 2020-12-02 22:34
    this.dataSource.paginator._intl.itemsPerPageLabel = "Your string here";
    

    this worked in latest angular8 + material8;

    0 讨论(0)
  • 2020-12-02 22:40

    Following the current Angular Material documentation (10.2.0) to modify the labels and text displayed, create a new instance of MatPaginatorIntl and include it in a custom provider. You have to import MatPaginatorModule (you would need it anyway to display paginator component)

    import { MatPaginatorModule } from '@angular/material/paginator';
    

    and in your component (where you are using paginator) you could use it in following way:

     constructor(private paginator: MatPaginatorIntl) {
        paginator.itemsPerPageLabel = 'Your custom text goes here'; 
    }
    
    0 讨论(0)
  • 2020-12-02 22:47

    For angular 9.0.0, if you are using i18n package, you can do

    require: ng add @angular/localization

    Create a file called my-paginator-intl.ts

    import { MatPaginatorIntl } from '@angular/material/paginator'
    
    const matRangeLabelIntl = (page: number, pageSize: number, length: number) => {
        if (length === 0 || pageSize === 0) {
            return $localize`:@@paginator.zeroRange:0 in ${length}`
        }
        length = Math.max(length, 0)
        const startIndex = page * pageSize
    
        // If the start index exceeds the list length, do not try and fix the end index to the end.
        const endIndex = startIndex < length ?  Math.min(startIndex + pageSize, length) : startIndex + pageSize
        return $localize`:@@paginator.rangeOfLabel:${startIndex + 1} - ${endIndex} in ${length}`
    }
    
    export function MyPaginatorIntl() {
        const paginatorIntl = new MatPaginatorIntl()
    
        paginatorIntl.itemsPerPageLabel = $localize`:@@paginator.displayPerPage:Items per page`
        paginatorIntl.nextPageLabel = $localize`:@@paginator.nextPage:Next page`
        paginatorIntl.previousPageLabel = $localize`:@@paginator.prevPage:Prev page`
        paginatorIntl.getRangeLabel = matRangeLabelIntl
    
        return paginatorIntl
    }
    

    Import to app.modudle.ts

    import { MatPaginatorIntl } from '@angular/material/paginator'
    import { MyPaginatorIntl } from './shared/paginator-int/my-paginator-intl'
    
    @NgModule({
        providers: [
            { provide: MatPaginatorIntl, useValue: MyPaginatorIntl() },
        ]
    })
    

    Copy following to your language xlf file

    <trans-unit id="paginator.zeroRange">
      <source>0 of <x id="PH" /></source>
      <target>0 trong <x id="PH" /></target>
    </trans-unit>
    <trans-unit id="paginator.rangeOfLabel">
      <source><x id="PH" /> - <x id="PH_1" /> of <x id="PH_2" /></source>
      <target><x id="PH" /> - <x id="PH_1" /> trong <x id="PH_2" /></target>
    </trans-unit>
    <trans-unit id="paginator.displayPerPage">
      <source>Items per page</source>
      <target>Hiển thị/Trang</target>
    </trans-unit>
    <trans-unit id="paginator.nextPage">
      <source>Next page</source>
      <target>Trang kế</target>
    </trans-unit>
    <trans-unit id="paginator.prevPage">
      <source>Prev page</source>
      <target>Trang trước</target>
    </trans-unit>
    
    0 讨论(0)
  • 2020-12-02 22:48

    For a quick and dirty solution use this.paginator._intl property.

    In my ...component.ts I have:

    @ViewChild(MatPaginator) paginator: MatPaginator;
    
    ngOnInit() {
      ...
      this.paginator._intl.itemsPerPageLabel = 'My translation for items per page.';
      ...
    }
    
    0 讨论(0)
  • 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;
    });
    
    0 讨论(0)
提交回复
热议问题