update-countries.sh 3.1 KB

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