[Paginator] Unclear in docs about how to set language
Bug, feature request, or proposal:
I want to set the paginator to german language. How can I do it???
See #5573 Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are Stack Overflow, gitter, mailing list, etc.
@katrina-engelbrecht the docs briefly mention it at the bottom of the paginator overview, but I don't feel like it's entirely clear. Here's an example
https://plnkr.co/edit/Zy0kOh78CBelkBH8VEug?p=preview
Note that you must be using the latest builds or wait until the next beta release since MdPaginatorIntl was just recently exposed.
I'll add something to the docs similar to the example provided by @willshowell so its clearer about how to use Angular's injection dependency to do this
@andrewseguin maybe additional docs will show a better way but I feel like it's clunky to override all the properties or extend the whole class when you're trying to change the text for all 4 labels. I'm wondering if MdPaginatorIntl could benefit from a constructor,
export const customPaginatorIntl = new MdPaginatorIntl(
'Items per page',
'Next page',
'Previous page',
(page, size, length) => `Page ${page} of ${length / size}`
);
//Atribute
@ViewChild(MatPaginator) paginator: MatPaginator;
ngOnInit(){
...
this.paginator._intl.itemsPerPageLabel = 'My translation for items per page.';
...
}
seriously this whole thing has to be reworked. its just insane how much code you need to translate 4-5 strings.
Hi there, I've something that worked in past (angular 8.x) to translate paginator strings:
import { MatPaginatorIntl } from '@angular/material/paginator';
// PAGINATOR TRANSLATIONS
import { PAGINATOR } from '../../../../i18n/languages';
export class CustomPaginator extends MatPaginatorIntl {
constructor() {
super();
this.itemsPerPageLabel = PAGINATOR.itemsPerPageLabel;
this.lastPageLabel = PAGINATOR.lastPageLabel;
this.nextPageLabel = PAGINATOR.nextPageLabel;
this.previousPageLabel = PAGINATOR.previousPageLabel;
}
}
that stopped working in the new version of material 9.1.3 and angular 9.0.7
Currently if I use that I'm receiving the following error:
ERROR TypeError: Cannot read property 'subscribe' of undefined
at new MatPaginator (paginator.js:235)
I'm using angular 9.1.0 and material 9.2.0. It's working for me:
// Custom class:
import { Injectable } from '@angular/core';
import { MatPaginatorIntl } from '@angular/material/paginator';
@Injectable({
providedIn: 'root'
})
export class CustomMatPaginatorInt extends MatPaginatorIntl {
itemsPerPageLabel = 'Itens por página';
nextPageLabel = 'Próxima página';
previousPageLabel = 'Página anterior';
firstPageLabel = 'Primeira página';
lastPageLabel = 'Última página';
getRangeLabel = (page: number, pageSize: number, length: number) => {
if (length === 0 || pageSize === 0) {
return '0 de ' + 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 startIndex + 1 + ' - ' + endIndex + ' de ' + length;
};
}
// Import on providers of app module:
providers: [
...
{ provide: MatPaginatorIntl, useClass: CustomMatPaginatorIntl}
],
I find this problem really intresting, why it's needed to build a whole component, to change the text of another component, which the main purpose is to not need more code ? Is there any solution except using: private properties of the paginator, third party implementation of the translation, building yourself the implementation solving the problem ?
@MuMaestro
I find this problem really intresting, why it's needed to build a whole component, to change the text of another component, which the main purpose is to not need more code ? Is there any solution except using: private properties of the paginator, third party implementation of the translation, building yourself the implementation solving the problem ?
@marcelo-3d's example is the go. Here's a working example. This method works with almost all the material components. As far as I remember, mat-datepicker has some other implementation.
This is not what I was talking about. Why my language needs a reimplementation of the component ? Why any language other than english should implement extra code to deal with localization ?
I know how to implement code, there are many examples online; I'm asking why I need to add another file to change it ? shouldn't this be possible with a simple input of label ?
Well I see, but this is just a small class. When they implemented it there was no easy way to do the localization of a library that also supports runtime changes. They come up with this. You write it once in the project, can be integrated with any custom localization library and does not need a lot of code.
You want a simplet input, but there are 6 labels.
Do you want 6 different inputs?
<mat-paginator itemsPerPageLabel="Items per page" nextPageLabel="Next page" ....></mat-paginator>
I don't think you want this.
Do you want a single input?
<mat-paginator [labels]="labels"></mat-paginator>
You will have an object that looks like the service you have to implement in every component you use the paginator.
Do you want to put it in the MAT_PAGINATOR_DEFAULT_OPTIONS so you can configure it with the other options?
You loose the ability of runtime changes.
I think this is the price we have to pay, but with this we can have our cake and eat it too.
Duplicate of #8239
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.