Language name from ISO 639-1 code in Javascript

internationalization, javascript

Solution

There is a native support for this in the new(ish) Intl API:

let languageNames = new Intl.DisplayNames(['en'], {type: 'language'});
languageNames.of('fr');      // "French"
languageNames.of('de');      // "German"
languageNames.of('fr-CA');   // "Canadian French"

Problem

I'm building a website where people can associate a language information to content. The website uses Javascript heavily and the language information associated to various elements is treated internally as an ISO 639-1 code. How to show a list of language names - in the language of the user ?

Original source

Related problems