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