Get country main language by country code

3.7k Views Asked by At

Is there a way i can get a country main language by its country code? I need something like countries-list npm package which gives me a list of spoken language codes by a country code. An example of what i mean: 'en' => 'English'

3

There are 3 best solutions below

0
Sofyane MAKERRI On BEST ANSWER

You can use this Rest Api for country : https://restcountries.com/

0
Scott Morrison On

https://www.npmjs.com/package/country-locale-map can return a default locale for each country

0
Inkling On

The CLDR contains this information, you can see a table here: https://www.unicode.org/cldr/charts/44/supplemental/territory_language_information.html

To use the data in Node.js you can install the cldr-core package from NPM and import the /supplemental/territoryInfo.json file from there. You can see how it's structured here: https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-core/supplemental/territoryInfo.json
Writing a function to look up a country in that data and return its language codes should be pretty trivial.

If you want to convert the language code into a language name, the Intl.DisplayNames API can do it (it can convert country codes into names too).