avatarSébastien Dubois.

Summary

The provided content outlines the process of translating the MatPaginator component in Angular Material, which requires extending the MatPaginatorIntl class and using a library like ngx-translate for dynamic label translations.

Abstract

The article discusses the internationalization of the Angular Material MatPaginator component, which is essential for applications catering to users speaking different languages. It explains that while Angular Material supports internationalization, the MatPaginator component's labels are not translated by default and must be handled specifically. The author guides readers through creating a custom implementation of the MatPaginatorIntl class to dynamically update labels according to the active language. The article includes code examples using ngx-translate for language change detection and translation, and it provides instructions on how to integrate this custom implementation into an Angular application. The author emphasizes that while the process is straightforward, it is not immediately obvious and requires this specific knowledge to execute effectively.

Opinions

  • The author suggests that internationalization and localization become important as soon as an application reaches a diverse user base, indicating the importance of these features in modern web development.
  • The author expresses that Angular Material has strong support for internationalization and localization, implying confidence in the framework's capabilities.
  • The use of ngx-translate is presented as a flexible choice for translation services, though the author acknowledges that other libraries could be used.
  • The author indicates that the default MatPaginatorIntl class's implementation is limited to English and requires extension for translation, highlighting a potential area for improvement in Angular Material.
  • The author's approach to translating the MatPaginator labels is seen as a practical solution, though they mention that more advanced methods could be employed for complex translation needs.
  • By providing a conclusion and encouraging readers to learn more through their book and newsletter, the author conveys a commitment to sharing knowledge and a belief in the value of their guidance on the subject.

Translating the MatPaginator Angular Material component

The MatPaginator component of Angular Material can be translated, but it requires a specific approach.

Picture courtesy of Nereeta Martin

Internationalization and localization start to matter as soon as your application is not limited to a single community of users, all speaking the same language. Fortunately for us, Angular Material has great support for internationalization & localization.

The MatPaginator component is often used in combination with lists / tables, in order to let end users control the number of items per page and allow them to easily navigate from page to page. Of course there are a few labels to translate (e.g., first page, items per page, last page, etc).

In this article, I’ll explain how to translate the MatPaginator component, which is a bit more complicated to handle; it’s one of those “you have to know that to know that” cases.

MatPaginatorIntl

To translate the MatPaginatorComponent, you need to use/extend the MatPaginatorIntl class

The built-in class exposes static properties providing the different labels:

  • firstPageLabel
  • getRangeLabel
  • itemsPerPageLabel
  • lastPageLabel
  • nextPageLabel
  • previousPageLabel

The default implementation does not translate those labels. It simply returns the english version statically. So we have to extend it in order to be able to translate the labels ourselves dynamically.

Let’s see how.

Creating a custom implementation of the MatPaginatorIntl class

Our custom MatPaginatorIntl implementation will extend the built-in class in order to dynamically replace the translations with the correct ones, depending on the active language. In order to do that, we’ll need to listen to language changes in the class and adapt the translations at that point, since those need to be stored in the class.

For this example, we’ll use ngx-translate, but you can easily adapt the code to use another internationalization library of your choice.

As you can see, our implementation simply extends the built-in class. So, by default, it inherits from the default english version of the labels.

We inject the TranslateService of ngx-translate and use it to subscribe to language change events. Whenever the language changes, we call the translateLabels method of our class to translate the different labels and update the superclass properties. The translateLabels method finishes by emitting an event using this.changes.next(), which warns Angular Material that the labels have changed and may need to be refreshed on screen.

In the constructor, we also immediately invoke the translateLabels method in order to directly get the correct translation, depending on the currently active language.

The getRangeLabel method requires a bit more fiddling around because the translation is in the middle of a sentence. The default implementation is simply using concatenation. Here we mostly do the same, except that we translate the “of” word. You can get fancy and use more advanced solutions here. In my case, this fit the bill.

With the above implementation, we simply need to add the following translation keys:

"I18N": {
    "MAT_PAGINATOR": {
    "FIRST_PAGE": "Première page",
    "ITEMS_PER_PAGE": "Eléments par page",
    "LAST_PAGE": "Dernière page",
    "NEXT_PAGE": "Page suivante",
    "PREVIOUS_PAGE": "Page précédente",
    "OF": "sur"
  }
}

Finally, we need to add a provider to some shared module:

Conclusion

In this short article, I’ve explained how you can translate the MatPaginator component of Angular Material. It’s not hard at all, but you just can’t guess it ;-)

That’s it for today!

Enjoyed this article?

If you want to learn tons of other cool things about software/Web development, TypeScript, Angular, React, Vue, Kotlin, Java, Docker/Kubernetes and other cool subjects, then don’t hesitate to grab a copy of my book and to subscribe to my newsletter!

Programming
Web Development
Software Development
Coding
Angular
Recommended from ReadMedium