$countryName) { if (is_numeric($countryCode) || in_array($countryCode, $ignoredCountries)) { // Ignore continents, regions, uninhabited islands. continue; } if (strpos($countryCode, '-alt-') !== FALSE) { // Ignore alternative names. continue; } $baseData[$countryCode]['code'] = $countryCode; // Countries are not guaranteed to have an alpha3 and/or numeric code. if (isset($codeMappings[$countryCode]['_alpha3'])) { $baseData[$countryCode]['three_letter_code'] = $codeMappings[$countryCode]['_alpha3']; } if (isset($codeMappings[$countryCode]['_numeric'])) { $baseData[$countryCode]['numeric_code'] = $codeMappings[$countryCode]['_numeric']; } // Determine the telephone code for this country. if (in_array($countryCode, array('IC', 'EA'))) { // "Canary Islands" and "Ceuta and Melilla" use Spain's. $baseData[$countryCode]['telephone_code'] = $telephoneCodeData['ES'][0]['telephoneCountryCode']; } elseif ($countryCode == 'XK') { // Kosovo uses three telephone codes. Use Serbia's until that gets resolved. $baseData[$countryCode]['telephone_code'] = $telephoneCodeData['RS'][0]['telephoneCountryCode']; } elseif (isset($telephoneCodeData[$countryCode])) { $baseData[$countryCode]['telephone_code'] = $telephoneCodeData[$countryCode][0]['telephoneCountryCode']; } } // Write out base.json. ksort($baseData); $json = json_encode($baseData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); file_put_contents('base.json', $json); // Gather available locales. $locales = array(); if ($handle = opendir('../json-full/main')) { while (false !== ($entry = readdir($handle))) { if (substr($entry, 0, 1) != '.') { $entryParts = explode('-', $entry); if (!in_array($entry, $ignoredLocales) && !in_array($entryParts[0], $ignoredLocales)) { $locales[] = $entry; } } } closedir($handle); } // Create the localizations. $countries = array(); foreach ($locales as $locale) { $data = json_decode(file_get_contents('../json-full/main/' . $locale . '/territories.json'), true); $data = $data['main'][$locale]['localeDisplayNames']['territories']; foreach ($data as $countryCode => $countryName) { if (isset($baseData[$countryCode])) { // This country name is untranslated, use the english version. if ($countryCode == $countryName) { $countryName = $countryData[$countryCode]; } $countries[$locale][$countryCode] = array( 'name' => $countryName, ); } } } // Identify localizations that are the same as the ones for the parent locale. // For example, "fr-FR" if "fr" has the same data. $duplicates = array(); foreach ($countries as $locale => $localizedCountries) { if (strpos($locale, '-') !== FALSE) { $localeParts = explode('-', $locale); array_pop($localeParts); $parentLocale = implode('-', $localeParts); $diff = array_udiff($localizedCountries, $countries[$parentLocale], function ($first, $second) { return ($first['name'] == $second['name']) ? 0 : 1; }); if (empty($diff)) { // The duplicates are not removed right away because they might // still be needed for other duplicate checks (for example, // when there are locales like bs-Latn-BA, bs-Latn, bs). $duplicates[] = $locale; } } } // Remove the duplicates. foreach ($duplicates as $locale) { unset($countries[$locale]); } // Write out the localizations. foreach ($countries as $locale => $localizedCountries) { $collator = collator_create($locale); uasort($localizedCountries, function($a, $b) use ($collator) { return collator_compare($collator, $a['name'], $b['name']); }); $json = json_encode($localizedCountries, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); file_put_contents($locale . '.json', $json); }