components icon indicating copy to clipboard operation
components copied to clipboard

MdPaginatorIntl Internationalization example

Open Laurensvdw opened this issue 9 years ago • 36 comments

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!

Laurensvdw avatar Jul 07 '17 12:07 Laurensvdw

Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are stack overflow, gitter, mailing list, etc.

jelbourn avatar Jul 07 '17 16:07 jelbourn

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.

CaerusKaru avatar Jul 07 '17 16:07 CaerusKaru

Try in oninit this: this.paginator._intl.itemsPerPageLabel = 'Registros por página';

ferlellws avatar Jul 07 '17 17:07 ferlellws

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
});

Laurensvdw avatar Jul 10 '17 08:07 Laurensvdw

_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.

CaerusKaru avatar Jul 10 '17 18:07 CaerusKaru

@Laurensvdw, actually @CaerusKaru is right but in your defence I can say you just couldn't implement it properly since MdPaginatorIntl is not exposed.

shlomiassaf avatar Jul 13 '17 06:07 shlomiassaf

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 !

Laurensvdw avatar Jul 13 '17 14:07 Laurensvdw

Ah, now that is a bug. I sent #5726 to fix.

jelbourn avatar Jul 13 '17 15:07 jelbourn

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 avatar Jul 14 '17 19:07 Lior-G

@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 avatar Jul 20 '17 14:07 willshowell

@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 avatar Jul 20 '17 15:07 Lior-G

@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

CaerusKaru avatar Jul 20 '17 15:07 CaerusKaru

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}`;
  }
}

diarcastro avatar Aug 10 '17 03:08 diarcastro

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';

zggb avatar Oct 26 '17 16:10 zggb

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 ???

mhosman avatar Dec 24 '17 18:12 mhosman

+1 to an example how to make this DYNAMIC!!! We use ngx-translate and cannot make this single line of the table dynamic...

rsaulo avatar Jan 16 '18 13:01 rsaulo

@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 avatar Jan 17 '18 13:01 rivoheinsalu

@rivoheinsalu , yes, i ended with the same solution, thanks for the reply!

rsaulo avatar Jan 17 '18 14:01 rsaulo

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 avatar Jan 17 '18 17:01 mhosman

@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.

rivoheinsalu avatar Jan 17 '18 17:01 rivoheinsalu