You are here

update-countries.sh in Drupal 9

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