I'm looking for a way to get the name of a language in any other language, based on a IETF language tag. For example: I have a list of IETF language tags ('en', 'fr', 'nl', 'de' , ...) and I want them mapped to the language display name of my current locale (e.g. Dutch):
- 'en' => 'Engels'
- 'fr' => 'Frans'
- 'nl' => 'Nederlands'
- 'de' => 'Duits'
I've searched NPM but found nothing. I would prefer a list in JSON, but I will settle for any language/format.
EDIT: Solution
This is how you can implement this using cldr-localenames-modern
(see @korpiq's answer below):
import en from 'cldr-localenames-modern/main/en/languages.json';
import nl from 'cldr-localenames-modern/main/nl/languages.json';
const localeDisplayNames = {
en: en.main.en.localeDisplayNames.languages,
nl: nl.main.nl.localeDisplayNames.languages
};
export function getLanguageDisplayName(language, locale) {
const languages = localeDisplayNames[locale] ?? localeDisplayNames['en'];
return languages[language];
}
This is the closest I have found so far: https://github.com/unicode-org/cldr-json/tree/main/cldr-json/cldr-localenames-modern
However, there the
displayNames
in each locale'slanguages.json
are tagged by 2-3 letter codes, instead of the<language>-<country>
naming used for the locales themselves. For two-letter names, I guess it's sufficient.