December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 16 | Creating Language-Sensitive Lists with Intl.ListFormat() |
Let’s say you have a gaming app and want to display the top three players of the week based on their scores. Because your app is available in multiple languages, you need a function that can format the list automatically based on the user’s language preference. It’s fairly common to have a series of items like this stored in an array that you need to format as a sentence in different languages.
Use the Intl.ListFormat() constructor:
| 1: | const topPlayers = ["Kraken", "Boss99", "Ninja"]; |
| - | const msg = "Congratulations to this week's winners: "; |
| - | |
| - | function getFormattedList(lang, arr) { |
| 5: | const formatter = new ... |
Read now
Unlock full access