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.
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.
MY is the two-letter country abbreviation for Malaysia.
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.
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With