Commit 6f29144a authored by Dries's avatar Dries

Issue #1938892 by JohnAlbin, Pancho: Fixed Switch from ISO-3166-1 country data...

Issue #1938892 by JohnAlbin, Pancho: Fixed Switch from ISO-3166-1 country data to CLDR unicode data.
parent 2a993a89
......@@ -36,13 +36,14 @@ public function __construct(ModuleHandlerInterface $module_handler) {
}
/**
* Get an array of all ISO 3166-1 alpha-2 country code => country name pairs.
* Get an array of all two-letter country code => country name pairs.
*
* @return array
* An array of country code => country name pairs.
*/
public static function getStandardList() {
$countries = array(
'AC' => t('Ascension Island'),
'AD' => t('Andorra'),
'AE' => t('United Arab Emirates'),
'AF' => t('Afghanistan'),
......@@ -71,9 +72,9 @@ public static function getStandardList() {
'BJ' => t('Benin'),
'BL' => t('Saint Barthélemy'),
'BM' => t('Bermuda'),
'BN' => t('Brunei Darussalam'),
'BO' => t('Bolivia, Plurinational State of'),
'BQ' => t('Bonaire, Sint Eustatius and Saba'),
'BN' => t('Brunei'),
'BO' => t('Bolivia'),
'BQ' => t('Caribbean Netherlands'),
'BR' => t('Brazil'),
'BS' => t('Bahamas'),
'BT' => t('Bhutan'),
......@@ -82,17 +83,18 @@ public static function getStandardList() {
'BY' => t('Belarus'),
'BZ' => t('Belize'),
'CA' => t('Canada'),
'CC' => t('Cocos (Keeling) Islands'),
'CD' => t('Congo, The Democratic Republic of the'),
'CC' => t('Cocos [Keeling] Islands'),
'CD' => t('Congo - Kinshasa'),
'CF' => t('Central African Republic'),
'CG' => t('Congo'),
'CG' => t('Congo - Brazzaville'),
'CH' => t('Switzerland'),
'CI' => t("Côte d'Ivoire"),
'CI' => t('Côte d’Ivoire'),
'CK' => t('Cook Islands'),
'CL' => t('Chile'),
'CM' => t('Cameroon'),
'CN' => t('China'),
'CO' => t('Colombia'),
'CP' => t('Clipperton Island'),
'CR' => t('Costa Rica'),
'CU' => t('Cuba'),
'CV' => t('Cape Verde'),
......@@ -101,11 +103,13 @@ public static function getStandardList() {
'CY' => t('Cyprus'),
'CZ' => t('Czech Republic'),
'DE' => t('Germany'),
'DG' => t('Diego Garcia'),
'DJ' => t('Djibouti'),
'DK' => t('Denmark'),
'DM' => t('Dominica'),
'DO' => t('Dominican Republic'),
'DZ' => t('Algeria'),
'EA' => t('Ceuta and Melilla'),
'EC' => t('Ecuador'),
'EE' => t('Estonia'),
'EG' => t('Egypt'),
......@@ -115,8 +119,8 @@ public static function getStandardList() {
'ET' => t('Ethiopia'),
'FI' => t('Finland'),
'FJ' => t('Fiji'),
'FK' => t('Falkland Islands (Malvinas)'),
'FM' => t('Micronesia, Federated States of'),
'FK' => t('Falkland Islands'),
'FM' => t('Micronesia'),
'FO' => t('Faroe Islands'),
'FR' => t('France'),
'GA' => t('Gabon'),
......@@ -138,12 +142,13 @@ public static function getStandardList() {
'GU' => t('Guam'),
'GW' => t('Guinea-Bissau'),
'GY' => t('Guyana'),
'HK' => t('Hong Kong'),
'HK' => t('Hong Kong SAR China'),
'HM' => t('Heard Island and McDonald Islands'),
'HN' => t('Honduras'),
'HR' => t('Croatia'),
'HT' => t('Haiti'),
'HU' => t('Hungary'),
'IC' => t('Canary Islands'),
'ID' => t('Indonesia'),
'IE' => t('Ireland'),
'IL' => t('Israel'),
......@@ -151,7 +156,7 @@ public static function getStandardList() {
'IN' => t('India'),
'IO' => t('British Indian Ocean Territory'),
'IQ' => t('Iraq'),
'IR' => t('Iran, Islamic Republic of'),
'IR' => t('Iran'),
'IS' => t('Iceland'),
'IT' => t('Italy'),
'JE' => t('Jersey'),
......@@ -164,12 +169,12 @@ public static function getStandardList() {
'KI' => t('Kiribati'),
'KM' => t('Comoros'),
'KN' => t('Saint Kitts and Nevis'),
'KP' => t("Korea, Democratic People's Republic of"),
'KR' => t('Korea, Republic of'),
'KP' => t('North Korea'),
'KR' => t('South Korea'),
'KW' => t('Kuwait'),
'KY' => t('Cayman Islands'),
'KZ' => t('Kazakhstan'),
'LA' => t("Lao People's Democratic Republic"),
'LA' => t('Laos'),
'LB' => t('Lebanon'),
'LC' => t('Saint Lucia'),
'LI' => t('Liechtenstein'),
......@@ -182,16 +187,16 @@ public static function getStandardList() {
'LY' => t('Libya'),
'MA' => t('Morocco'),
'MC' => t('Monaco'),
'MD' => t('Moldova, Republic of'),
'MD' => t('Moldova'),
'ME' => t('Montenegro'),
'MF' => t('Saint Martin (French part)'),
'MF' => t('Saint Martin'),
'MG' => t('Madagascar'),
'MH' => t('Marshall Islands'),
'MK' => t('Macedonia, Republic of'),
'MK' => t('Macedonia'),
'ML' => t('Mali'),
'MM' => t('Myanmar'),
'MM' => t('Myanmar [Burma]'),
'MN' => t('Mongolia'),
'MO' => t('Macao'),
'MO' => t('Macau SAR China'),
'MP' => t('Northern Mariana Islands'),
'MQ' => t('Martinique'),
'MR' => t('Mauritania'),
......@@ -224,17 +229,18 @@ public static function getStandardList() {
'PK' => t('Pakistan'),
'PL' => t('Poland'),
'PM' => t('Saint Pierre and Miquelon'),
'PN' => t('Pitcairn'),
'PN' => t('Pitcairn Islands'),
'PR' => t('Puerto Rico'),
'PS' => t('Palestine, State of'),
'PS' => t('Palestinian Territories'),
'PT' => t('Portugal'),
'PW' => t('Palau'),
'PY' => t('Paraguay'),
'QA' => t('Qatar'),
'QO' => t('Outlying Oceania'),
'RE' => t('Réunion'),
'RO' => t('Romania'),
'RS' => t('Serbia'),
'RU' => t('Russian Federation'),
'RU' => t('Russia'),
'RW' => t('Rwanda'),
'SA' => t('Saudi Arabia'),
'SB' => t('Solomon Islands'),
......@@ -242,7 +248,7 @@ public static function getStandardList() {
'SD' => t('Sudan'),
'SE' => t('Sweden'),
'SG' => t('Singapore'),
'SH' => t('Saint Helena, Ascension and Tristan da Cunha'),
'SH' => t('Saint Helena'),
'SI' => t('Slovenia'),
'SJ' => t('Svalbard and Jan Mayen'),
'SK' => t('Slovakia'),
......@@ -252,11 +258,12 @@ public static function getStandardList() {
'SO' => t('Somalia'),
'SR' => t('Suriname'),
'SS' => t('South Sudan'),
'ST' => t('Sao Tome and Principe'),
'ST' => t('São Tomé and Príncipe'),
'SV' => t('El Salvador'),
'SX' => t('Sint Maarten (Dutch part)'),
'SY' => t('Syrian Arab Republic'),
'SX' => t('Sint Maarten'),
'SY' => t('Syria'),
'SZ' => t('Swaziland'),
'TA' => t('Tristan da Cunha'),
'TC' => t('Turks and Caicos Islands'),
'TD' => t('Chad'),
'TF' => t('French Southern Territories'),
......@@ -271,23 +278,24 @@ public static function getStandardList() {
'TR' => t('Turkey'),
'TT' => t('Trinidad and Tobago'),
'TV' => t('Tuvalu'),
'TW' => t('Taiwan, Province of China'),
'TZ' => t('Tanzania, United Republic of'),
'TW' => t('Taiwan'),
'TZ' => t('Tanzania'),
'UA' => t('Ukraine'),
'UG' => t('Uganda'),
'UM' => t('United States Minor Outlying Islands'),
'UM' => t('U.S. Outlying Islands'),
'US' => t('United States'),
'UY' => t('Uruguay'),
'UZ' => t('Uzbekistan'),
'VA' => t('Holy See (Vatican City State)'),
'VA' => t('Vatican City'),
'VC' => t('Saint Vincent and the Grenadines'),
'VE' => t('Venezuela, Bolivarian Republic of'),
'VG' => t('Virgin Islands, British'),
'VI' => t('Virgin Islands, U.S.'),
'VN' => t('Viet Nam'),
'VE' => t('Venezuela'),
'VG' => t('British Virgin Islands'),
'VI' => t('U.S. Virgin Islands'),
'VN' => t('Vietnam'),
'VU' => t('Vanuatu'),
'WF' => t('Wallis and Futuna'),
'WS' => t('Samoa'),
'XK' => t('Kosovo'),
'YE' => t('Yemen'),
'YT' => t('Mayotte'),
'ZA' => t('South Africa'),
......
......@@ -3468,7 +3468,7 @@ function hook_updater_info_alter(&$updaters) {
* Alter the default country list.
*
* @param $countries
* The associative array of countries keyed by ISO 3166-1 country code.
* The associative array of countries keyed by two-letter country code.
*
* @see \Drupal\Core\Locale\CountryManager::getList().
*/
......
......@@ -3,15 +3,14 @@
/**
* @file
* Updates ISO-3166 codes in standard.inc to latest data.
* Updates CLDR codes in CountryManager.php to latest data.
*
* We rely on the Debian ISO code repository, because it is easily accessible,
* scriptable, in the right human-readable format, and all changes went through
* sufficient FOSS community discussion already.
* We rely on the CLDR data set, because it is easily accessible, scriptable,
* and in the right human-readable format.
*/
// Determine DRUPAL_ROOT.
$cwd = $dir = dirname(__FILE__);
$dir = dirname(__FILE__);
while (!defined('DRUPAL_ROOT')) {
if (is_dir($dir . '/core')) {
define('DRUPAL_ROOT', $dir);
......@@ -20,15 +19,25 @@
}
// Determine source data file URI to process.
$uri = $cwd . '/iso_3166.xml';
// Despite its actual file name, at least Firefox merges and converts the path
// and filename into a combined filename.
$uri = DRUPAL_ROOT . '/territories.json';
if (!file_exists($uri)) {
$uri = $cwd . '/iso_3166_iso_3166.xml';
$usage = <<< USAGE
- Choose the latest release data from http://cldr.unicode.org/index/downloads
and download the json.zip file.
- Unzip the json.zip file and place the main/en/territories.json in the
Drupal root directory.
- Run this script.
USAGE;
exit('CLDR data file not found. (' . $uri . ")\n\n" . $usage . "\n");
}
// Fall back and default to original Debian source.
if (!file_exists($uri)) {
$uri = 'http://anonscm.debian.org/gitweb/?p=iso-codes/iso-codes.git;a=blob_plain;f=iso_3166/iso_3166.xml;hb=master';
// Fake the t() function used in CountryManager.php instead of attempting a full
// Drupal bootstrap of core/includes/bootstrap.inc (where t() is declared).
if (!function_exists('t')) {
function t($string) {
return $string;
}
}
// Read in existing codes.
......@@ -40,17 +49,35 @@
$countries = $existing_countries;
// Parse the source data into an array.
$data = simplexml_load_file($uri);
foreach ($data->iso_3166_entry as $entry) {
// Ignore every territory that doesn't have a alpha-2 code.
if (!isset($entry['alpha_2_code'])) {
$data = json_decode(file_get_contents($uri));
foreach ($data->main->en->localeDisplayNames->territories as $code => $name) {
// Use any alternate codes the Drupal community wishes to.
$alt_codes = array(
// 'CI-alt-variant', // Use CI-alt-variant instead of the CI entry.
);
if (in_array($code, $alt_codes)) {
// Just use the first 2 character part of the alt code.
$code = strtok($code, '-');
}
// Skip any codes we wish to exclude from our country list.
$exclude_codes = array(
'EU', // The European Union is not a country.
'ZZ', // Don't allow "Unknown Region".
);
if (in_array($code, $exclude_codes)) {
continue;
}
// Ignore every territory that doesn't have a 2 character code.
if (strlen($code) !== 2) {
continue;
}
$name = isset($entry['name']) ? (string) $entry['name'] : (string) $entry['official_name'];
$countries[(string) $entry['alpha_2_code']] = $name;
$countries[(string) $code] = $name;
}
if (empty($countries)) {
echo 'ERROR: Did not find expected alpha_2_code country names.' . PHP_EOL;
echo 'ERROR: Did not find expected country names.' . PHP_EOL;
exit;
}
// Sort by country code (to minimize diffs).
......@@ -66,7 +93,7 @@
}
// Replace the actual PHP code in standard.inc.
$file = DRUPAL_ROOT . '/core/includes/standard.inc';
$file = DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
$content = file_get_contents($file);
$content = preg_replace('/(\$countries = array\(\n)(.+?)(^\s+\);)/ms', '$1' . $out . '$3', $content);
file_put_contents($file, $content);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment