Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get country name from country code in javascript

I am building a weather website, and I want to display the country name for the given location but my API request to openweathermap only returns country code. So is there a way to convert the country code to country name from javascript itself or I need to make one more API call to other url which will convert me the code to name.

like image 464
Ulmasbek rakhmatullaev Avatar asked May 26 '20 18:05

Ulmasbek rakhmatullaev


People also ask

How do you find the country code of currency?

If by currency codes you mean ISO 4217 codes you can get ISO 3166-1 alpha-2 country code from first 2 characters in code for some countries, and then crosscheck it with country name.

What is my 2 character country code?

MY is the two-letter country abbreviation for Malaysia.

Are all country codes 2 letters?

The ISO country codes are internationally recognized codes that designate every country and most of the dependent areas a two-letter combination or a three-letter combination; it is like an acronym, that stands for a country or a state.

How do you add a country code in HTML?

HTML uses 5-character ISO codes according to this pattern: ll-CC. The ll = lower-case language code, and the CC = upper-case country code.


2 Answers

You can use Intl.DisplayNames for this purpose, standard JS api, but not with full browser support yet (needs polyfills for Firefox & Safari as of January 2021).

Usage:

let regionNames = new Intl.DisplayNames(['en'], {type: 'region'});
regionNames.of('US');  // "United States"

Details: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames

like image 77
trefnis Avatar answered Oct 16 '22 17:10

trefnis


I think there is nothing like that build into the Javascript standard library, but as always, there is an NPM library for it: https://www.npmjs.com/package/i18n-iso-countries

like image 33
Tarmo Avatar answered Oct 16 '22 16:10

Tarmo