update-countries.sh

Same filename in other branches
  1. 9 core/scripts/update-countries.sh
  2. 8.9.x 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. // cspell:ignore localenames
  12. // Determine DRUPAL_ROOT.
  13. $dir = dirname(__FILE__);
  14. while (!defined('DRUPAL_ROOT')) {
  15. if (is_dir($dir . '/core')) {
  16. define('DRUPAL_ROOT', $dir);
  17. }
  18. $dir = dirname($dir);
  19. }
  20. // Determine source data file URI to process.
  21. $uri = DRUPAL_ROOT . '/territories.json';
  22. if (!file_exists($uri)) {
  23. $usage = <<< USAGE
  24. - Download territories.json from
  25. https://github.com/unicode-org/cldr-json/blob/main/cldr-json/cldr-localenames-full/main/en/territories.json
  26. and place it in the Drupal root directory.
  27. - Run this script.
  28. USAGE;
  29. exit('CLDR data file not found. (' . $uri . ")\n\n" . $usage . "\n");
  30. }
  31. // Read in existing codes.
  32. // @todo Allow to remove previously existing country codes.
  33. // @see https://www.drupal.org/node/1436754
  34. require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManagerInterface.php';
  35. require_once DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
  36. $existing_countries = CountryManager::getStandardList();
  37. $countries = $existing_countries;
  38. // Parse the source data into an array.
  39. $data = json_decode(file_get_contents($uri));
  40. foreach ($data->main->en->localeDisplayNames->territories as $code => $name) {
  41. // Use any alternate codes the Drupal community wishes to.
  42. $alt_codes = [
  43. // 'CI-alt-variant', // Use CI-alt-variant instead of the CI entry.
  44. ];
  45. if (in_array($code, $alt_codes)) {
  46. // Just use the first 2 character part of the alt code.
  47. $code = strtok($code, '-');
  48. }
  49. // Skip any codes we wish to exclude from our country list.
  50. $exclude_codes = [
  51. // The European Union is not a country.
  52. 'EU',
  53. // The Eurozone is not a country.
  54. 'EZ',
  55. // The United Nations is not a country.
  56. 'UN',
  57. // "Pseudo-Accents" is not a country.
  58. 'XA',
  59. // "Pseudo-Bidi" is not a country.
  60. 'XB',
  61. // Don't allow "Unknown Region".
  62. 'ZZ',
  63. ];
  64. if (in_array($code, $exclude_codes)) {
  65. continue;
  66. }
  67. // Ignore every territory that doesn't have a 2 character code.
  68. if (strlen($code) !== 2) {
  69. continue;
  70. }
  71. $countries[(string) $code] = $name;
  72. }
  73. if (empty($countries)) {
  74. echo 'ERROR: Did not find expected country names.' . PHP_EOL;
  75. exit;
  76. }
  77. // Sort by country code (to minimize diffs).
  78. ksort($countries);
  79. // Produce PHP code.
  80. $out = '';
  81. foreach ($countries as $code => $name) {
  82. // For .po translation file's sake, use double-quotes instead of escaped
  83. // single-quotes.
  84. $name = str_contains($name, '\'' ? '"' . $name . '"' : "'" . $name . "'");
  85. $out .= ' ' . var_export($code, TRUE) . ' => t(' . $name . '),' . "\n";
  86. }
  87. // Replace the actual PHP code in standard.inc.
  88. $file = DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
  89. $content = file_get_contents($file);
  90. $content = preg_replace('/(\$countries = \[\n)(.+?)(^\s+\];)/ms', '$1' . $out . '$3', $content, -1, $count);
  91. file_put_contents($file, $content);

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