i18n for ISO 3166-1 country codes. We support Alpha-2, Alpha-3 and Numeric codes from 'Wikipedia: Officially assigned code elements'
Install it using npm: npm install i18n-iso-countries
var countries = require("i18n-iso-countries");
If you use i18n-iso-countries
with Node.js, you are done. If you use the package in a browser environment, you have to register the languages you want to use to minimize the file size.
// Support french & english languages.
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
countries.registerLocale(require("i18n-iso-countries/langs/fr.json"));
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log("US (Alpha-2) => " + countries.getName("US", "en")); // United States of America
console.log("US (Alpha-2) => " + countries.getName("US", "de")); // Vereinigte Staaten von Amerika
console.log("USA (Alpha-3) => " + countries.getName("USA", "en")); // United States of America
console.log("USA (Numeric) => " + countries.getName("840", "en")); // United States of America
// Some countries have alias/short names defined. `select` is used to control which
// name will be returned.
console.log("GB (select: official) => " + countries.getName("GB", "en", {select: "official"})); // United Kingdom
console.log("GB (select: alias) => " + countries.getName("GB", "en", {select: "alias"})); // UK
console.log("GB (select: all) => " + countries.getName("GB", "en", {select: "all"})); // ["United Kingdom", "UK", "Great Britain"]
// Countries without an alias will always return the offical name
console.log("LT (select: official) => " + countries.getName("LT", "en", {select: "official"})); // Lithuania
console.log("LT (select: alias) => " + countries.getName("LT", "en", {select: "alias"})); // Lithuania
console.log("LT (select: all) => " + countries.getName("LT", "en", {select: "all"})); // ["Lithuania"]
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.getNames("en", {select: "official"})); // { 'AF': 'Afghanistan', 'AL': 'Albania', [...], 'ZM': 'Zambia', 'ZW': 'Zimbabwe' }
In case you want to add new language, please refer ISO 639-1 table.
af
: Afrikaansam
: Amharicar
: Arabicaz
: Azerbaijanibe
: Belorussianbg
: Bulgarianbn
: Bengalibr
: Bretonbs
: Bosnianca
: Catalancs
: Czechcy
: Cymraegda
: Danishde
: Germandv
: Dhivehien
: Englishes
: Spanishet
: Estonianeu
: Basquefa
: Persianfi
: Finnishfr
: Frenchgl
: Galicianel
: Greekha
: Hausahe
: Hebrewhi
: Hindihr
: Croatianhu
: Hungarianhy
: Armenianis
: Icelandicit
: Italianid
: Indonesianja
: Japaneseka
: Georgiankk
: Kazakhkm
: Khmerko
: Koreanku
: Kurdishky
: Kyrgyzlt
: Lithuanianlv
: Latvianmk
: Macedonianml
: Malayalammn
: Mongolianmr
: Marathims
: Malaynb
: Norwegian Bokmålnl
: Dutchnn
: Norwegian Nynorskno
: Norwegianpl
: Polishps
: Pashtopt
: Portuguesero
: Romanianru
: Russiansd
: Sindhisk
: Slovaksl
: Sloveneso
: Somalisq
: Albaniansr
: Serbiansv
: Swedishsw
: Swahilita
: Tamiltg
: Tajikth
: Thaitr
: Turkishtt
: Tatarug
: Uyghuruk
: Ukrainianur
: Urduuz
: Uzbekzh
: Chinesevi
: Vietnamese
var countries = require("i18n-iso-countries");
console.log("List of supported languages => " + countries.getSupportedLanguages());
// List of supported languages => ["cy", "dv", "sw", "eu", "af", "am", ...]
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log("United States of America => " + countries.getAlpha2Code("United States of America", "en"));
// United States of America => US
console.log("United States of America => " + countries.getAlpha3Code("United States of America", "en"));
// United States of America => USA
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log("USA (Alpha-3) => " + countries.alpha3ToAlpha2("USA") + " (Alpha-2)");
// USA (Alpha-3) => US (Alpha-2)
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log("840 (Numeric) => " + countries.numericToAlpha2("840") + " (Alpha-2)");
// 840 (Numeric) => US (Alpha-2)
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log("DE (Alpha-2) => " + countries.alpha2ToAlpha3("DE") + " (Alpha-3)");
// DE (Alpha-2) => DEU (Alpha-3)
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log("840 (Numeric) => " + countries.numericToAlpha3("840") + " (Alpha-3)");
// 840 (Numeric) => USA (Alpha-3)
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.alpha3ToNumeric("SWE"));
// 752
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.alpha2ToNumeric("SE"));
// 752
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.getAlpha2Codes());
// { 'AF': 'AFG', 'AX': 'ALA', [...], 'ZM': 'ZMB', 'ZW': 'ZWE' }
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.getAlpha3Codes());
// { 'AFG': 'AF', 'ALA': 'AX', [...], 'ZMB': 'ZM', 'ZWE': 'ZW' }
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(countries.getNumericCodes());
// { '004': 'AF', '008': 'AL', [...], '887': 'YE', '894': 'ZM' }
var countries = require("i18n-iso-countries");
// in a browser environment: countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
console.log(
countries.isValid("US"),
countries.isValid("USA"),
countries.isValid("XX")
);
// true, true, false
To add a language:
- add a json file under langs/
- add the language to the list in supportedLocales.json at the top
- add language to section Supported languages in README.md
- add language to keywords in package.json
- run
npm run lint
andnpm test
- open a PR on GitHub
You can check codes here: https://www.iso.org/obp/ui/#home