i18n-iso-countries
i18n for ISO 3166-1 country codes. We support Alpha-2, Alpha-3 and Numeric codes from http://en.wikipedia.org/wiki/ISO_3166-1#Officially_assigned_code_elements
Installing
Install it using npm: npm install i18n-iso-countries
var countries = require("i18n-iso-countries");
If you use i18n-iso-countries
with Node.js your are done. If you use the package in a browser environment you also have to register the languages you want to use to minimize file size.
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
countries.registerLocale(require("i18n-iso-countries/langs/fr.json"));
Code to Country
Get the name of a country by it's ISO 3166-1 Alpha-2, Alpha-3 or Numeric code
var countries = require("i18n-iso-countries");
console.log("US (Alpha-2) => " + countries.getName("US", "en"));
console.log("US (Alpha-2) => " + countries.getName("US", "de"));
console.log("USA (Alpha-3) => " + countries.getName("USA", "en"));
console.log("USA (Numeric) => " + countries.getName("840", "en"));
Get all names by their ISO 3166-1 Alpha-2 code
var countries = require("i18n-iso-countries");
console.log(countries.getNames("en"));
Supported languages (ISO 639-1)
ar
: Arabicaz
: Azerbaijanibe
: Belorussianbg
: Bulgarianbs
: Bosnianca
: Catalancs
: Czechda
: Danishde
: Germanen
: Englishes
: Spanishet
: Estonianfa
: Persianfi
: Finnishfr
: Frenchel
: Greekhe
: Hebrewhr
: Croatianhu
: Hungarianhy
: Armenianit
: Italianid
: Indonesianja
: Japaneseka
: Georgiankk
: Kazakhko
: Koreanky
: Kyrgyzlt
: Lithuanianlv
: Latvianmk
: Macedonianmn
: Mongoliannb
: Norwegian Bokmålnl
: Dutchnn
: Norwegian Nynorskpl
: Polishpt
: Portuguesero
: Romanianru
: Russiansk
: Slovaksl
: Slovenesr
: Serbiansv
: Swedishtr
: Turkishuk
: Ukrainianuz
: Uzbekzh
: Chinese
List of ISO 639-1 codes
Country to Code
var countries = require("i18n-iso-countries");
console.log("United States of America => " + countries.getAlpha2Code('United States of America', 'en'));
console.log("United States of America => " + countries.getAlpha3Code('United States of America', 'en'));
Codes
Convert Alpha-3 to Alpha-2 code
var countries = require("i18n-iso-countries");
console.log("USA (Alpha-3) => " + countries.alpha3ToAlpha2("USA") + " (Alpha-2)");
Convert Numeric to Alpha-2 code
var countries = require("i18n-iso-countries");
console.log("840 (Numeric) => " + countries.numericToAlpha2("840") + " (Alpha-2)");
Convert Alpha-2 to Alpha-3 code
var countries = require("i18n-iso-countries");
console.log("DE (Alpha-2) => " + countries.alpha2ToAlpha3("DE") + " (Alpha-3)");
Convert Numeric to Alpha-3 code
var countries = require("i18n-iso-countries");
console.log("840 (Numeric) => " + countries.numericToAlpha3("840") + " (Alpha-3)");
Convert Alpha-3 to Numeric code
var countries = require("i18n-iso-countries");
console.log(countries.alpha3ToNumeric("SWE"));
Convert Alpha-2 to Numeric code
var countries = require("i18n-iso-countries");
console.log(countries.alpha2ToNumeric("SE"));
Get all Alpha-2 codes
var countries = require("i18n-iso-countries");
console.log(countries.getAlpha2Codes());
Get all Alpha-3 codes
var countries = require("i18n-iso-countries");
console.log(countries.getAlpha3Codes());
Get all Numeric codes
var countries = require("i18n-iso-countries");
console.log(countries.getNumericCodes());
Validate country code
var countries = require("i18n-iso-countries");
console.log(countries.isValid("US"), countries.isValid("USA"), countries.isValid("XX")));
Contribution
To add a language:
- add a json file under langs/
- add the language to the
data
object in enty-node.js at the top - add language to section Supported languages in README.md
- add language to keywords in package.json
- run
npm install && make test
to make sure that tests are passing - open a PR on GitHub
You can check codes here: https://www.iso.org/obp/ui/#home