DisplayNames for Languages and Regions
You'll learn how to convert ISO language and region codes into human-readable, localized names using Intl.DisplayNames.
What Problem Does This Solve?
Imagine you have the code "en" or "US" in your database. How do you display "English" or "United States" to users—in their own language? Hard-coding translations is tedious and error-prone. Intl.DisplayNames solves this by providing localized names for ISO codes automatically.
Using DisplayNames for Languages
When you specify type: 'language', DisplayNames converts language codes (like "fr", "es", "ja") into their names in the display locale:
const namesInEnglish = new Intl.DisplayNames(['en'], { type: 'language' });
console.log(namesInEnglish.of('fr')); // "French"
console.log(namesInEnglish.of('ja')); // "Japanese"
const namesInFrench = new Intl.DisplayNames(['fr'], { type: 'language' });
console.log(namesInFrench.of('fr')); // "français"
console.log(namesInFrench.of('ja')); // "japonais"
Using DisplayNames for Regions
With type: 'region', you convert country/region codes (like "US", "JP", "BR") into localized region names:
const regionsInEnglish = new Intl.DisplayNames(['en'], { type: 'region' });
console.log(regionsInEnglish.of('US')); // "United States"
console.log(regionsInEnglish.of('BR')); // "Brazil"
const regionsInSpanish = new Intl.DisplayNames(['es'], { type: 'region' });
console.log(regionsInSpanish.of('US')); // "Estados Unidos"
console.log(regionsInSpanish.of('BR')); // "Brasil"
Why This Matters
This API respects cultural conventions—showing "Japan" to English speakers but "日本" to Japanese speakers—without you maintaining translation tables.
Key Takeaway: Use Intl.DisplayNames with type: 'language' or type: 'region' to transform ISO codes into localized, user-friendly names automatically.