Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

world_countries_lists

stefangabos36.7kLGPL-3.0-or-later2.9.0TypeScript support: included

Constantly updated lists of world countries and their associated alpha-2, alpha-3 and numeric codes as defined by the ISO 3166 standard, available in multiple formats, in multiple languages and with national flags included

countries, flags, national-flags, iso-3166, sql, json, csv, php, xml

readme

zebrajs

World countries  Tweet

available in multiple languages, in CSV, JSON, PHP, SQL and XML formats, with associated codes as defined by the ISO 3166 standard, and with national flags included; also available are the ISO 3166-2 codes used for identifying the principal subdivisions (e.g., provinces or states) of all countries coded in ISO 3166-1

npm Total Monthly JSDelivr License

Constantly updated lists of world countries, territories and areas of geographical interest, with associated alpha-2, alpha-3 and numeric codes as defined by the ISO 3166-1 standard, published and maintained by the International Organization for Standardization, available in CSV, JSON, PHP, SQL and XML formats, in multiple languages and with national flags included. Also available are the ISO 3166-2 codes used for identifying the principal subdivisions (e.g., provinces or states) of all countries coded in ISO 3166-1.

In the language folders (inside data/countries) you will find files named in two different ways.
Here's what you will find in each of them:

File names Content
world.* Files named like this contain all the 249 countries, territories, and areas of geographical interest that have an officially assigned ISO 3166-1 code.
countries.* Files named like this contain 195 entries made up from the 193 sovereign states (commonly referred to as countries) that are members of the United Nations (UN) plus the 2 observer states of Palestine and the Vatican City State.

Note that there are 206 sovereign states in the world, the 11 states that are missing from the countries list being the ones having their sovereignty disputed. See the list of sovereign states with information on their status and recognition of their sovereignty.

The files contain:

  • the ISO 3166-1 numeric country codes
  • the ISO 3166 official short names in English1
  • the ISO 3166-1 alpha-2 two-letter country codes2
  • the ISO 3166-1 alpha-3 three-letter country codes2

1 for other languages the country names are in that particular language
2 ISO 3166-1 alpha codes are uppercase but this library provides them in lowercase

The lists are currently available in 35 languages:

  • Arabic
  • Armenian
  • Basque
  • Bulgarian
  • Chinese (Simplified)
  • Chinese (Traditional)
  • Croatian
  • Czech
  • Danish
  • Dutch
  • English
  • Esperanto
  • Estonian
  • Farsi
  • Finnish
  • French
  • German
  • Greek
  • Hungarian
  • Italian
  • Japanese
  • Korean
  • Lithuanian
  • Norwegian
  • Polish
  • Portuguese
  • Romanian
  • Russian
  • Serbian
  • Slovak
  • Slovenian
  • Spanish
  • Swedish
  • Thai
  • Ukrainian

The language folders are named based on the ISO 639-1 standard.

ISO 3166-2 codes

The project also tries to be a comprehensive and up-to-date source for ISO 3166-2 which defines codes for identifying the principal subdivisions (e.g., provinces or states) of all countries coded in ISO 3166-1.

The purpose of ISO 3166-2 is to establish an international standard of short and unique alphanumeric codes to represent the relevant administrative divisions and dependent territories of all countries in a more convenient and less ambiguous form than their full names. Each complete ISO 3166-2 code consists of two parts, separated by a hyphen:

US-TX for Texas, USA

The first part is the ISO 3166-1 alpha-2 code of the country; The second part is a string of up to three alphanumeric characters, which is usually obtained from national sources and stems from coding systems already in use in the country concerned, but may also be developed by the ISO itself. Each complete ISO 3166-2 code can then be used to uniquely identify a country subdivision in a global context.

The list is available in CSV, JSON, PHP, SQL and XML formats in the data/subdivisions folder and the CSV one looks like this:

US,US-AL,Alabama
US,US-AK,Alaska
US,US-AZ,Arizona
US,US-AR,Arkansas
US,US-CA,California
US,US-CO,Colorado

The starting point of this list was the ISO 3166-2 Subdivision Code list provided by IP2Location but this one will be maintained by the community.

🎂 Support the development of this project

Your support means a lot and it keeps me motivated to keep working on open source projects.
If you like this project please ⭐ it by clicking on the star button at the top of the page.
If you are feeling generous, you can buy me a coffee by donating through PayPal, or you can become a sponsor.
Either way - Thank you! 🎉

Star it on GitHub Donate

Installation

The lists are available as a npm package. To install it use:

# the "--save" argument adds the plugin as a dependency in packages.json
npm install world_countries_lists --save

You can install the lists via Composer

composer require stefangabos/world_countries

Alternatively, you can load data from JSDelivr CDN like this:

<script src="https://cdn.jsdelivr.net/npm/world_countries_lists@latest/data/countries/en/countries.json"></script>

Or

download a customized build.

Data formats

SQL

Excerpt from the data/countries/en/countries.sql file:

(250,'fr','fra','France'),
(266,'ga','gab','Gabon'),
(270,'gm','gmb','Gambia'),
(268,'ge','geo','Georgia'),
(276,'de','deu','Germany'),
(288,'gh','gha','Ghana'),
(300,'gr','grc','Greece'),
(308,'gd','grd','Grenada'),

CSV

Excerpt from the data/countries/en/countries.csv file:

250,fr,fra,France
266,ga,gab,Gabon
270,gm,gmb,Gambia
268,ge,geo,Georgia
276,de,deu,Germany
288,gh,gha,Ghana
300,gr,grc,Greece
308,gd,grd,Grenada

JSON

Excerpt from the data/countries/en/countries.json file:

{"id":250,"alpha2":"fr","alpha3":"fra","name":"France"},
{"id":266,"alpha2":"ga","alpha3":"gab","name":"Gabon"},
{"id":270,"alpha2":"gm","alpha3":"gmb","name":"Gambia"},
{"id":268,"alpha2":"ge","alpha3":"geo","name":"Georgia"},
{"id":276,"alpha2":"de","alpha3":"deu","name":"Germany"},
{"id":288,"alpha2":"gh","alpha3":"gha","name":"Ghana"},
{"id":300,"alpha2":"gr","alpha3":"grc","name":"Greece"},
{"id":308,"alpha2":"gd","alpha3":"grd","name":"Grenada"},

Here's a little helper function for searching for a specific country's data

The helper function assumes that the JSON with the countries data is associated with a variable named countries which is in the same scope as the function

The helper function is to be used with the non-combined data sets.
For the combined data sets you can write the function yourself.

//  returns an object with the sought country's data if the search yields a result
//  returns undefined if no results could be found or if argument is incorrect
function search_country(query) {

    // if argument is not valid return false
    if (undefined === query.id && undefined === query.alpha2 && undefined === query.alpha3) return undefined;

        // iterate over the array of countries
    return countries.filter(function(country) {

        // return country's data if
        return (
            // we are searching by ID and we have a match
            (undefined !== query.id && parseInt(country.id, 10) === parseInt(query.id, 10))
            // or we are searching by alpha2 and we have a match
            || (undefined !== query.alpha2 && country.alpha2 === query.alpha2.toLowerCase())
            // or we are searching by alpha3 and we have a match
            || (undefined !== query.alpha3 && country.alpha3 === query.alpha3.toLowerCase())
        )

    // since "filter" returns an array we use pop to get just the data object
    }).pop()

}

Usage

search_country({id: 250})
search_country({alpha2: 'fr'})
search_country({alpha3: 'fra'})

TypeScript

Typings are available (source):

import { Country, LanguageCode, TranslatedCountry } from 'world_countries_lists'

PHP

Excerpt from the data/countries/en/countries.php file:

250 => array('id' => 250, 'alpha2' => 'fr', 'alpha3' => 'fra', 'name' => 'France'),
266 => array('id' => 266, 'alpha2' => 'ga', 'alpha3' => 'gab', 'name' => 'Gabon'),
270 => array('id' => 270, 'alpha2' => 'gm', 'alpha3' => 'gmb', 'name' => 'Gambia'),

Here's a little helper function for searching for a specific country's data

The helper function is to be used with the non-combined data sets.
For the combined data sets you can write the function yourself.

//  this function assumes that you have done this:
$countries = require 'path/to/countries.php';

//  returns an array with the sought country's data if the search yields a result
//  returns false if no results could be found or if argument is incorrect
function search_country($query) {

    // make the countries available in the function
    global $countries;

    // if argument is not valid return false
    if (!isset($query['id']) && !isset($query['alpha2']) && !isset($query['alpha3'])) return false;

    // iterate over the array of countries
    $result = array_filter($countries, function($country) use ($query) {

        // return country's data if
        return (
            // we are searching by ID and we have a match
            (isset($query['id']) && $country['id'] == $query['id'])
            // or we are searching by alpha2 and we have a match
            || (isset($query['alpha2']) && $country['alpha2'] == strtolower($query['alpha2']))
            // or we are searching by alpha3 and we have a match
            || (isset($query['alpha3']) && $country['alpha3'] == strtolower($query['alpha3']))
        );

    });

    // since "array_filter" returns an array we use pop to get just the data object
    // we return false if a result was not found
    return empty($result) ? false : array_pop($result);

}

Usage

search_country(array('id' => 250));
search_country(array('alpha2' => 'fr'));
search_country(array('alpha3' => 'fra'));

XML

Excerpt from the data/countries/en/countries.xml file:

<country id="250" alpha2="fr" alpha3="fra" name="France"/>
<country id="266" alpha2="ga" alpha3="gab" name="Gabon"/>
<country id="270" alpha2="gm" alpha3="gmb" name="Gambia"/>
<country id="268" alpha2="ge" alpha3="geo" name="Georgia"/>
<country id="276" alpha2="de" alpha3="deu" name="Germany"/>
<country id="288" alpha2="gh" alpha3="gha" name="Ghana"/>
<country id="300" alpha2="gr" alpha3="grc" name="Greece"/>
<country id="308" alpha2="gd" alpha3="grd" name="Grenada"/>

Flags

The package also contains the national flags of each country as a 16x16, 24x24, 32x32, 48x48, 64x64 and 128x128 PNG images. The image files are named using the ISO 3166-1-alpha-2 code of the country they represent, for easily pairing flags with countries.

Flag images are also available as single JSON files, one for each of the available sizes, containing all flag images as data-uri

Data sources

Country names in all languages are taken from Wikipedia.

changelog

version 2.9.0 (January 26, 2024)

  • added country names in Farsi; see #12; thanks Mehdi Khodayari!
  • updated country names in various languages

version 2.8.2 (August 18, 2023)

  • fixed missing flags as single JSON files; this fixes #77
  • added missing region; see #79 - thanks RD-PINGEDPNG!
  • updated country names in various languages

version 2.8.1 (April 18, 2023)

  • updated country names for various languages

version 2.8.0 (February 08, 2023)

version 2.7.0 (January 23, 2023)

  • added Serbian and Slovenian languages; see #73; thanks to AppyxDaniel!
  • udpated Mauritania's flag; see #36
  • fixed broken downloads for Chinese and Simplified Chinese languages; see #72; thanks to Roy!
  • added typings for TypeScript users; see #70; thanks to Blake Fischer!

version 2.6.0 (June 19, 2022)

version 2.5.1 (May 10, 2022)

version 2.5.0 (March 30, 2022)

  • added ISO 3166-2 codes; see #61; thanks to Alan Jones for providing the links; what we have at this point should be considered as a start - a lot of codes are missing (according to Wikipedia which says there are 5047 while our list has only 3607) and names are written in their anglicized version rather than local - this will have to be updated by the community
  • added Esperanto language
  • updated formatting and usage of PHP files; thanks to Jacek Karczmarczyk for suggesting - see #58

version 2.4.0 (February 06, 2022)

  • added XML file format
  • added combined lists; see #29
  • aded a note about the fact that alpha-2 and alpha-3 codes are lowercase instead of uppercase as defined by ISO 3166-1; see #50
  • fixed id of Sudan for Swedish language
  • fixed broken SQL files
  • changed folder structure in preparations for adding more data to the library; for now, countries moved to their own countries folder inside the data folder and the flags folder was moved inside the data folder

version 2.3.0 (September 28, 2021)

  • flags are now available as JSON files with data uris; see #41
  • fixed folder name for Estonian translations; see #52
  • fixed a potential issue with the lists for German language by removing a soft-hyphen (\u00ad) from country names; see #54
  • fixed broken download links for Basque

version 2.2.0 (June 06, 2021)

  • added Bulgarian translations
  • updated country names for various languages

version 2.1.1 (October 17, 2020)

version 2.1.0 (October 15, 2020)

  • added country names in Finnish; see #42, thanks Alex Garcia!
  • use official short name for Belarus for German language; see #40

version 2.0.0 (September 25, 2020)

  • added Basque translation; thanks erral!
  • changed data folder name for Chinese (Simplified) from cn to zh to correctly reflect ISO-639 language codes
  • changed data folder name for Chinese (Traditional) from cn to zh-tw to correctly reflect ISO-639 language codes
  • changed data folder name for Estonian from et to ee to correctly reflect ISO-639 language codes
  • updates to some country names in Arabic, German, Ukrainian, Traditional Chinese

version 1.1.1 (August 02, 2020)

  • fixed issues with Traditional Chinese

version 1.1.0 (August 02, 2020)

  • added Trasitional Chinese translations; see #35
  • added Korean translations; see #34
  • updates to some country names in Spanish, Swedish and Thai

version 1.0.2 (May 22, 2020)

  • added Swedish translations
  • Chinese translations are now in simplified Chinese instead of a mix (see #32); thanks Sun Jiao for the help!
  • fixed #33 where the country code of Sudan, for some translations, it was saved as string instead of int in the JSON format - thanks Ruben Lie King for reporting!
  • all data in files is up to date as of May 22nd, 2020

version 1.0.0 (April 02, 2019)

  • first re-release since moving to GitHub

version 1.2.1 (January 26, 2016)

  • the list is now available on GitHub
  • flags are now availabel also in 24x24, 32x32, 48x48, 64x64 and 128x128 pixels

version 1.2 (December 09, 2012)

  • added new country and codes: "South Sudan" (thanks to Jackman)
  • changed the ISO alpha-3 code of "Congo" from "cod" to "cog"
  • added "cod" ISO alpha-3 code to "Congo, The Democratic Republic of the"
  • added "ggy" ISO alpha-3 code to "Guernsey"
  • added "jey" ISO alpha-3 code to "Jersey"
  • added "myt" ISO alpha-3 code to "Mayotte"
  • added "maf" ISO alpha-3 code to "Saint Martin (French Part)
  • added "sxm" ISO alpha-3 code to "Sint Maarten" (Dutch Part)
  • alpha-2 and alpha-3 codes are now lowercase and the countries' names are no longer all-caps
  • replaced the old flag icons dating from 2007 and with missing images for quite a few countries, with new ones by IconDrawer

version 1.1 (June 27, 2011)

  • updated the list

version 1.0 (January 30, 2006)

  • initial release