update-countries.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // Don't allow "Unknown Region".
  61. 'ZZ',
  62. ];
  63. if (in_array($code, $exclude_codes)) {
  64. continue;
  65. }
  66. // Ignore every territory that doesn't have a 2 character code.
  67. if (strlen($code) !== 2) {
  68. continue;
  69. }
  70. $countries[(string) $code] = $name;
  71. }
  72. if (empty($countries)) {
  73. echo 'ERROR: Did not find expected country names.' . PHP_EOL;
  74. exit;
  75. }
  76. // Sort by country code (to minimize diffs).
  77. ksort($countries);
  78. // Produce PHP code.
  79. $out = '';
  80. foreach ($countries as $code => $name) {
  81. // For .po translation file's sake, use double-quotes instead of escaped
  82. // single-quotes.
  83. $name = (strpos($name, '\'') !== FALSE ? '"' . $name . '"' : "'" . $name . "'");
  84. $out .= ' ' . var_export($code, TRUE) . ' => t(' . $name . '),' . "\n";
  85. }
  86. // Replace the actual PHP code in standard.inc.
  87. $file = DRUPAL_ROOT . '/core/lib/Drupal/Core/Locale/CountryManager.php';
  88. $content = file_get_contents($file);
  89. $content = preg_replace('/(\$countries = \[\n)(.+?)(^\s+\];)/ms', '$1' . $out . '$3', $content, -1, $count);
  90. file_put_contents($file, $content);