-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.php
More file actions
455 lines (382 loc) · 17.5 KB
/
Copy pathgenerate.php
File metadata and controls
455 lines (382 loc) · 17.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
declare(strict_types=1);
require_once 'vendor/autoload.php';
use Sokil\IsoCodes\IsoCodesFactory;
use Sokil\IsoCodes\TranslationDriver\SymfonyTranslationDriver;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\Loader\MoFileLoader;
// Strip trailing iso-codes annotation brackets from a subdivision name.
// Upstream encodes alt-script names and parent_code references as " [...]"
// suffixes (e.g. "Wales [Cymru GB-CYM]", "Stockholms län [SE-01]"). See #5.
function stripIsoBrackets(string $name): string
{
return trim(preg_replace('/\s*\[[^\]]+\]\s*$/u', '', $name));
}
// Source the locale list from the iso-codes translation directory rather than
// ResourceBundle::getLocales(''), so output is reproducible across hosts. ICU's
// locale enumeration varies by version (e.g. newer ICU surfaces `ba`/`nso`
// where older ICU does not); the .mo files shipped in this composer package
// are the authoritative source of translations.
$translationBasePath = 'vendor/sokil/php-isocodes-db-i18n/messages';
if (!is_dir($translationBasePath)) {
echo "✗ Translation directory not found: $translationBasePath\n";
exit(1);
}
$allIntlLocales = [];
foreach (scandir($translationBasePath) as $dir) {
if ($dir === '.' || $dir === '..') {
continue;
}
if (!is_dir($translationBasePath . '/' . $dir)) {
continue;
}
$allIntlLocales[] = $dir;
}
sort($allIntlLocales);
echo 'Found ' . count($allIntlLocales) . " locales in iso-codes translation directory\n";
$countriesData = [];
// First, process en to establish the base names
echo "Processing base locale: en\n";
try {
$translationDriver = new SymfonyTranslationDriver();
$translationDriver->setLocale('en');
$factory = new IsoCodesFactory(null, $translationDriver);
$countries = $factory->getCountries();
foreach ($countries as $country) {
$alpha2 = $country->getAlpha2();
$countriesData[$alpha2] = [];
$countriesData[$alpha2]['en'] = $country->getLocalName() ?: $country->getName();
}
echo 'Processed ' . count($countriesData) . " countries for en\n";
} catch (Exception $e) {
echo ' ✗ Error processing en: ' . $e->getMessage() . "\n";
exit(1);
}
// Process all other locales
foreach ($allIntlLocales as $locale) {
if ($locale === 'en') {
continue; // Skip, already processed
}
// Skip locales with @ suffix (script variants like sr@latin, tt@iqtelif)
if (str_contains($locale, '@')) {
continue;
}
echo "Processing locale: $locale\n";
try {
// Create translation driver (no cache directory)
$translationDriver = new SymfonyTranslationDriver();
// Set the locale - Symfony will handle fallback automatically
$translationDriver->setLocale($locale);
// Create ISO codes factory with the translation driver
$factory = new IsoCodesFactory(null, $translationDriver);
$countries = $factory->getCountries();
// Get all countries and their translations
foreach ($countries as $country) {
$alpha2 = $country->getAlpha2();
// Skip if country not in our base data
if (!isset($countriesData[$alpha2])) {
continue;
}
// Get the localized name
$localizedName = $country->getLocalName();
$enName = $countriesData[$alpha2]['en'];
// Skip if translation is same as en (no point in storing duplicate)
if (!$localizedName || $localizedName === $enName) {
continue;
}
// Check if this is a variant locale (e.g., de_AT, de_CH)
$languageCode = strstr($locale, '_', true); // Get language part (e.g., 'de' from 'de_AT')
// If it's a variant locale, check if base language already exists with same translation
if ($languageCode && isset($countriesData[$alpha2][$languageCode])) {
// Skip if the variant has the same translation as the base language
if ($localizedName === $countriesData[$alpha2][$languageCode]) {
continue; // Skip this variant
}
}
$countriesData[$alpha2][$locale] = $localizedName;
}
} catch (Exception $e) {
echo " ✗ Error processing locale $locale: " . $e->getMessage() . "\n";
continue;
}
}
// Sort countries by ISO code
ksort($countriesData);
// Generate JSON file
$jsonOutput = json_encode($countriesData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if ($jsonOutput === false) {
echo 'Error encoding JSON: ' . json_last_error_msg() . "\n";
exit(1);
}
file_put_contents('countries.json', $jsonOutput);
echo 'Generated countries.json with ' . count($countriesData) . " countries\n";
// Generate regions
echo "\nGenerating regions...\n";
// Create regions directory if it doesn't exist
if (!is_dir('regions')) {
mkdir('regions', 0755, true);
}
// Get all country codes that we processed
$processedCountries = array_keys($countriesData);
$regionsData = [];
// First, process en subdivisions to establish the base names
echo "Processing base subdivisions for locale: en\n";
try {
$translationDriver = new SymfonyTranslationDriver();
$translationDriver->setLocale('en');
$factory = new IsoCodesFactory(null, $translationDriver);
$subdivisions = $factory->getSubdivisions();
// Define type hierarchy from most general to most specific (based on Maho's approach)
$typeHierarchy = [
// Most general
'Country', 'Nation',
'Region', 'Autonomous region',
'State', 'Territory', 'Union territory', 'Federal territory',
'Autonomous community', 'Autonomous province',
'Governorate', 'Prefecture', 'Federal district',
'Province', 'Department', 'County',
'District', 'Canton', 'Division',
'Municipality', 'Metropolitan city', 'City',
'Free municipal consortium', 'Decentralized regional entity',
'Commune', 'Parish', 'Borough',
// Most specific
];
// Create a map of type to hierarchy level
$typeScore = array_flip($typeHierarchy);
// Group subdivisions by country and analyze types
$subdivisionsByCountry = [];
foreach ($subdivisions as $subdivision) {
$countryCode = substr($subdivision->getCode(), 0, 2);
if (!in_array($countryCode, $processedCountries)) {
continue;
}
if (!isset($subdivisionsByCountry[$countryCode])) {
$subdivisionsByCountry[$countryCode] = [];
}
$subdivisionsByCountry[$countryCode][] = $subdivision;
}
// For each country, select the most appropriate subdivision level
foreach ($subdivisionsByCountry as $countryCode => $countrySubdivisions) {
// Count subdivisions by type
$typeCounts = [];
foreach ($countrySubdivisions as $subdivision) {
$type = $subdivision->getType();
if (!isset($typeCounts[$type])) {
$typeCounts[$type] = 0;
}
$typeCounts[$type]++;
}
// Find the most specific type with significant coverage (>10 subdivisions or >40% of total)
$totalCount = count($countrySubdivisions);
$selectedTypes = [];
// Sort types by specificity (highest score first)
$scoredTypes = [];
foreach ($typeCounts as $type => $count) {
$score = $typeScore[$type] ?? 999;
$scoredTypes[] = ['type' => $type, 'count' => $count, 'score' => $score];
}
usort($scoredTypes, function ($a, $b) {
return $b['score'] - $a['score']; // Higher score = more specific
});
// Define shipping-relevant subdivision types by country
// Based on: UPU addressing standards, ISO 19160, e-commerce platforms (Magento/PrestaShop), and postal services
// These are the administrative levels typically used for shipping/postal addresses
$shippingTypes = [
'IT' => ['Province', 'Metropolitan city', 'Free municipal consortium', 'Decentralized regional entity', 'Autonomous province'],
'DE' => ['Land'],
'FR' => ['Department'],
'ES' => ['Province', 'Autonomous city in north africa'],
'GB' => ['Country', 'Province'], // ENG/SCT/WLS are 'Country'; NIR is the sole 'Province'
'IE' => ['County'],
'NL' => ['Province'],
'BE' => ['Province'],
'CH' => ['Canton'],
'AT' => ['State'],
'PL' => ['Voivodship'],
'SE' => ['County'],
'DK' => ['Region'],
'NO' => ['County', 'Arctic region'],
'FI' => ['Region'],
'US' => ['State', 'District', 'Outlying area'],
'CA' => ['Province', 'Territory'],
'MX' => ['State', 'Federal entity'],
'AU' => ['State', 'Territory'],
'IN' => ['State', 'Union territory'],
'CN' => ['Province', 'Autonomous region', 'Municipality', 'Special administrative region'],
'JP' => ['Prefecture'],
'KR' => ['Province', 'Metropolitan city', 'Special city', 'Special self-governing province', 'Special self-governing city'],
'BR' => ['State', 'Federal district'],
'AR' => ['Province', 'City'],
'ZA' => ['Province'],
'RU' => ['Republic', 'Administrative territory', 'Administrative region', 'Autonomous district', 'Autonomous city', 'Autonomous region'],
'BS' => ['District', 'Island'],
];
if (isset($shippingTypes[$countryCode])) {
// Use predefined shipping-relevant types for this country
$allowedTypes = $shippingTypes[$countryCode];
foreach ($typeCounts as $type => $count) {
if (in_array($type, $allowedTypes) && $count > 0) {
$selectedTypes[] = $type;
}
}
// Log if we have unexpected types not in our predefined list
$unexpectedTypes = array_diff(array_keys($typeCounts), $allowedTypes);
if (!empty($unexpectedTypes)) {
echo " Note: $countryCode has additional types: " . implode(', ', $unexpectedTypes) . "\n";
}
}
// If no predefined types found, fall back to smart selection
if (empty($selectedTypes)) {
echo " Unknown country $countryCode - types available: " . implode(', ', array_keys($typeCounts)) . "\n";
// For unknown countries, select the middle administrative level
// Skip very general (regions/states with <20 subdivisions) and very specific (municipalities)
$skipGeneral = ['Region', 'Autonomous region', 'Country', 'Nation'];
$skipSpecific = ['Municipality', 'City', 'Commune', 'Parish', 'Borough', 'Town'];
foreach ($scoredTypes as $typeInfo) {
if (!in_array($typeInfo['type'], $skipGeneral) &&
!in_array($typeInfo['type'], $skipSpecific) &&
$typeInfo['count'] >= 3) {
$selectedTypes[] = $typeInfo['type'];
}
}
// If still nothing, take the most common type
if (empty($selectedTypes)) {
$maxCount = max($typeCounts);
foreach ($typeCounts as $type => $count) {
if ($count === $maxCount) {
$selectedTypes[] = $type;
break;
}
}
}
}
// Process subdivisions of selected types
foreach ($countrySubdivisions as $subdivision) {
if (!in_array($subdivision->getType(), $selectedTypes)) {
continue;
}
$code = $subdivision->getCode();
$regionCode = substr($code, 3); // After the hyphen
// Initialize country regions if not exists
if (!isset($regionsData[$countryCode])) {
$regionsData[$countryCode] = [];
}
// Initialize region entry if not exists
if (!isset($regionsData[$countryCode][$regionCode])) {
$regionsData[$countryCode][$regionCode] = [];
}
$regionsData[$countryCode][$regionCode]['en'] = stripIsoBrackets(
$subdivision->getLocalName() ?: $subdivision->getName(),
);
}
}
echo "Processed subdivisions for en\n";
} catch (Exception $e) {
echo ' ✗ Error processing en subdivisions: ' . $e->getMessage() . "\n";
}
// Augment regions from libaddressinput (formats/) for entries that genuinely
// don't exist in ISO 3166-2 but are real postal jurisdictions. Keep this list
// tight — every entry is curated. Names come from formats/{CC}.json so we
// follow upstream's English spelling; later translation passes will overlay
// iso-codes' multilingual names for any entries that happen to match.
$formatsAugmentations = [
'US' => ['AA', 'AE', 'AP'], // Military APO/FPO/DPO codes (Armed Forces Americas/Europe/Pacific)
'AU' => ['JBT'], // Jervis Bay Territory (Commonwealth-administered, not in ISO 3166-2)
];
foreach ($formatsAugmentations as $cc => $keys) {
$formatsPath = "formats/$cc.json";
if (!is_file($formatsPath)) {
echo " Warning: formats/$cc.json missing — skipping augmentation for $cc. Run generate-formats.php first.\n";
continue;
}
$formatsData = json_decode((string) file_get_contents($formatsPath), true);
if (!is_array($formatsData)) {
echo " Warning: formats/$cc.json is not valid JSON — skipping augmentation for $cc.\n";
continue;
}
$subKeys = explode('~', $formatsData['country']['sub_keys'] ?? '');
$subNames = explode('~', $formatsData['country']['sub_names'] ?? '');
foreach ($keys as $key) {
$idx = array_search($key, $subKeys, true);
if ($idx === false) {
echo " Warning: $cc-$key not found in formats/$cc.json sub_keys — libaddressinput may have removed it.\n";
continue;
}
$name = $subNames[$idx] ?? '';
if ($name === '') {
echo " Warning: $cc-$key has empty sub_name in formats/$cc.json — keeping the key in regions but no English name.\n";
$name = $key;
}
if (!isset($regionsData[$cc])) {
$regionsData[$cc] = [];
}
if (!isset($regionsData[$cc][$key])) {
$regionsData[$cc][$key] = ['en' => $name];
}
}
}
// Process all other locales for subdivisions
foreach ($allIntlLocales as $locale) {
if ($locale === 'en') {
continue; // Skip, already processed
}
// Skip locales with @ suffix (script variants like sr@latin, tt@iqtelif)
if (str_contains($locale, '@')) {
continue;
}
echo "Processing subdivisions for locale: $locale\n";
try {
// Create translation driver (no cache directory)
$translationDriver = new SymfonyTranslationDriver();
// Set the locale - Symfony will handle fallback automatically
$translationDriver->setLocale($locale);
// Create ISO codes factory with the translation driver
$factory = new IsoCodesFactory(null, $translationDriver);
$subdivisions = $factory->getSubdivisions();
// Get all subdivisions and their translations
foreach ($subdivisions as $subdivision) {
$code = $subdivision->getCode();
$countryCode = substr($code, 0, 2); // First 2 characters are country code
$regionCode = substr($code, 3); // After the hyphen
// Skip if region not in our base data
if (!isset($regionsData[$countryCode][$regionCode])) {
continue;
}
$localizedName = $subdivision->getLocalName();
if ($localizedName) {
$localizedName = stripIsoBrackets($localizedName);
}
$enName = $regionsData[$countryCode][$regionCode]['en'];
// Skip if translation is same as en (no point in storing duplicate)
if (!$localizedName || $localizedName === $enName) {
continue;
}
// Check if this is a variant locale (e.g., de_AT, de_CH)
$languageCode = strstr($locale, '_', true); // Get language part (e.g., 'de' from 'de_AT')
// If it's a variant locale, check if base language already exists with same translation
if ($languageCode && isset($regionsData[$countryCode][$regionCode][$languageCode])) {
// Skip if the variant has the same translation as the base language
if ($localizedName === $regionsData[$countryCode][$regionCode][$languageCode]) {
continue; // Skip this variant
}
}
$regionsData[$countryCode][$regionCode][$locale] = $localizedName;
}
} catch (Exception $e) {
echo " ✗ Error processing subdivisions for locale $locale: " . $e->getMessage() . "\n";
continue;
}
}
// Generate individual country region files
foreach ($regionsData as $countryCode => $regions) {
// Sort regions by code
ksort($regions);
$regionJsonOutput = json_encode($regions, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
if ($regionJsonOutput === false) {
echo "Error encoding JSON for country $countryCode: " . json_last_error_msg() . "\n";
continue;
}
file_put_contents("regions/$countryCode.json", $regionJsonOutput);
echo "Generated regions/$countryCode.json with " . count($regions) . " regions\n";
}
echo "\nGenerated region files for " . count($regionsData) . " countries\n";