annotate core/scripts/update-countries.sh @ 0:4c8ae668cc8c

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