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