/
language-packs.ts
554 lines (517 loc) · 20.2 KB
/
language-packs.ts
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
import 'bootstrap';
import $ from 'jquery';
import { AjaxResponse } from '@typo3/core/ajax/ajax-response';
import { AbstractInteractableModule } from '../abstract-interactable-module';
import AjaxRequest from '@typo3/core/ajax/ajax-request';
import SecurityUtility from '@typo3/core/security-utility';
import FlashMessage from '../../renderable/flash-message';
import InfoBox from '../../renderable/info-box';
import ProgressBar from '../../renderable/progress-bar';
import Severity from '../../renderable/severity';
import Router from '../../router';
import MessageInterface from '@typo3/install/message-interface';
/**
* Module: @typo3/install/module/language-packs
*/
class LanguagePacks extends AbstractInteractableModule {
private readonly selectorOutputContainer: string = '.t3js-languagePacks-output';
private readonly selectorContentContainer: string = '.t3js-languagePacks-mainContent';
private readonly selectorActivateLanguage: string = '.t3js-languagePacks-activateLanguage';
private readonly selectorActivateLanguageIcon: string = '#t3js-languagePacks-activate-icon';
private readonly selectorAddLanguageToggle: string = '.t3js-languagePacks-addLanguage-toggle';
private readonly selectorLanguageInactive: string = '.t3js-languagePacks-inactive';
private readonly selectorDeactivateLanguage: string = '.t3js-languagePacks-deactivateLanguage';
private readonly selectorDeactivateLanguageIcon: string = '#t3js-languagePacks-deactivate-icon';
private readonly selectorUpdate: string = '.t3js-languagePacks-update';
private readonly selectorLanguageUpdateIcon: string = '#t3js-languagePacks-languageUpdate-icon';
private readonly selectorNotifications: string = '.t3js-languagePacks-notifications';
private activeLanguages: string[] = [];
private activeExtensions: string[] = [];
private packsUpdateDetails: { [id: string]: number } = {
toHandle: 0,
handled: 0,
updated: 0,
new: 0,
failed: 0,
skipped: 0,
};
private notifications: JQuery[] = [];
private static pluralize(count: number, word: string = 'pack', suffix: string = 's', additionalCount: number = 0): string {
return count !== 1 && additionalCount !== 1 ? word + suffix : word;
}
public initialize(currentModal: JQuery): void {
this.currentModal = currentModal;
// Get configuration list on modal open
this.getData();
currentModal.on('click', this.selectorAddLanguageToggle, (): void => {
currentModal.find(this.selectorContentContainer + ' ' + this.selectorLanguageInactive).toggle();
});
currentModal.on('click', this.selectorActivateLanguage, (e: JQueryEventObject): void => {
const iso = $(e.target).closest(this.selectorActivateLanguage).data('iso');
e.preventDefault();
this.activateLanguage(iso);
});
currentModal.on('click', this.selectorDeactivateLanguage, (e: JQueryEventObject): void => {
const iso = $(e.target).closest(this.selectorDeactivateLanguage).data('iso');
e.preventDefault();
this.deactivateLanguage(iso);
});
currentModal.on('click', this.selectorUpdate, (e: JQueryEventObject): void => {
const iso = $(e.target).closest(this.selectorUpdate).data('iso');
const extension = $(e.target).closest(this.selectorUpdate).data('extension');
e.preventDefault();
this.updatePacks(iso, extension);
});
}
private getData(): void {
const modalContent = this.getModalBody();
(new AjaxRequest(Router.getUrl('languagePacksGetData')))
.get({ cache: 'no-cache' })
.then(
async (response: AjaxResponse): Promise<void> => {
const data = await response.resolve();
if (data.success === true) {
this.activeLanguages = data.activeLanguages;
this.activeExtensions = data.activeExtensions;
modalContent.empty().append(data.html);
const contentContainer: JQuery = modalContent.parent().find(this.selectorContentContainer);
contentContainer.empty();
contentContainer.append(this.languageMatrixHtml(data));
contentContainer.append(this.extensionMatrixHtml(data));
} else {
const message = InfoBox.render(Severity.error, 'Something went wrong', '');
this.addNotification(message);
}
this.renderNotifications();
},
(error: AjaxResponse): void => {
Router.handleAjaxError(error, modalContent);
}
);
}
private activateLanguage(iso: string): void {
const modalContent = this.getModalBody();
const $outputContainer = this.findInModal(this.selectorOutputContainer);
const message = ProgressBar.render(Severity.loading, 'Loading...', '');
$outputContainer.empty().append(message);
this.getNotificationBox().empty();
(new AjaxRequest(Router.getUrl()))
.post({
install: {
action: 'languagePacksActivateLanguage',
token: this.getModuleContent().data('language-packs-activate-language-token'),
iso: iso,
},
})
.then(
async (response: AjaxResponse): Promise<void> => {
const data = await response.resolve();
$outputContainer.empty();
if (data.success === true && Array.isArray(data.status)) {
data.status.forEach((element: MessageInterface): void => {
const message = InfoBox.render(element.severity, element.title, element.message);
this.addNotification(message);
});
} else {
const message = FlashMessage.render(Severity.error, 'Something went wrong', '');
this.addNotification(message);
}
this.getData();
},
(error: AjaxResponse): void => {
Router.handleAjaxError(error, modalContent);
}
);
}
private deactivateLanguage(iso: string): void {
const modalContent = this.getModalBody();
const $outputContainer = this.findInModal(this.selectorOutputContainer);
const message = ProgressBar.render(Severity.loading, 'Loading...', '');
$outputContainer.empty().append(message);
this.getNotificationBox().empty();
(new AjaxRequest(Router.getUrl()))
.post({
install: {
action: 'languagePacksDeactivateLanguage',
token: this.getModuleContent().data('language-packs-deactivate-language-token'),
iso: iso,
},
})
.then(
async (response: AjaxResponse): Promise<void> => {
const data = await response.resolve();
$outputContainer.empty();
if (data.success === true && Array.isArray(data.status)) {
data.status.forEach((element: MessageInterface): void => {
const message = InfoBox.render(element.severity, element.title, element.message);
this.addNotification(message);
});
} else {
const message = FlashMessage.render(Severity.error, 'Something went wrong', '');
this.addNotification(message);
}
this.getData();
},
(error: AjaxResponse): void => {
Router.handleAjaxError(error, modalContent);
}
);
}
private updatePacks(iso: string, extension: string): void {
const $outputContainer = this.findInModal(this.selectorOutputContainer);
const $contentContainer = this.findInModal(this.selectorContentContainer);
const isos = iso === undefined ? this.activeLanguages : [iso];
let updateIsoTimes = true;
let extensions = this.activeExtensions;
if (extension !== undefined) {
extensions = [extension];
updateIsoTimes = false;
}
this.packsUpdateDetails = {
toHandle: isos.length * extensions.length,
handled: 0,
updated: 0,
new: 0,
failed: 0,
skipped: 0,
};
$outputContainer.empty().append(
$('<div>', { 'class': 'progress' }).append(
$('<div>', {
'class': 'progress-bar progress-bar-info',
'role': 'progressbar',
'aria-valuenow': 0,
'aria-valuemin': 0,
'aria-valuemax': 100,
'style': 'width: 0;',
}).append(
$(
'<span>',
{ 'class': 'text-nowrap' }).text('0 of ' + this.packsUpdateDetails.toHandle + ' language ' +
LanguagePacks.pluralize(this.packsUpdateDetails.toHandle) + ' updated'
),
),
));
$contentContainer.empty();
isos.forEach((isoCode: string): void => {
extensions.forEach((extensionKey: string): void => {
this.getNotificationBox().empty();
(new AjaxRequest(Router.getUrl()))
.post({
install: {
action: 'languagePacksUpdatePack',
token: this.getModuleContent().data('language-packs-update-pack-token'),
iso: isoCode,
extension: extensionKey,
},
})
.then(
async (response: AjaxResponse): Promise<void> => {
const data = await response.resolve();
if (data.success === true) {
this.packsUpdateDetails.handled++;
if (data.packResult === 'new') {
this.packsUpdateDetails.new++;
} else if (data.packResult === 'update') {
this.packsUpdateDetails.updated++;
} else if (data.packResult === 'skipped') {
this.packsUpdateDetails.skipped++;
} else {
this.packsUpdateDetails.failed++;
}
this.packUpdateDone(updateIsoTimes, isos);
} else {
this.packsUpdateDetails.handled++;
this.packsUpdateDetails.failed++;
this.packUpdateDone(updateIsoTimes, isos);
}
},
(): void => {
this.packsUpdateDetails.handled++;
this.packsUpdateDetails.failed++;
this.packUpdateDone(updateIsoTimes, isos);
}
);
});
});
}
private packUpdateDone(updateIsoTimes: boolean, isos: string[]): void {
const modalContent = this.getModalBody();
const $outputContainer = this.findInModal(this.selectorOutputContainer);
if (this.packsUpdateDetails.handled === this.packsUpdateDetails.toHandle) {
// All done - create summary, update 'last update' of iso list, render main view
const message = InfoBox.render(
Severity.ok,
'Language packs updated',
this.packsUpdateDetails.new + ' new language ' + LanguagePacks.pluralize(this.packsUpdateDetails.new) + ' downloaded, ' +
this.packsUpdateDetails.updated + ' language ' + LanguagePacks.pluralize(this.packsUpdateDetails.updated) + ' updated, ' +
this.packsUpdateDetails.skipped + ' language ' + LanguagePacks.pluralize(this.packsUpdateDetails.skipped) + ' skipped, ' +
this.packsUpdateDetails.failed + ' language ' + LanguagePacks.pluralize(this.packsUpdateDetails.failed) + ' not available',
);
this.addNotification(message);
if (updateIsoTimes === true) {
(new AjaxRequest(Router.getUrl()))
.post({
install: {
action: 'languagePacksUpdateIsoTimes',
token: this.getModuleContent().data('language-packs-update-iso-times-token'),
isos: isos,
},
})
.then(
async (response: AjaxResponse): Promise<void> => {
const data = await response.resolve();
if (data.success === true) {
this.getData();
} else {
const m = FlashMessage.render(Severity.error, 'Something went wrong', '');
this.addNotification(m);
}
},
(error: AjaxResponse): void => {
Router.handleAjaxError(error, modalContent);
}
);
} else {
this.getData();
}
} else {
// Update progress bar
const percent = (this.packsUpdateDetails.handled / this.packsUpdateDetails.toHandle) * 100;
$outputContainer.find('.progress-bar')
.css('width', percent + '%')
.attr('aria-valuenow', percent)
.find('span')
.text(
this.packsUpdateDetails.handled + ' of ' + this.packsUpdateDetails.toHandle + ' language ' +
LanguagePacks.pluralize(this.packsUpdateDetails.handled, 'pack', 's', this.packsUpdateDetails.toHandle) + ' updated'
);
}
}
private languageMatrixHtml(data: any): string {
const activateIcon = this.findInModal(this.selectorActivateLanguageIcon).html();
const deactivateIcon = this.findInModal(this.selectorDeactivateLanguageIcon).html();
const updateIcon = this.findInModal(this.selectorLanguageUpdateIcon).html();
const configurationIsWritable = this.getModuleContent().data('configuration-is-writable');
const $markupContainer = $('<div>');
const $tbody = $('<tbody>');
data.languages.forEach((language: any): void => {
const availableMatrixActions = [];
const active = language.active;
if (configurationIsWritable) {
availableMatrixActions.push($('<a>', {
'class': 'btn btn-default t3js-languagePacks-deactivateLanguage',
'data-iso': language.iso,
'title': 'Deactivate',
}).append(deactivateIcon));
}
availableMatrixActions.push($('<a>', {
'class': 'btn btn-default t3js-languagePacks-update',
'data-iso': language.iso,
'title': 'Download language packs',
}).append(updateIcon));
const $tr = $('<tr>');
if (active) {
$tbody.append(
$tr.append(
$('<td>').text(' ' + language.name).prepend(
$('<div />', { class: 'btn-group' }).append(
availableMatrixActions
),
),
),
);
} else {
if (configurationIsWritable) {
availableMatrixActions.push($('<a>', {
'class': 'btn btn-default t3js-languagePacks-activateLanguage',
'data-iso': language.iso,
'title': 'Activate',
}).append(activateIcon));
}
$tbody.append(
$tr.addClass('t3-languagePacks-inactive t3js-languagePacks-inactive').css({ 'display': 'none' }).append(
$('<td>').text(' ' + language.name).prepend(
$('<div />', { class: 'btn-group' }).append(
availableMatrixActions
),
),
),
);
}
$tr.append(
$('<td>').text(language.iso),
$('<td>').text(language.dependencies.join(', ')),
$('<td>').text(language.lastUpdate === null ? '' : language.lastUpdate),
);
$tbody.append($tr);
});
const globalActions = [];
if (configurationIsWritable) {
globalActions.push($('<button>', {
'class': 'btn btn-default t3js-languagePacks-addLanguage-toggle',
'type': 'button'
}).append(
$('<span>').append(activateIcon),
' Add language',
));
}
globalActions.push($('<button>', {
'class': 'btn btn-default disabled update-all t3js-languagePacks-update',
'type': 'button',
'disabled': 'disabled'
}).append(
$('<span>').append(updateIcon),
' Update all',
));
$markupContainer.append(
$('<h3>').text('Active languages'),
$('<table>', { 'class': 'table table-striped table-bordered' }).append(
$('<thead>').append(
$('<tr>').append(
$('<th>').append(
$('<div />', { class: 'btn-group' }).append(
globalActions
),
),
$('<th>').text('Locale'),
$('<th>').text('Dependencies'),
$('<th>').text('Last update'),
),
),
$tbody,
),
);
if (Array.isArray(this.activeLanguages) && this.activeLanguages.length) {
$markupContainer.find('.update-all').removeClass('disabled').removeAttr('disabled');
}
return $markupContainer.html();
}
private extensionMatrixHtml(data: any): any {
const securityUtility = new SecurityUtility();
const updateIcon: string = this.findInModal(this.selectorLanguageUpdateIcon).html();
let tooltip: string = '';
let extensionTitle: JQuery;
let rowCount: number = 0;
const $markupContainer: JQuery = $('<div>');
const $headerRow: JQuery = $('<tr>');
$headerRow.append(
$('<th>').text('Extension'),
$('<th>').text('Key'),
);
data.activeLanguages.forEach((language: string): void => {
$headerRow.append(
$('<th>').append(
$('<a>', {
'class': 'btn btn-default t3js-languagePacks-update',
'data-iso': language,
'title': 'Download and update all language packs',
}).append(
$('<span>').append(updateIcon),
' ' + language,
),
),
);
});
const $tbody = $('<tbody>');
data.extensions.forEach((extension: any): void => {
rowCount++;
if (typeof extension.icon !== 'undefined') {
extensionTitle = $('<span>').append(
$('<img>', {
'style': 'max-height: 16px; max-width: 16px;',
'src': extension.icon,
'alt': extension.title,
}),
$('<span>').text(' ' + extension.title),
);
} else {
extensionTitle = $('<span>').text(extension.title);
}
const $tr = $('<tr>');
$tr.append(
$('<td>').html(extensionTitle.html()),
$('<td>').text(extension.key),
);
data.activeLanguages.forEach((language: string): void => {
let packFoundForLanguage: boolean = false;
extension.packs.forEach((pack: any): void => {
if (pack.iso !== language) {
return;
}
packFoundForLanguage = true;
const $column = $('<td>');
$tr.append($column);
if (pack.exists !== true) {
if (pack.lastUpdate !== null) {
tooltip = 'No language pack available for ' + pack.iso + ' when tried at ' + pack.lastUpdate + '. Click to re-try.';
} else {
tooltip = 'Language pack not downloaded. Click to download';
}
} else {
if (pack.lastUpdate === null) {
tooltip = 'Downloaded. Click to renew';
} else {
tooltip = 'Language pack downloaded at ' + pack.lastUpdate + '. Click to renew';
}
}
$column.append(
$('<a>', {
'class': 'btn btn-default t3js-languagePacks-update',
'data-extension': extension.key,
'data-iso': pack.iso,
'title': securityUtility.encodeHtml(tooltip),
}).append(updateIcon),
);
});
// Render empty colum to avoid disturbed table build up if pack was not found for language.
if (!packFoundForLanguage) {
const $column = $('<td>');
$tr.append($column).append(' ');
}
});
$tbody.append($tr);
});
$markupContainer.append(
$('<h3>').text('Translation status'),
$('<table>', { 'class': 'table table-striped table-bordered' }).append(
$('<thead>').append($headerRow),
$tbody,
),
);
if (rowCount === 0) {
return InfoBox.render(
Severity.ok,
'Language packs have been found for every installed extension.',
'To download the latest changes, use the refresh button in the list above.',
);
}
return $markupContainer.html();
}
private getNotificationBox(): JQuery {
return this.findInModal(this.selectorNotifications);
}
private addNotification(notification: JQuery): void {
this.notifications.push(notification);
}
private renderNotifications(): void {
const $notificationBox: JQuery = this.getNotificationBox();
for (const notification of this.notifications) {
$notificationBox.append(notification);
}
this.notifications = [];
}
}
export default new LanguagePacks();