MdPaginatorIntl Internationalization example
The new paginator has a small text in the documentation about Internationalization. I was wondering if anyone has an example of doing this. The document states that you can do this by "providing your own instance of MdPaginatorIntl". I have no idea how to do this... Thanks!
Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.
Although this is not explicitly mentioned in the docs, you can infer the purpose of MdPaginatorIntl because it's listed as an Injectable class. This means that it can be leveraged with Angular Dependency Injection (DI), which you can read up on here.
When the docs say you must provide your own instance, it means extending the MdPaginatorIntl class, and then overriding the existing implementation in your component in the providers section.
Try in oninit this: this.paginator._intl.itemsPerPageLabel = 'Registros por página';
Okay I solved this by using each of your solutions ;)
Adding MdPaginator to providers array =>
providers: [MdPaginator]
In the constructor inject the paginator =>
private paginator: MdPaginator
In the OnInit cycle (using translateService of ng2-translate) =>
this.translate.get('app.pagination.itemsPerPageLabel').subscribe(translation => {
this.paginator._intl.itemsPerPageLabel = translation
});
_intl is a private data member on MdPaginator for a reason: it shouldn't be modified directly. Please follow the documentation to create your own instance of the provider, instead of violating abstraction barriers.
@Laurensvdw, actually @CaerusKaru is right but in your defence I can say you just couldn't implement it properly since MdPaginatorIntl is not exposed.
Okay that explains why I couldn't get it working with the documentation @CaerusKaru mentioned earlier about DI, as the MdPaginatorIntl was not available to place in the providers... Thanks for your findings @shlomiassaf !
Ah, now that is a bug. I sent #5726 to fix.
I'm still unclear as to the process for this.
what I understand; and please correct me wherever I'm wrong
- create a new file. Let's call it: paginator.provider.ts
- import { MdPaginatorIntl } from ... not really sure
- create a new Injectable similar to the original; but which extends MdPaginatorIntl
- on your app.modules.ts then import your own version of the instance, lets call it: MdPaginatorProvider
- add (or update) your providers array to include your instance
- add it to the app constructor:
- private mdPaginatorProvider: MdPaginatorProvider
what else?
thank you in advanced
@Lior-G see https://plnkr.co/edit/Zy0kOh78CBelkBH8VEug?p=preview. I don't know if that's the best way to do it, but it works
@willshowell what version of Angular Material are you using? MdPaginatorIntl is not exposed in in 2.0.0-beta.8 which currently is the latest release
@Lior-G The Plunkr uses the latest build from the master branch (generally a couple of commits behind the latest commit). MdPaginatorIntl is exposed as of #5716.
You can have your project rely on the latest build by following Step 1 in the Getting Started guide, just run:
npm install --save angular/material2-builds angular/cdk-builds
Hi, MdPaginatorIntl is exported as ex on (@angular/material/typings/index.d.ts) you can import this class like this:
import {
ɵx as MdPaginatorIntl
} from '@angular/material';
And after:
@NgModule({
declarations: [
],
imports: [
{ provide: MdPaginatorIntl, useClass: MyClassIntl }
]
, bootstrap: [AppComponent]
})
export class AppModule { }
//MyClassIntl
export class MyClassIntl {
itemsPerPageLabel = 'Registros por página: ';
nextPageLabel = 'Página siguiente';
previousPageLabel = 'Página anterior';
getRangeLabel(page: number, pageSize: number, length: number): string {
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}`;
}
}
The cool example that @diarcastro provide unfortunately is not working on beta.10
seems that /typings/index.d.ts in beta.10 has't
export { MdPaginatorIntl as ɵx } from './paginator/paginator-intl';
The documentation to do this is really poor. Anybody with an example for creating your own class with internationalization using for example ngx-translate? Something DYNAMIC ???
+1 to an example how to make this DYNAMIC!!! We use ngx-translate and cannot make this single line of the table dynamic...
@mhosman @rsaulo This is how I did it in "@angular/material": "^5.0.3",
Create separate file e.g. custom-mat-paginator-int.ts:
import {TranslateService} from '@ngx-translate/core';
import {MatPaginatorIntl} from '@angular/material';
import {Injectable} from '@angular/core';
@Injectable()
export class CustomMatPaginatorIntl extends MatPaginatorIntl {
constructor(private translate: TranslateService) {
super();
this.translate.onLangChange.subscribe((e: Event) => {
this.getAndInitTranslations();
});
this.getAndInitTranslations();
}
getAndInitTranslations() {
this.translate.get(['ITEMS_PER_PAGE', 'NEXT_PAGE', 'PREVIOUS_PAGE', 'OF_LABEL']).subscribe(translation => {
this.itemsPerPageLabel = translation['ITEMS_PER_PAGE'];
this.nextPageLabel = translation['NEXT_PAGE'];
this.previousPageLabel = translation['PREVIOUS_PAGE'];
this.changes.next();
});
}
getRangeLabel = (page: number, pageSize: number, length: number) => {
if (length === 0 || pageSize === 0) {
return `0 / ${length}`;
}
length = Math.max(length, 0);
const startIndex = page * pageSize;
const endIndex = startIndex < length ? Math.min(startIndex + pageSize, length) : startIndex + pageSize;
return `${startIndex + 1} - ${endIndex} / ${length}`;
}
}
Load and include it in providers in app.module.ts
import {CustomMatPaginatorIntl} from './shared/custom-mat-paginator-intl';
providers: [{
provide: MatPaginatorIntl,
useClass: CustomMatPaginatorIntl
}]
@rivoheinsalu , yes, i ended with the same solution, thanks for the reply!
Hey @rivoheinsalu, thanks for sharing. The only problem about this is that it keeps saying for example 1 of 2. How can I change the "of"?
@mhosman I have edited my example, see getRangeLabel.
I have not tried it with 'of', insted changed it to '/'.
You could try to set private properti and assing value in translation part: this.ofLabel = translation['OF_LABEL']
And then try to use it in getRangeLabel(). Unfortunately I cannot currently test it.