Translating the MatPaginator Angular Material component
The MatPaginator component of Angular Material can be translated, but it requires a specific approach
The MatPaginator component of Angular Material can be translated, but it requires a specific approach.
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! ✨
About Sébastien
I am Sébastien Dubois. You can follow me on X 🐦 and on BlueSky 🦋.
I am an author, founder, and coach. I write books and articles about Knowledge Work, Personal Knowledge Management, Note-taking, Lifelong Learning, Personal Organization, and Zen Productivity. I also craft lovely digital products . You can learn more about my projects here.
If you want to follow my work, then become a member.
Ready to get to the next level?
To embark on your Knowledge Management journey, consider investing in resources that will equip you with the tools and strategies you need. Check out the Obsidian Starter Kit and the accompanying video course. It will give you a rock-solid starting point for your note-taking and Knowledge Management efforts.
If you want to take a more holistic approach, then the Knowledge Worker Kit is for you. It covers PKM, but expands into productivity, personal organization, project/task management, and more:
If you are in a hurry, then do not hesitate to book a coaching session with me: