JavaScript: Localization of Languages Names

I wanted to make myself write more in the languages out side of English (not my native language). Plus, just in case somebody is actually reading my blog, then I wanted to make sure, they could see just the blog posts, that they could see.

While I was doing this, I found an interesting ability in the JavaScript, I did not know about before.

Apparently the JavaScript has the ability to localize many string, from time, currencies and regions (like countries) and so on. Among them was also the ability to get the name of the languages just by having a language codes of both languages (name of language and language used).

This is the code, that I end up using.

new Intl.DisplayNames([inLang], {type: "language"}).of(lang)

In this case, the inLang is the language used. And lang is the name of the language, that we are translating.

So getting the name of English in Japanese would be new Intl.DisplayNames(["ja"], {type: "language"}).of("en"). And the result would be 英語.

It seems that have been supported in Firefox since February last year (version 86) and in Node about the year longer (version 14). So it seems a bit new capability. In my case I so check, if the Intl and Intl.DisplayNames exist, and if not, only language code is returned.

More info can be found on Mozilla developer page for Init.DisplayName.