components icon indicating copy to clipboard operation
components copied to clipboard

[Paginator] Unclear in docs about how to set language

Open katrina-engelbrecht opened this issue 9 years ago • 15 comments

Bug, feature request, or proposal:

I want to set the paginator to german language. How can I do it???

katrina-engelbrecht avatar Jul 20 '17 13:07 katrina-engelbrecht

See #5573 Please keep GitHub issues for bug reports / feature requests. Better avenues for troubleshooting / questions are Stack Overflow, gitter, mailing list, etc.

donroyco avatar Jul 20 '17 13:07 donroyco

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

willshowell avatar Jul 20 '17 14:07 willshowell

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

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

willshowell avatar Jul 20 '17 16:07 willshowell

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

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

venicyuspaz avatar Jul 13 '18 20:07 venicyuspaz

seriously this whole thing has to be reworked. its just insane how much code you need to translate 4-5 strings.

gotwig avatar Mar 08 '19 09:03 gotwig

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

BruneXX avatar Mar 20 '20 14:03 BruneXX

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)

BruneXX avatar Mar 20 '20 14:03 BruneXX

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}
  ],

marcelo-3d avatar Apr 06 '20 11:04 marcelo-3d

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 avatar Apr 08 '21 18:04 MuMaestro

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

Totati avatar Apr 09 '21 09:04 Totati

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 ?

MuMaestro avatar Apr 14 '21 17:04 MuMaestro

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.

Totati avatar Apr 15 '21 09:04 Totati

Duplicate of #8239

jelbourn avatar May 25 '21 23:05 jelbourn

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.