8

I'm using material design on Angular 4. I have a table with paginator on the bottom:

enter image description here

Is there any way to translate "Items per page" in Angular4?

I got access to this label by

this.paginator._intl.itemsPerPageLabel

in .ts file of my component, which I put in ngOnInit()

I also have translations in i18n fo .json file, which structure is:

"MESSAGES_LIST_TABLE": {
      "DATE": "Date",
      "FROM": "From",
      "TO": "To",
      "MESSAGE": "Message",
      "AMOUNT": "Amount",
      "BALANCE": "Balance",
      "AVAILABLE_BALANCE": "Avail. bal",
      "ITEMS_PER_PAGE": "Items per page"
}

Can I somehow translate it like this?

this.paginator._intl.itemsPerPageLabel = {{'CUSTOMER.MESSAGES_LIST_TABLE.ITEMS_PER_PAGE' | translate}}
megalucio
  • 5,051
  • 2
  • 22
  • 26
bigmeister
  • 1,137
  • 3
  • 13
  • 20

3 Answers3

6

You make an Injectable extending from MatPaginatorIntl

@Injectable()
export class MatPaginatorIntlGerman extends MatPaginatorIntl {
    itemsPerPageLabel = 'Pro Seite: ';
    nextPageLabel = 'Nächste Seite';
    previousPageLabel = 'Vorherige Seite';

    getRangeLabel = (page: number, pageSize: number, length: number) => {
        return ((page * pageSize) + 1) + ' - ' + ((page * pageSize) + pageSize) + ' von ' + length;
    }
}

And provide it like this in your module

{
    provide: MatPaginatorIntl,
    useClass: forwardRef(() => MatPaginatorIntlGerman)
}
Shadowlauch
  • 581
  • 1
  • 5
  • 13
  • Do I have to put @Injectable() in my component .ts file? Somwhere under ngOnInit()? – bigmeister Nov 24 '17 at 09:22
  • You can have it somewhere in your component at the bottom or in a new file. – Shadowlauch Nov 24 '17 at 09:29
  • Ok, I got it, thank you. Do you have any ideas what can I do, if I want to have also translation to other language and how to implement it? – bigmeister Nov 24 '17 at 09:31
  • I'm not 100% sure, but I think the best way would be to not handcode the labels in your extended MatPaginatorIntl, but to load them via a service, depending on the current language. – Shadowlauch Nov 24 '17 at 09:39
5

https://stackoverflow.com/a/51113262/7296430

//Atribute @ViewChild(MatPaginator) paginator: MatPaginator;

ngOnInit(){ ... this.paginator._intl.itemsPerPageLabel = 'My translation for items per page.'; ... }

0

Create a new instance of MatPaginatorIntl and include it in a custom provider as explained in angular material documentation.

megalucio
  • 5,051
  • 2
  • 22
  • 26