6 * Updates CLDR codes in CountryManager.php to latest data.
8 * We rely on the CLDR data set, because it is easily accessible, scriptable,
9 * and in the right human-readable format.
12 // Determine DRUPAL_ROOT.
13 $dir = dirname(__FILE__);
14 while (!defined('DRUPAL_ROOT')) {
15 if (is_dir($dir . '/core')) {
16 define('DRUPAL_ROOT', $dir);
21 // Determine source data file URI to process.
22 $uri = DRUPAL_ROOT . '/territories.json';
24 if (!file_exists($uri)) {
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.
32 exit('CLDR data file not found. (' . $uri . ")\n\n" . $usage . "\n");
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')) {
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;
51 // Parse the source data into an array.
52 $data = json_decode(file_get_contents($uri));
54 foreach ($data->main->en->localeDisplayNames->territories as $code => $name) {
55 // Use any alternate codes the Drupal community wishes to.
57 // 'CI-alt-variant', // Use CI-alt-variant instead of the CI entry.
59 if (in_array($code, $alt_codes)) {
60 // Just use the first 2 character part of the alt code.
61 $code = strtok($code, '-');
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".
69 if (in_array($code, $exclude_codes)) {
73 // Ignore every territory that doesn't have a 2 character code.
74 if (strlen($code) !== 2) {
77 $countries[(string) $code] = $name;
79 if (empty($countries)) {
80 echo 'ERROR: Did not find expected country names.' . PHP_EOL;
83 // Sort by country code (to minimize diffs).
88 foreach ($countries as $code => $name) {
89 // For .po translation file's sake, use double-quotes instead of escaped
91 $name = (strpos($name, '\'') !== FALSE ? '"' . $name . '"' : "'" . $name . "'");
92 $out .= ' ' . var_export($code, TRUE) . ' => t(' . $name . '),' . "\n";
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);