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'
Get country main language by country code
3.7k views Asked by Aihara At
3
There are 3 answers
0
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).
You can use this Rest Api for country : https://restcountries.com/