Convert country names to country codes with support for both exact matching and diacritics-insensitive matching. Handles official names, aliases, and localized variations across 79 languages.
Retrieves the Alpha-2 country code for a given country name in the specified language.
/**
* Get Alpha-2 code from country name (exact matching)
* @param name - Country name in the specified language
* @param lang - ISO 639-1 language code
* @returns Alpha-2 code or undefined if not found
*/
function getAlpha2Code(name: string, lang: string): string | undefined;Usage Examples:
const countries = require("i18n-iso-countries");
// English names
console.log(countries.getAlpha2Code("United States of America", "en")); // "US"
console.log(countries.getAlpha2Code("Germany", "en")); // "DE"
console.log(countries.getAlpha2Code("United Kingdom", "en")); // "GB"
console.log(countries.getAlpha2Code("UK", "en")); // "GB" (alias)
console.log(countries.getAlpha2Code("Great Britain", "en")); // "GB" (alias)
// Other languages
console.log(countries.getAlpha2Code("Deutschland", "de")); // "DE"
console.log(countries.getAlpha2Code("Allemagne", "fr")); // "DE"
console.log(countries.getAlpha2Code("Alemania", "es")); // "DE"
console.log(countries.getAlpha2Code("ドイツ", "ja")); // "DE"
// Case sensitive exact matching
console.log(countries.getAlpha2Code("united states of america", "en")); // "US"
console.log(countries.getAlpha2Code("GERMANY", "en")); // "DE"
// Name not found
console.log(countries.getAlpha2Code("Nonexistent Country", "en")); // undefined
console.log(countries.getAlpha2Code("Germany", "xx")); // undefined (unsupported language)Retrieves the Alpha-2 country code with diacritics-insensitive matching for better internationalization support.
/**
* Get Alpha-2 code from country name (diacritics-insensitive matching)
* @param name - Country name in the specified language
* @param lang - ISO 639-1 language code
* @returns Alpha-2 code or undefined if not found
*/
function getSimpleAlpha2Code(name: string, lang: string): string | undefined;Usage Examples:
const countries = require("i18n-iso-countries");
// Standard matching (same as getAlpha2Code)
console.log(countries.getSimpleAlpha2Code("Germany", "en")); // "DE"
console.log(countries.getSimpleAlpha2Code("France", "en")); // "FR"
// Diacritics-insensitive matching
console.log(countries.getSimpleAlpha2Code("Cote d'Ivoire", "en")); // "CI"
console.log(countries.getSimpleAlpha2Code("Côte d'Ivoire", "en")); // "CI"
console.log(countries.getSimpleAlpha2Code("Bosnia and Herzegovina", "en")); // "BA"
console.log(countries.getSimpleAlpha2Code("Bosna i Hercegovina", "bs")); // "BA"
// Works with accented characters
console.log(countries.getSimpleAlpha2Code("México", "es")); // "MX"
console.log(countries.getSimpleAlpha2Code("Mexico", "es")); // "MX"
console.log(countries.getSimpleAlpha2Code("Brasil", "pt")); // "BR"
console.log(countries.getSimpleAlpha2Code("Brasil", "pt")); // "BR"
// Case and diacritics insensitive
console.log(countries.getSimpleAlpha2Code("CÔTE D'IVOIRE", "fr")); // "CI"
console.log(countries.getSimpleAlpha2Code("cote d ivoire", "fr")); // "CI"Retrieves the Alpha-3 country code for a given country name in the specified language.
/**
* Get Alpha-3 code from country name (exact matching)
* @param name - Country name in the specified language
* @param lang - ISO 639-1 language code
* @returns Alpha-3 code or undefined if not found
*/
function getAlpha3Code(name: string, lang: string): string | undefined;Usage Examples:
const countries = require("i18n-iso-countries");
// English names
console.log(countries.getAlpha3Code("United States of America", "en")); // "USA"
console.log(countries.getAlpha3Code("Germany", "en")); // "DEU"
console.log(countries.getAlpha3Code("Japan", "en")); // "JPN"
console.log(countries.getAlpha3Code("United Kingdom", "en")); // "GBR"
// Aliases work
console.log(countries.getAlpha3Code("UK", "en")); // "GBR"
console.log(countries.getAlpha3Code("Great Britain", "en")); // "GBR"
// Other languages
console.log(countries.getAlpha3Code("日本", "ja")); // "JPN"
console.log(countries.getAlpha3Code("Japon", "fr")); // "JPN"
console.log(countries.getAlpha3Code("Japón", "es")); // "JPN"
// Name not found
console.log(countries.getAlpha3Code("Invalid Country", "en")); // undefinedRetrieves the Alpha-3 country code with diacritics-insensitive matching.
/**
* Get Alpha-3 code from country name (diacritics-insensitive matching)
* @param name - Country name in the specified language
* @param lang - ISO 639-1 language code
* @returns Alpha-3 code or undefined if not found
*/
function getSimpleAlpha3Code(name: string, lang: string): string | undefined;Usage Examples:
const countries = require("i18n-iso-countries");
// Standard matching
console.log(countries.getSimpleAlpha3Code("Germany", "en")); // "DEU"
console.log(countries.getSimpleAlpha3Code("France", "en")); // "FRA"
// Diacritics-insensitive matching
console.log(countries.getSimpleAlpha3Code("Côte d'Ivoire", "fr")); // "CIV"
console.log(countries.getSimpleAlpha3Code("Cote d'Ivoire", "fr")); // "CIV"
console.log(countries.getSimpleAlpha3Code("México", "es")); // "MEX"
console.log(countries.getSimpleAlpha3Code("Mexico", "es")); // "MEX"
// Case and diacritics insensitive
console.log(countries.getSimpleAlpha3Code("BRASIL", "pt")); // "BRA"
console.log(countries.getSimpleAlpha3Code("brasil", "pt")); // "BRA"getAlpha2CodegetAlpha3CodegetSimpleAlpha2CodegetSimpleAlpha3CodediacriticsBoth matching approaches:
undefinedAll name-to-code conversion functions handle errors gracefully:
undefinedundefinedundefinedundefinedName-to-code conversion works with all 79 supported languages. Some examples:
// European languages
console.log(countries.getAlpha2Code("Deutschland", "de")); // "DE" (German)
console.log(countries.getAlpha2Code("Francia", "es")); // "FR" (Spanish)
console.log(countries.getAlpha2Code("Италия", "ru")); // "IT" (Russian)
// Asian languages
console.log(countries.getAlpha2Code("中国", "zh")); // "CN" (Chinese)
console.log(countries.getAlpha2Code("인도", "ko")); // "IN" (Korean)
console.log(countries.getAlpha2Code("อินเดีย", "th")); // "IN" (Thai)
// African languages
console.log(countries.getAlpha2Code("Misri", "sw")); // "EG" (Swahili)
console.log(countries.getAlpha2Code("Nijeriya", "ha")); // "NG" (Hausa)In browser environments, ensure the required language data is registered before attempting name-to-code conversion:
const countries = require("i18n-iso-countries");
// Register required languages
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));
countries.registerLocale(require("i18n-iso-countries/langs/de.json"));
// Now conversion works
console.log(countries.getAlpha2Code("Germany", "en")); // "DE"
console.log(countries.getAlpha2Code("Deutschland", "de")); // "DE"
// Unregistered language returns undefined
console.log(countries.getAlpha2Code("Francia", "es")); // undefined