i18n for ISO 3166-1 country codes with support for 79 languages and bidirectional conversion between Alpha-2, Alpha-3, and Numeric codes
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Access localized country names with flexible selection options for official names, aliases, or all variants. Supports 79 languages and handles country name variations seamlessly.
Retrieves the country name for a given country code in the specified language.
/**
* Get country name by country code
* @param code - Alpha-2, Alpha-3, or Numeric country code
* @param lang - ISO 639-1 language code
* @param options - Selection options for name variants
* @returns Country name(s) or undefined if not found
*/
function getName(
code: string | number | Alpha2Code | Alpha3Code,
lang: string,
options?: GetNameOptions
): string | string[] | undefined;
interface GetNameOptions {
select: 'official' | 'alias' | 'all';
}Usage Examples:
const countries = require("i18n-iso-countries");
// Basic usage (returns official name)
console.log(countries.getName("US", "en")); // "United States of America"
console.log(countries.getName("DE", "fr")); // "Allemagne"
// Using different code formats
console.log(countries.getName("USA", "en")); // "United States of America" (Alpha-3)
console.log(countries.getName("840", "en")); // "United States of America" (Numeric)
console.log(countries.getName(840, "en")); // "United States of America" (Numeric as number)
// Selecting specific name variants
console.log(countries.getName("GB", "en", { select: "official" })); // "United Kingdom"
console.log(countries.getName("GB", "en", { select: "alias" })); // "UK"
console.log(countries.getName("GB", "en", { select: "all" })); // ["United Kingdom", "UK", "Great Britain"]
// Countries without aliases return the official name for all variants
console.log(countries.getName("LT", "en", { select: "official" })); // "Lithuania"
console.log(countries.getName("LT", "en", { select: "alias" })); // "Lithuania"
console.log(countries.getName("LT", "en", { select: "all" })); // ["Lithuania"]
// Multiple languages
console.log(countries.getName("JP", "en")); // "Japan"
console.log(countries.getName("JP", "de")); // "Japan"
console.log(countries.getName("JP", "fr")); // "Japon"
console.log(countries.getName("JP", "zh")); // "日本"Retrieves all country names for a specific language as a mapping object.
/**
* Get all country names for a language
* @param lang - ISO 639-1 language code
* @param options - Selection options for name variants
* @returns Object mapping Alpha-2 codes to country names
*/
function getNames(
lang: string,
options?: GetNameOptions
): LocalizedCountryNames<GetNameOptions>;
type LocalizedCountryNames<T extends GetNameOptions> = {
[alpha2Key: string]: CountryName<T>;
};
type CountryName<T extends GetNameOptions> = T extends { select: 'all' }
? string[]
: string;Usage Examples:
const countries = require("i18n-iso-countries");
// Get all official country names in English
const englishNames = countries.getNames("en");
console.log(englishNames.US); // "United States of America"
console.log(englishNames.FR); // "France"
console.log(englishNames.DE); // "Germany"
// Get all country names with aliases
const namesWithAliases = countries.getNames("en", { select: "all" });
console.log(namesWithAliases.GB); // ["United Kingdom", "UK", "Great Britain"]
console.log(namesWithAliases.US); // ["United States of America"]
// Get all aliases (falls back to official if no alias exists)
const aliases = countries.getNames("en", { select: "alias" });
console.log(aliases.GB); // "UK"
console.log(aliases.US); // "United States of America"
// Different languages
const germanNames = countries.getNames("de");
console.log(germanNames.US); // "Vereinigte Staaten von Amerika"
console.log(germanNames.GB); // "Vereinigtes Königreich"
const frenchNames = countries.getNames("fr");
console.log(frenchNames.US); // "États-Unis"
console.log(frenchNames.CN); // "Chine"undefined for invalid country codes{} for unsupported languages in getNames()TypeError for invalid select options ("LocaleNameType must be one of these: all, official, alias!")The following 79 languages are supported (use getSupportedLanguages() for the complete list):
In browser environments, language data must be manually registered before use:
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/fr.json"));
// Now you can use getName() and getNames() with these languages
console.log(countries.getName("US", "en")); // Works
console.log(countries.getName("US", "de")); // Returns undefined (not registered)