update-countries.sh

Same filename in other branches
  1. 9 core/scripts/update-countries.sh
  2. 10 core/scripts/update-countries.sh
  3. 11.x core/scripts/update-countries.sh

Updates CLDR codes in CountryManager.php to latest data.

We rely on the CLDR data set, because it is easily accessible, scriptable, and in the right human-readable format.

File

core/scripts/update-countries.sh

View source
  1. #!/bin/php
  2. <?php
  3. /**
  4. * @file
  5. * Updates CLDR codes in CountryManager.php to latest data.
  6. *
  7. * We rely on the CLDR data set, because it is easily accessible, scriptable,
  8. * and in the right human-readable format.
  9. */
  10. use Drupal\Core\Locale\CountryManager;
  11. // Determine DRUPAL_ROOT.
  12. $dir = dirname(__FILE__);
  13. while (!defined('DRUPAL_ROOT')) {
  14. if (is_dir($dir . '/core')) {
  15. define('DRUPAL_ROOT', $dir);
  16. }
  17. $dir = dirname($dir);
  18. }
  19. // Determine source data file URI to process.
  20. $uri = DRUPAL_ROOT . '/territories.json';
  21. if (!file_exists($uri)) {
  22. $usage = <<< USAGE
  23. - Choose the latest release data from http://cldr.unicode.org/index/downloads
  24. and download the json.zip file.
  25. - Unzip the json.zip file and place the main/en/territories.json in the
  26. Drupal root directory.
  27. - Run this script.
  28. USAGE;
  29. exit('CLDR data file not found. (' . $uri . ")\n\n" . $usage . "\n");
  30. }
  31. // Fake the t() function used in CountryManager.php instead of attempting a full
  32. // Drupal bootstrap of core/includes/bootstrap.inc (where t() is declared).
  33. if (!function_exists('t')) {
  34. function t($string) {
  35. return $string;
  36. }
  37. }
  38. // Read in existing codes.
  39. // @todo Allow to remove previously existing country codes.
  40. // @see https://www.drupal.org/node/1436754
  41. require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManagerInterface.php';
  42. require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
  43. $existing_countries = CountryManager::getStandardList();
  44. $countries = $existing_countries;
  45. // Parse the source data into an array.
  46. $data = json_decode(file_get_contents($uri));
  47. foreach ($data->main->en->localeDisplayNames->territories as $code => $name) {
  48. // Use any alternate codes the Drupal community wishes to.
  49. $alt_codes = [
  50. // 'CI-alt-variant', // Use CI-alt-variant instead of the CI entry.
  51. ];
  52. if (in_array($code, $alt_codes)) {
  53. // Just use the first 2 character part of the alt code.
  54. $code = strtok($code, '-');
  55. }
  56. // Skip any codes we wish to exclude from our country list.
  57. $exclude_codes = [
  58. // The European Union is not a country.
  59. 'EU',
  60. // The Eurozone is not a country.
  61. 'EZ',
  62. // The United Nations is not a country.
  63. 'UN',
  64. // "Pseudo-Accents" is not a country.
  65. 'XA',
  66. // "Pseudo-Bidi" is not a country.
  67. 'XB',
  68. // Don't allow "Unknown Region".
  69. 'ZZ',
  70. ];
  71. if (in_array($code, $exclude_codes)) {
  72. continue;
  73. }
  74. // Ignore every territory that doesn't have a 2 character code.
  75. if (strlen($code) !== 2) {
  76. continue;
  77. }
  78. $countries[(string) $code] = $name;
  79. }
  80. if (empty($countries)) {
  81. echo 'ERROR: Did not find expected country names.' . PHP_EOL;
  82. exit;
  83. }
  84. // Sort by country code (to minimize diffs).
  85. ksort($countries);
  86. // Produce PHP code.
  87. $out = '';
  88. foreach ($countries as $code => $name) {
  89. // For .po translation file's sake, use double-quotes instead of escaped
  90. // single-quotes.
  91. $name = (strpos($name, '\'') !== FALSE ? '"' . $name . '"' : "'" . $name . "'");
  92. $out .= ' ' . var_export($code, TRUE) . ' => t(' . $name . '),' . "\n";
  93. }
  94. // Replace the actual PHP code in standard.inc.
  95. $file = DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
  96. $content = file_get_contents($file);
  97. $content = preg_replace('/(\$countries = \[\n)(.+?)(^\s+\];)/ms', '$1' . $out . '$3', $content, -1, $count);
  98. file_put_contents($file, $content);

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.