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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\Intl\Commands;
use Piwik\Container\StaticContainer;
use Piwik\Development;
use Piwik\Filesystem;
use Piwik\Http;
use Piwik\Plugin\ConsoleCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Console Command to generate Intl-data files for Piwik
*
* This script uses the master data of unicode-cldr/cldr-localenames-full repository to fetch available translations
*/
class GenerateIntl extends ConsoleCommand
{
public function isEnabled()
{
return Development::isEnabled();
}
protected function configure()
{
$this->setName('translations:generate-intl-data')
->setDescription('Generates Intl-data for Piwik');
}
protected function transformLangCode($langCode)
{
if (substr_count($langCode, '-') == 1) {
$langCodeParts = explode('-', $langCode, 2);
return sprintf('%s-%s', $langCodeParts[0], strtoupper($langCodeParts[1]));
}
return $langCode;
}
protected function transform($str)
{
if (empty($str)) {
return $str;
}
preg_match_all("~^(.)(.*)$~u", $str, $arr);
return mb_strtoupper($arr[1][0], 'UTF-8').$arr[2][0];
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$piwikLanguages = \Piwik\Plugins\LanguagesManager\API::getInstance()->getAvailableLanguages();
$aliasesUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-core/master/supplemental/aliases.json';
$aliasesData = Http::fetchRemoteFile($aliasesUrl);
$aliasesData = json_decode($aliasesData, true);
$aliasesData = $aliasesData['supplemental']['metadata']['alias']['languageAlias'];
$writePath = Filesystem::getPathToPiwikRoot() . '/plugins/Intl/lang/%s.json';
foreach ($piwikLanguages AS $langCode) {
if ($langCode == 'dev') {
continue;
}
$requestLangCode = $transformedLangCode = $this->transformLangCode($langCode);
if (array_key_exists($requestLangCode, $aliasesData)) {
$requestLangCode = $aliasesData[$requestLangCode]['_replacement'];
}
// fix some locales
$localFixes = array(
'pt' => 'pt-PT',
'pt-br' => 'pt',
'zh-cn' => 'zh-Hans',
'zh-tw' => 'zh-Hant'
);
if (array_key_exists($langCode, $localFixes)) {
$requestLangCode = $localFixes[$langCode];
}
setlocale(LC_ALL, $langCode);
$translations = (array)@json_decode(file_get_contents(sprintf($writePath, $langCode)), true);
$this->fetchLanguageData($output, $transformedLangCode, $requestLangCode, $translations);
$this->fetchTerritoryData($output, $transformedLangCode, $requestLangCode, $translations);
$this->fetchCalendarData($output, $transformedLangCode, $requestLangCode, $translations);
$this->fetchLayoutDirection($output, $transformedLangCode, $requestLangCode, $translations);
$this->fetchUnitData($output, $transformedLangCode, $requestLangCode, $translations);
ksort($translations['Intl']);
file_put_contents(sprintf($writePath, $langCode), json_encode($translations, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
}
}
protected function getEnglishLanguageName($code)
{
$languageDataUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-localenames-full/master/main/%s/languages.json';
static $languageData = array();
try {
if (empty($languageData)) {
$languageData = Http::fetchRemoteFile(sprintf($languageDataUrl, 'en'));
$languageData = json_decode($languageData, true);
$languageData = $languageData['main']['en']['localeDisplayNames']['languages'];
}
return (array_key_exists($code, $languageData) && $languageData[$code] != $code) ? $this->transform($languageData[$code]) : '';
} catch (\Exception $e) {
}
return '';
}
protected function fetchLanguageData(OutputInterface $output, $langCode, $requestLangCode, &$translations)
{
$languageCodes = array_keys(StaticContainer::get('Piwik\Intl\Data\Provider\LanguageDataProvider')->getLanguageList());
$languageDataUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-localenames-full/master/main/%s/languages.json';
try {
$languageData = Http::fetchRemoteFile(sprintf($languageDataUrl, $requestLangCode));
$languageData = json_decode($languageData, true);
$languageData = $languageData['main'][$requestLangCode]['localeDisplayNames']['languages'];
if (empty($languageData)) {
throw new \Exception();
}
foreach ($languageCodes AS $code) {
if (!empty($languageData[$code]) && $languageData[$code] != $code) {
$translations['Intl']['Language_' . $code] = $this->transform($languageData[$code]);
}
}
if (array_key_exists($langCode, $languageData) && $languageData[$langCode] != $langCode) {
$translations['Intl']['OriginalLanguageName'] = $this->transform($languageData[$langCode]);
} else if (array_key_exists($requestLangCode, $languageData) && $languageData[$requestLangCode] != $requestLangCode) {
$translations['Intl']['OriginalLanguageName'] = $this->transform($languageData[$requestLangCode]);
}
$translations['Intl']['EnglishLanguageName'] = $this->getEnglishLanguageName($langCode) ? $this->getEnglishLanguageName($langCode) : $this->getEnglishLanguageName($requestLangCode);
$output->writeln('Saved language data for ' . $langCode);
} catch (\Exception $e) {
$output->writeln('Unable to import language data for ' . $langCode);
}
}
protected function fetchLayoutDirection(OutputInterface $output, $langCode, $requestLangCode, &$translations)
{
$layoutDirectionUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-misc-full/master/main/%s/layout.json';
try {
$layoutData = Http::fetchRemoteFile(sprintf($layoutDirectionUrl, $requestLangCode));
$layoutData = json_decode($layoutData, true);
$layoutData = $layoutData['main'][$requestLangCode]['layout']['orientation'];
if (empty($layoutData)) {
throw new \Exception();
}
$translations['Intl']['LayoutDirection'] = 'ltr';
if ($layoutData['characterOrder'] == 'right-to-left') {
$translations['Intl']['LayoutDirection'] = 'rtl';
}
$output->writeln('Saved language data for ' . $langCode);
} catch (\Exception $e) {
$output->writeln('Unable to import language data for ' . $langCode);
}
}
protected function fetchTerritoryData(OutputInterface $output, $langCode, $requestLangCode, &$translations)
{
$territoryDataUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-localenames-full/master/main/%s/territories.json';
$countryCodes = array_keys(StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider')->getCountryList());
$countryCodes = array_map('strtoupper', $countryCodes);
$continentMapping = array(
"afr" => "002",
"amc" => "013",
"amn" => "003",
"ams" => "005",
"ant" => "AQ",
"asi" => "142",
"eur" => "150",
"oce" => "009"
);
try {
$territoryData = Http::fetchRemoteFile(sprintf($territoryDataUrl, $requestLangCode));
$territoryData = json_decode($territoryData, true);
$territoryData = $territoryData['main'][$requestLangCode]['localeDisplayNames']['territories'];
foreach ($countryCodes AS $code) {
if (!empty($territoryData[$code]) && $territoryData[$code] != $code) {
$translations['Intl']['Country_' . $code] = $this->transform($territoryData[$code]);
}
}
foreach ($continentMapping as $shortCode => $code) {
if (!empty($territoryData[$code]) && $territoryData[$code] != $code) {
$translations['Intl']['Continent_' . $shortCode] = $this->transform($territoryData[$code]);
}
}
$output->writeln('Saved territory data for ' . $langCode);
} catch (\Exception $e) {
$output->writeln('Unable to import territory data for ' . $langCode);
}
}
protected function fetchCalendarData(OutputInterface $output, $langCode, $requestLangCode, &$translations)
{
$calendarDataUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-dates-full/master/main/%s/ca-gregorian.json';
try {
$calendarData = Http::fetchRemoteFile(sprintf($calendarDataUrl, $requestLangCode));
$calendarData = json_decode($calendarData, true);
$calendarData = $calendarData['main'][$requestLangCode]['dates']['calendars']['gregorian'];
for ($i = 1; $i <= 12; $i++) {
$translations['Intl']['ShortMonth_' . $i] = $this->transform($calendarData['months']['format']['abbreviated'][$i]);
$translations['Intl']['LongMonth_' . $i] = $this->transform($calendarData['months']['format']['wide'][$i]);
}
$days = array(
1 => 'mon',
2 => 'tue',
3 => 'wed',
4 => 'thu',
5 => 'fri',
6 => 'sat',
7 => 'sun'
);
foreach ($days AS $nr => $day) {
$translations['Intl']['ShortDay_' . $nr] = $this->transform($calendarData['days']['format']['abbreviated'][$day]);
$translations['Intl']['LongDay_' . $nr] = $this->transform($calendarData['days']['format']['wide'][$day]);
}
$days = array(
'Mo' => 'mon',
'Tu' => 'tue',
'We' => 'wed',
'Th' => 'thu',
'Fr' => 'fri',
'Sa' => 'sat',
'Su' => 'sun'
);
foreach ($days AS $nr => $day) {
$translations['Intl']['Day' . $nr] = $this->transform($calendarData['days']['format']['short'][$day]);
}
$output->writeln('Saved calendar data for ' . $langCode);
} catch (\Exception $e) {
$output->writeln('Unable to import calendar data for ' . $langCode);
}
$dateFieldsUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-dates-full/master/main/%s/dateFields.json';
try {
$dateFieldData = Http::fetchRemoteFile(sprintf($dateFieldsUrl, $requestLangCode));
$dateFieldData = json_decode($dateFieldData, true);
$dateFieldData = $dateFieldData['main'][$requestLangCode]['dates']['fields'];
$translations['Intl']['PeriodWeek'] = $dateFieldData['week']['displayName'];
$translations['Intl']['PeriodYear'] = $dateFieldData['year']['displayName'];
$translations['Intl']['PeriodDay'] = $dateFieldData['day']['displayName'];
$translations['Intl']['PeriodMonth'] = $dateFieldData['month']['displayName'];
$translations['Intl']['YearShort'] = $dateFieldData['year-narrow']['displayName'];
$translations['Intl']['Today'] = $this->transform($dateFieldData['day']['relative-type-0']);
$translations['Intl']['Yesterday'] = $this->transform($dateFieldData['day']['relative-type--1']);
$output->writeln('Saved date fields for ' . $langCode);
} catch (\Exception $e) {
$output->writeln('Unable to import date fields for ' . $langCode);
}
}
protected function fetchUnitData(OutputInterface $output, $langCode, $requestLangCode, &$translations)
{
$unitsUrl = 'https://raw.githubusercontent.com/unicode-cldr/cldr-units-full/master/main/%s/units.json';
try {
$unitsData = Http::fetchRemoteFile(sprintf($unitsUrl, $requestLangCode));
$unitsData = json_decode($unitsData, true);
$unitsData = $unitsData['main'][$requestLangCode]['units'];
$translations['Intl']['NSeconds'] = $this->replacePlaceHolder($unitsData['long']['duration-second']['unitPattern-count-other']);
$translations['Intl']['NSecondsShort'] = $this->replacePlaceHolder($unitsData['narrow']['duration-second']['unitPattern-count-other']);
$translations['Intl']['Seconds'] = $unitsData['long']['duration-second']['displayName'];
$translations['Intl']['NMinutes'] = $this->replacePlaceHolder($unitsData['long']['duration-minute']['unitPattern-count-other']);
if (isset($unitsData['long']['duration-minute']['unitPattern-count-one'])) {
$translations['Intl']['OneMinute'] = $this->replacePlaceHolder($unitsData['long']['duration-minute']['unitPattern-count-one'], '1');
} else {
$translations['Intl']['OneMinute'] = $this->replacePlaceHolder($unitsData['long']['duration-minute']['unitPattern-count-other'], '1');
}
if (isset($unitsData['short']['duration-minute']['unitPattern-count-one'])) {
$translations['Intl']['OneMinuteShort'] = $this->replacePlaceHolder($unitsData['short']['duration-minute']['unitPattern-count-one'], '1');
} else {
$translations['Intl']['OneMinuteShort'] = $this->replacePlaceHolder($unitsData['short']['duration-minute']['unitPattern-count-other'], '1');
}
$translations['Intl']['Minutes'] = $unitsData['long']['duration-minute']['displayName'];
$translations['Intl']['Hours'] = $unitsData['long']['duration-hour']['displayName'];
$translations['Intl']['NHoursShort'] = $this->replacePlaceHolder($unitsData['narrow']['duration-hour']['unitPattern-count-other']);
$translations['Intl']['NDays'] = $this->replacePlaceHolder($unitsData['long']['duration-day']['unitPattern-count-other']);
if (isset($unitsData['short']['duration-day']['unitPattern-count-one'])) {
$translations['Intl']['OneDay'] = $this->replacePlaceHolder($unitsData['long']['duration-day']['unitPattern-count-one'], '1');
} else {
$translations['Intl']['OneDay'] = $this->replacePlaceHolder($unitsData['long']['duration-day']['unitPattern-count-other'], '1');
}
$translations['Intl']['PeriodWeeks'] = $unitsData['long']['duration-week']['displayName'];
$translations['Intl']['PeriodYears'] = $unitsData['long']['duration-year']['displayName'];
$translations['Intl']['PeriodDays'] = $unitsData['long']['duration-day']['displayName'];
$translations['Intl']['PeriodMonths'] = $unitsData['long']['duration-month']['displayName'];
$output->writeln('Saved unit data for ' . $langCode);
} catch (\Exception $e) {
$output->writeln('Unable to import unit data for ' . $langCode);
}
}
protected function replacePlaceHolder($string, $replacement = '%s')
{
return str_replace('{0}', $replacement, $string);
}
}
|