pastebin

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