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