|
| 1 | +#!/usr/bin/env php |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Generates translation files for use in TimezoneType based on the Unicode |
| 6 | + * Common Locale Data Repository. |
| 7 | + * |
| 8 | + * The generates files are written to ../translations and should be committed to |
| 9 | + * Git. |
| 10 | + * |
| 11 | + * Before running the script, download core.zip from the latest CLDR release |
| 12 | + * from http://cldr.unicode.org/index/downloads. Unzip the file into a |
| 13 | + * subdirectory of the directory containing this PHP script. |
| 14 | + * |
| 15 | + * Run the following commands inside the translations-source directory: |
| 16 | + * $ mkdir cldr |
| 17 | + * $ wget http://unicode.org/Public/cldr/XXX/core.zip <-- replace XXX with the latest version |
| 18 | + * $ unzip core.zip |
| 19 | + */ |
| 20 | + |
| 21 | +require_once '../../../../../../vendor/autoload.php'; |
| 22 | + |
| 23 | +use Symfony\Component\Translation\Writer\TranslationWriter; |
| 24 | +use Symfony\Component\Translation\Dumper\XliffFileDumper; |
| 25 | +use Symfony\Component\Translation\Loader\XliffFileLoader; |
| 26 | +use Symfony\Component\Translation\MessageCatalogue; |
| 27 | + |
| 28 | +/** |
| 29 | + * Translates a time zone identifier into an English string. |
| 30 | + * |
| 31 | + * Uses the algorithm used in TimezoneType::getTimezones(). |
| 32 | + * |
| 33 | + * @param string $identifier |
| 34 | + * @return string |
| 35 | + */ |
| 36 | +function getSourceName($identifier) |
| 37 | +{ |
| 38 | + $parts = explode('/', $identifier); |
| 39 | + |
| 40 | + if (count($parts) > 2) { |
| 41 | + $name = $parts[1].' - '.$parts[2]; |
| 42 | + } elseif (count($parts) > 1) { |
| 43 | + $name = $parts[1]; |
| 44 | + } else { |
| 45 | + $name = $parts[0]; |
| 46 | + } |
| 47 | + |
| 48 | + return str_replace('_', ' ', $name); |
| 49 | +} |
| 50 | + |
| 51 | +$locales = [ |
| 52 | + // English must be processed first. |
| 53 | + 'en' => 'en', |
| 54 | + |
| 55 | + // We only support a small subset of the many locales included in CLDR. |
| 56 | + 'ar' => 'ar', |
| 57 | + 'az' => 'az', |
| 58 | + 'bg' => 'bg', |
| 59 | + 'ca' => 'ca', |
| 60 | + 'cs' => 'cs', |
| 61 | + 'da' => 'da', |
| 62 | + 'de' => 'de', |
| 63 | + 'el' => 'el', |
| 64 | + 'en' => 'en', |
| 65 | + 'es' => 'es', |
| 66 | + 'et' => 'et', |
| 67 | + 'eu' => 'eu', |
| 68 | + 'fa' => 'fa', |
| 69 | + 'fi' => 'fi', |
| 70 | + 'fr' => 'fr', |
| 71 | + 'gl' => 'gl', |
| 72 | + 'he' => 'he', |
| 73 | + 'hr' => 'hr', |
| 74 | + 'hu' => 'hu', |
| 75 | + 'hy' => 'hy', |
| 76 | + 'id' => 'id', |
| 77 | + 'it' => 'it', |
| 78 | + 'ja' => 'ja', |
| 79 | + 'lb' => 'lb', |
| 80 | + 'lt' => 'lt', |
| 81 | + 'lv' => 'lv', |
| 82 | + 'mn' => 'mn', |
| 83 | + 'nb' => 'nb', |
| 84 | + 'nl' => 'nl', |
| 85 | + 'pl' => 'pl', |
| 86 | + 'pt_BR' => 'pt_BR', |
| 87 | + 'pt' => 'pt', |
| 88 | + 'ro' => 'ro', |
| 89 | + 'ru' => 'ru', |
| 90 | + 'sk' => 'sk', |
| 91 | + 'sl' => 'sl', |
| 92 | + 'sr_Cyrl' => 'sr', |
| 93 | + 'sr_Latn' => 'sr_Latn', |
| 94 | + 'sv' => 'sv', |
| 95 | + 'uk' => 'uk', |
| 96 | + 'zh_CN' => 'zh_Hans_CN', |
| 97 | +]; |
| 98 | + |
| 99 | +$options = array( |
| 100 | + 'path' => __DIR__ . '/../translations', |
| 101 | + 'default_locale' => 'en', |
| 102 | + 'tool_info' => array( |
| 103 | + 'tool-id' => basename(__FILE__), |
| 104 | + 'tool-name' => 'Time zone translation generator', |
| 105 | + ), |
| 106 | +); |
| 107 | + |
| 108 | +// The source names are generated from the list identifiers. They are English |
| 109 | +// names, except they miss some punctuation and diacritics. E.g. the identifier |
| 110 | +// America/St_Barthelemy has the source name "St Barthelemy", but the proper |
| 111 | +// English name is "St. Barthélemy". |
| 112 | +$sourceNames = array(); |
| 113 | +foreach (\DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC) as $identifier) { |
| 114 | + $sourceNames[$identifier] = getSourceName($identifier); |
| 115 | +} |
| 116 | + |
| 117 | +$domain = 'timezones'; |
| 118 | +$dumper = new XliffFileDumper(); |
| 119 | +$loader = new XliffFileLoader(); |
| 120 | + |
| 121 | +foreach ($locales as $locale => $cldrLocale) { |
| 122 | + $localeNames = array(); |
| 123 | + |
| 124 | + $cldrFile = __DIR__ . '/cldr/common/main/' . $cldrLocale . '.xml'; |
| 125 | + if (!file_exists($cldrFile)) { |
| 126 | + throw new \RuntimeException('Source file not found: ' . $cldrFile); |
| 127 | + } |
| 128 | + |
| 129 | + $xml = simplexml_load_file($cldrFile); |
| 130 | + |
| 131 | + if (!isset($xml->dates->timeZoneNames)) { |
| 132 | + $cldrLocale = (string) $xml->identity->language->attributes()->type; |
| 133 | + $cldrFile = __DIR__ . '/cldr/common/main/' . $cldrLocale . '.xml'; |
| 134 | + if (!file_exists($cldrFile)) { |
| 135 | + throw new \RuntimeException('Fallback source file not found: ' . $cldrFile); |
| 136 | + } |
| 137 | + |
| 138 | + $xml = simplexml_load_file($cldrFile); |
| 139 | + |
| 140 | + if (!isset($xml->dates->timeZoneNames)) { |
| 141 | + throw new \RuntimeException('No time zone info defined in fallback locale: ' . $cldrFile); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + foreach ($xml->dates->timeZoneNames->zone as $zone) { |
| 146 | + $identifier = (string)$zone->attributes()->type; |
| 147 | + $localeName = (string)$zone->exemplarCity; |
| 148 | + |
| 149 | + // Add time zones that exist in the CLDR data but is not (yet) known by |
| 150 | + // PHP on this computer. |
| 151 | + if ($locale == 'en' && !isset($sourceNames[$identifier])) { |
| 152 | + $sourceNames[$identifier] = getSourceName($identifier); |
| 153 | + } |
| 154 | + |
| 155 | + if ($localeName && isset($sourceNames[$identifier])) { |
| 156 | + $enName = $sourceNames[$identifier]; |
| 157 | + $localeNames[$enName] = $localeName; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + $catalogue = new MessageCatalogue($locale); |
| 162 | + $catalogue->add($localeNames, $domain); |
| 163 | + |
| 164 | + // Load additional translations (including continent names) and overrides. |
| 165 | + $extraFile = __DIR__ . '/timezones.' . $locale . '.xlf'; |
| 166 | + if (file_exists($extraFile)) { |
| 167 | + $extraCatalogue = $loader->load($extraFile, $locale, $domain); |
| 168 | + $catalogue->addCatalogue($extraCatalogue); |
| 169 | + } |
| 170 | + |
| 171 | + $dumper->dump($catalogue, $options); |
| 172 | +} |
0 commit comments