or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

code-conversion.mdindex.mdlistings-validation.mdlocale-management.mdname-retrieval.mdname-to-code.md
tile.json

name-retrieval.mddocs/

Country Name Retrieval

Access localized country names with flexible selection options for official names, aliases, or all variants. Supports 79 languages and handles country name variations seamlessly.

Capabilities

Get Country Name

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")); // "日本"

Get All Country Names

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"

Error Handling

  • Returns
    undefined
    for invalid country codes
  • Returns empty object
    {}
    for unsupported languages in
    getNames()
  • Throws
    TypeError
    for invalid
    select
    options ("LocaleNameType must be one of these: all, official, alias!")

Language Support

The following 79 languages are supported (use

getSupportedLanguages()
for the complete list):

  • af: Afrikaans
  • am: Amharic
  • ar: Arabic
  • az: Azerbaijani
  • be: Belorussian
  • bg: Bulgarian
  • bn: Bengali
  • br: Breton
  • bs: Bosnian
  • ca: Catalan
  • cs: Czech
  • cy: Cymraeg
  • da: Danish
  • de: German
  • dv: Dhivehi
  • el: Greek
  • en: English
  • es: Spanish
  • et: Estonian
  • eu: Basque
  • fa: Persian
  • fi: Finnish
  • fr: French
  • ga: Irish
  • gl: Galician
  • ha: Hausa
  • he: Hebrew
  • hi: Hindi
  • hr: Croatian
  • hu: Hungarian
  • hy: Armenian
  • id: Indonesian
  • is: Icelandic
  • it: Italian
  • ja: Japanese
  • ka: Georgian
  • kk: Kazakh
  • km: Khmer
  • ko: Korean
  • ku: Kurdish
  • ky: Kyrgyz
  • lt: Lithuanian
  • lv: Latvian
  • mk: Macedonian
  • ml: Malayalam
  • mn: Mongolian
  • mr: Marathi
  • ms: Malay
  • mt: Maltese
  • nb: Norwegian Bokmål
  • nl: Dutch
  • nn: Norwegian Nynorsk
  • no: Norwegian
  • pl: Polish
  • ps: Pashto
  • pt: Portuguese
  • ro: Romanian
  • ru: Russian
  • sd: Sindhi
  • sk: Slovak
  • sl: Slovene
  • so: Somali
  • sq: Albanian
  • sr: Serbian
  • sv: Swedish
  • sw: Swahili
  • ta: Tamil
  • tg: Tajik
  • th: Thai
  • tk: Turkmen
  • tr: Turkish
  • tt: Tatar
  • ug: Uyghur
  • uk: Ukrainian
  • ur: Urdu
  • uz: Uzbek
  • vi: Vietnamese
  • zh: Chinese

Browser Environment Notes

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)