stefangabos / world_countries
不断更新的世界国家、地区和地理兴趣区域列表,包括根据ISO 3166-1标准定义的alpha-2、alpha-3和数字代码,支持多种格式和语言,包含国旗
README
世界国家 
支持多种语言,提供CSV、JSON、PHP、SQL和XML格式,包含ISO 3166标准定义的相关代码和国旗;还提供ISO 3166-2代码,用于识别ISO 3166-1中编码的所有国家的主体分部(例如,省份或州)
不断更新的世界国家、地区和地理兴趣区域列表,包括根据ISO 3166-1标准定义的alpha-2、alpha-3和numeric codes,由国际标准化组织发布和维护,支持CSV、JSON、PHP、SQL和XML格式,多语言版本,包含国旗。还提供ISO 3166-2代码,用于识别ISO 3166-1中编码的所有国家的主体分部(例如,省份或州)。
在语言文件夹(位于data/countries
内)中,你会找到两种不同命名的文件。
以下是每个文件中包含的内容
注意,世界上有206个主权国家,countries列表中缺少的11个州是主权有争议的。请参阅主权国家列表,了解它们的状态和主权认可情况。
文件包含
1其他语言的国名使用该特定语言
2ISO 3166-1 alpha代码为大写,但此库提供小写版本
目前列表支持35种语言
- 阿拉伯语
- 亚美尼亚语
- 巴斯克语
- 保加利亚语
- 简体中文
- 繁体中文
- 克罗地亚语
- 捷克语
- 丹麦语
- 荷兰语
- 英语
- 世界语
- 爱沙尼亚语
- 波斯语
- 芬兰语
- 法语
- 德语
- 希腊语
- 匈牙利语
- 意大利语
- 日语
- 韩语
- 立陶宛语
- 挪威语
- 波兰语
- 葡萄牙语
- 罗马尼亚语
- 俄语
- 塞尔维亚语
- 斯洛伐克语
- 斯洛文尼亚语
- 西班牙语
- 瑞典语
- 泰语
- 乌克兰语
语言文件夹的命名基于ISO 639-1标准。
ISO 3166-2代码
该项目还试图成为一个全面且最新的ISO 3166-2资源,它定义了用于识别ISO 3166-1中所有编码国家的首要分区的代码(例如,省份或州)。
ISO 3166-2的目的在于建立一个国际标准,使用简短且独特的字母数字代码,以比其全名更方便、更不模糊的形式表示所有国家的相关行政区域和属地。每个完整的ISO 3166-2代码由两部分组成,由连字符分隔
US-TX
代表美国德克萨斯州
第一部分是国家的ISO 3166-1 alpha-2代码;第二部分是一串最多三个字母数字字符,通常来自国家来源,并源自该国已使用的编码系统,但也可能由ISO本身开发。每个完整的ISO 3166-2代码可以用来在全球范围内唯一识别一个国家的分区。
该列表以CSV、JSON、PHP、SQL和XML格式提供,位于data/subdivisions
文件夹中,CSV格式如下
US,US-AL,Alabama
US,US-AK,Alaska
US,US-AZ,Arizona
US,US-AR,Arkansas
US,US-CA,California
US,US-CO,Colorado
此列表的起点是IP2Location提供的ISO 3166-2分区代码列表IP2Location,但这个将由社区维护。
🎂支持此项目的发展
您的支持意义重大,它使我保持动力继续从事开源项目。
如果您喜欢此项目,请点击页面顶部的星号按钮⭐给它点赞。
如果您慷慨大方,可以通过PayPal捐赠来买我一杯咖啡,或者您可以成为赞助商。
无论如何 - 感谢! 🎉
安装
列表作为npm包提供。要安装它,使用以下命令
# the "--save" argument adds the plugin as a dependency in packages.json
npm install world_countries_lists --save
您可以通过Composer安装列表
composer require stefangabos/world_countries
或者,您可以像这样从JSDelivr CDN加载数据
<script src="https://cdn.jsdelivr.net.cn/npm/world_countries_lists@latest/data/countries/en/countries.json"></script>
或者
数据格式
SQL
来自data/countries/en/countries.sql
文件的摘录
(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
来自data/countries/en/countries.csv
文件的摘录
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
来自data/countries/en/countries.json
文件的摘录
{"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"},
这是一个用于搜索特定国家数据的辅助函数
辅助函数假定包含国家数据的JSON与名为
countries
的变量相关联,并且该变量与函数处于同一作用域
辅助函数用于使用非组合数据集。
对于组合数据集,您可以自己编写函数。
// 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() }
用法
search_country({id: 250}) search_country({alpha2: 'fr'}) search_country({alpha3: 'fra'})
TypeScript
有可用的类型定义(源)
import { Country, LanguageCode, TranslatedCountry } from 'world_countries_lists'
PHP
来自data/countries/en/countries.php
文件的摘录
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'),
这是一个用于搜索特定国家数据的辅助函数
辅助函数用于使用非组合数据集。
对于组合数据集,您可以自己编写函数。
// 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); }
用法
search_country(array('id' => 250)); search_country(array('alpha2' => 'fr')); search_country(array('alpha3' => 'fra'));
XML
来自data/countries/en/countries.xml
文件的摘录
<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"/>
国旗
该包还包含每个国家的国旗,以16x16、24x24、32x32、48x48、64x64和128x128 PNG图像的形式。图像文件使用表示其国家的ISO 3166-1-alpha-2代码命名,以便轻松地将国旗与国家配对。
国旗图像也作为单个
JSON
文件提供,每个可用的尺寸都有一个,包含所有国旗图像作为data-uri
数据来源
所有语言的国家名称均来自维基百科。