|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/* |
| 5 | + * This file is part of the Symfony package. |
| 6 | + * |
| 7 | + * (c) Fabien Potencier <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * Generates translation files for use in TimezoneType based on the Unicode |
| 15 | + * Common Locale Data Repository. |
| 16 | + * |
| 17 | + * The generates files are written to Resources/translations and should be |
| 18 | + * committed to Git. |
| 19 | + */ |
| 20 | +$componentDir = realpath(__DIR__.'/../..'); |
| 21 | +$autoloadFile = $componentDir.'/vendor/autoload.php'; |
| 22 | + |
| 23 | +if (!is_file($autoloadFile)) { |
| 24 | + die("$autoloadFile not found; run `composer update` in $componentDir.\n"); |
| 25 | +} |
| 26 | + |
| 27 | +require_once $autoloadFile; |
| 28 | + |
| 29 | +use Symfony\Component\Form\Extension\Core\Type\TimezoneType; |
| 30 | +use Symfony\Component\Translation\Dumper\XliffFileDumper; |
| 31 | +use Symfony\Component\Translation\MessageCatalogue; |
| 32 | + |
| 33 | +// We only support a small subset of the many locales included in CLDR. |
| 34 | +$locales = array( |
| 35 | + 'ar' => 'ar', |
| 36 | + 'az' => 'az', |
| 37 | + 'bg' => 'bg', |
| 38 | + 'ca' => 'ca', |
| 39 | + 'cs' => 'cs', |
| 40 | + 'da' => 'da', |
| 41 | + 'de' => 'de', |
| 42 | + 'el' => 'el', |
| 43 | + 'en' => 'en', |
| 44 | + 'es' => 'es', |
| 45 | + 'et' => 'et', |
| 46 | + 'eu' => 'eu', |
| 47 | + 'fa' => 'fa', |
| 48 | + 'fi' => 'fi', |
| 49 | + 'fr' => 'fr', |
| 50 | + 'gl' => 'gl', |
| 51 | + 'he' => 'he', |
| 52 | + 'hr' => 'hr', |
| 53 | + 'hu' => 'hu', |
| 54 | + 'hy' => 'hy', |
| 55 | + 'id' => 'id', |
| 56 | + 'it' => 'it', |
| 57 | + 'ja' => 'ja', |
| 58 | + 'lb' => 'lb', |
| 59 | + 'lt' => 'lt', |
| 60 | + 'lv' => 'lv', |
| 61 | + 'mn' => 'mn', |
| 62 | + 'nb' => 'nb', |
| 63 | + 'nl' => 'nl', |
| 64 | + 'nn' => 'nn', |
| 65 | + 'no' => 'no', |
| 66 | + 'pl' => 'pl', |
| 67 | + 'pt' => 'pt', |
| 68 | + 'pt_BR' => 'pt_BR', |
| 69 | + 'ro' => 'ro', |
| 70 | + 'ru' => 'ru', |
| 71 | + 'sk' => 'sk', |
| 72 | + 'sl' => 'sl', |
| 73 | + 'sr_Cyrl' => 'sr', |
| 74 | + 'sr_Latn' => 'sr_Latn', |
| 75 | + 'sv' => 'sv', |
| 76 | + 'tl' => 'tl', |
| 77 | + 'uk' => 'uk', |
| 78 | + 'zh_CN' => 'zh_Hans_CN', |
| 79 | +); |
| 80 | + |
| 81 | +// Timezone identifiers containing 2 slashes need a qualifier in addition to |
| 82 | +// just the city name. E.g. America/Argentina/San_Juan needs to be distinguished |
| 83 | +// from other cities named San Juan. On the other hand, for US states the CLDR |
| 84 | +// string already contains the qualifier, e.g. the English string fo |
| 85 | +// America/Indiana/Knox is "Knox, Indiana". |
| 86 | +$countries = array( |
| 87 | + 'America/Argentina' => 'AR', |
| 88 | + 'America/Indiana' => false, |
| 89 | + 'America/Kentucky' => false, |
| 90 | + 'America/North_Dakota' => false, |
| 91 | +); |
| 92 | + |
| 93 | +// Not all tzdata areas (the string before the first slash) correspond to |
| 94 | +// territories definded in CLDR. |
| 95 | +$areas = array( |
| 96 | + 'America' => '019', |
| 97 | + 'Africa' => '002', |
| 98 | + 'Antarctica' => 'AQ', |
| 99 | + 'Arctic' => false, |
| 100 | + 'Asia' => '142', |
| 101 | + 'Atlantic' => false, |
| 102 | + 'Australia' => '053', |
| 103 | + 'Europe' => '150', |
| 104 | + 'Indian' => false, |
| 105 | + 'Pacific' => false, |
| 106 | + 'Other' => false, |
| 107 | +); |
| 108 | + |
| 109 | +$options = array( |
| 110 | + 'path' => $componentDir.'/Resources/translations/generated', |
| 111 | + 'default_locale' => 'en', |
| 112 | + 'tool_info' => array( |
| 113 | + 'tool-id' => basename(__FILE__), |
| 114 | + 'tool-name' => 'CLDR time zone translation generator', |
| 115 | + ), |
| 116 | +); |
| 117 | + |
| 118 | +$timezones = TimezoneType::getTimezones(\DateTimeZone::ALL); |
| 119 | + |
| 120 | +$domain = 'timezones'; |
| 121 | + |
| 122 | +$identifiers = \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC); |
| 123 | + |
| 124 | +foreach ($locales as $locale => $cldrLocale) { |
| 125 | + $names = array(); |
| 126 | + |
| 127 | + foreach ($timezones as $area => $identifiers) { |
| 128 | + if (!isset($areas[$area])) { |
| 129 | + die("Area $area not \$areas.\n"); |
| 130 | + } |
| 131 | + if ($areas[$area]) { |
| 132 | + $areaName = Punic\Territory::getName($areas[$area], $cldrLocale); |
| 133 | + if ($areaName) { |
| 134 | + $names[$area] = $areaName; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + foreach ($identifiers as $key => $identifier) { |
| 139 | + $timezoneName = Punic\Calendar::getTimezoneExemplarCity($identifier, false, $cldrLocale); |
| 140 | + |
| 141 | + $parts = explode('/', $identifier); |
| 142 | + if (count($parts) >= 3) { |
| 143 | + $prefix = $parts[0].'/'.$parts[1]; |
| 144 | + if (!isset($countries[$prefix])) { |
| 145 | + die("Prefix $prefix not defined in \$countries.\n"); |
| 146 | + } |
| 147 | + if ($countries[$prefix]) { |
| 148 | + $countryName = Punic\Territory::getName($countries[$prefix], $cldrLocale); |
| 149 | + $timezoneName .= ', '.$countryName; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if ($timezoneName) { |
| 154 | + $names[$key] = $timezoneName; |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + $catalogue = new MessageCatalogue($locale); |
| 160 | + $catalogue->add($names, $domain); |
| 161 | + |
| 162 | + $dumper = new XliffFileDumper(); |
| 163 | + $dumper->dump($catalogue, $options); |
| 164 | +} |
0 commit comments