This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,35 @@
.esun-mlt-category-selection {
background: #fff;
border: 1px solid #adadad;
margin-bottom: 1.5rem;
padding: 1.2rem;
}
.esun-mlt-category-selection__title {
font-size: 1.6rem;
font-weight: 600;
margin-bottom: .8rem;
}
.esun-mlt-category-selection__summary {
color: #666;
margin-bottom: 1rem;
}
.esun-mlt-category-selection__actions {
display: flex;
flex-wrap: wrap;
gap: .7rem;
}
.esun-mlt-category-selection__actions button {
padding-left: 1rem;
padding-right: 1rem;
}
.esun-mlt-category-checkbox {
margin: 0 .6rem 0 0;
position: relative;
vertical-align: middle;
z-index: 2;
}
@@ -0,0 +1,331 @@
.esun-mlt-modal.modal-popup {
left: 2%;
right: 2%;
width: 96%;
}
.esun-mlt-modal .modal-inner-wrap {
width: 100%;
max-width: none;
}
.esun-mlt-intro {
display: flex;
gap: 18px;
align-items: center;
margin-bottom: 18px;
color: #514943;
}
.esun-mlt-intro span {
color: #777;
}
.esun-mlt-settings {
position: relative;
margin-bottom: 22px;
overflow: hidden;
background: #fff;
border: 1px solid #d6d6d6;
border-radius: 3px;
}
.esun-mlt-settings__summary {
position: relative;
display: flex;
align-items: center;
justify-content: flex-start;
gap: 12px;
min-height: 54px;
padding: 0 390px 0 18px;
color: #41362f;
background: #f8f8f8;
cursor: pointer;
list-style: none;
user-select: none;
}
.esun-mlt-settings__summary::-webkit-details-marker {
display: none;
}
.esun-mlt-settings__summary::after {
position: absolute;
top: 50%;
right: 20px;
width: 9px;
height: 9px;
border-right: 2px solid #514943;
border-bottom: 2px solid #514943;
content: '';
transform: translateY(-65%) rotate(45deg);
transition: transform .15s ease;
}
.esun-mlt-settings[open] .esun-mlt-settings__summary::after {
transform: translateY(-30%) rotate(225deg);
}
.esun-mlt-settings__summary:hover {
background: #f2f2f2;
}
.esun-mlt-settings__summary strong {
font-size: 15px;
}
.esun-mlt-settings__summary span {
overflow: hidden;
color: #777;
text-overflow: ellipsis;
white-space: nowrap;
}
.esun-mlt-selector {
display: flex;
align-items: flex-end;
gap: 16px;
padding: 18px;
border-top: 1px solid #d6d6d6;
}
.esun-mlt-selector__field {
width: min(720px, 75vw);
}
.esun-mlt-selector__field > label {
display: block;
margin-bottom: 7px;
font-weight: 600;
}
.esun-mlt-selector__provider {
position: absolute;
z-index: 2;
top: 9px;
right: 52px;
display: flex;
align-items: center;
gap: 10px;
}
.esun-mlt-selector__provider > label {
margin: 0;
font-weight: 600;
white-space: nowrap;
}
.esun-mlt-selector__provider select {
width: 180px;
}
.esun-mlt-selector__field select {
width: 100%;
}
.esun-mlt-selector__field .note {
margin-top: 5px;
}
.esun-mlt-workspace {
overflow-x: auto;
padding-bottom: 12px;
}
.esun-mlt-grid {
display: grid;
min-width: max-content;
border-top: 1px solid #d6d6d6;
border-left: 1px solid #d6d6d6;
}
.esun-mlt-column-heading {
position: sticky;
top: 0;
z-index: 2;
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
min-height: 62px;
padding: 14px 16px;
background: #fff;
border-right: 1px solid #d6d6d6;
border-bottom: 2px solid #adadad;
}
.esun-mlt-column-heading--source {
background: #f7f7f7;
}
.esun-mlt-column-heading strong {
font-size: 16px;
}
.esun-mlt-column-heading span {
color: #777;
font-size: 12px;
}
.esun-mlt-cell {
position: relative;
padding: 16px;
background: #fff;
border-right: 1px solid #d6d6d6;
border-bottom: 1px solid #d6d6d6;
}
.esun-mlt-cell:nth-child(n) textarea[readonly] {
background: #f3f3f3;
color: #555;
}
.esun-mlt-field-label {
display: flex;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
font-weight: 600;
}
.esun-mlt-field-label small {
color: #999;
font-weight: 400;
}
.esun-mlt-cell textarea {
width: 100%;
resize: vertical;
}
.esun-mlt-use-default {
display: inline-flex;
align-items: center;
gap: 6px;
margin-top: 9px;
color: #555;
cursor: pointer;
font-size: 12px;
}
.esun-mlt-use-default input {
margin: 0;
}
.esun-mlt-cell textarea:disabled {
background: #f3f3f3;
color: #777;
}
.esun-mlt-html-badge {
position: absolute;
right: 20px;
bottom: 20px;
padding: 2px 5px;
color: #666;
background: #eee;
border-radius: 2px;
font-size: 10px;
}
.esun-mlt-actions {
position: sticky;
bottom: 0;
z-index: 3;
padding: 15px 0 4px;
text-align: right;
background: linear-gradient(to bottom, rgba(255, 255, 255, 0), #fff 28%);
}
.esun-mlt-actions button + button {
margin-left: 10px;
}
.esun-mlt-progress {
margin: 14px 0 4px;
padding: 14px 16px;
color: #514943;
background: #f8f8f8;
border: 1px solid #d6d6d6;
border-radius: 3px;
}
.esun-mlt-progress__status {
margin-bottom: 9px;
font-weight: 600;
}
.esun-mlt-progress__track {
height: 10px;
overflow: hidden;
background: #e3e3e3;
border-radius: 5px;
}
.esun-mlt-progress__bar {
display: block;
width: 0;
height: 100%;
background: #eb5202;
border-radius: 5px;
transition: width .2s ease;
}
.esun-mlt-progress__summary {
margin-top: 7px;
color: #777;
font-size: 12px;
}
.esun-mlt-progress--success {
background: #f2f8ed;
border-color: #95c37b;
}
.esun-mlt-progress--success .esun-mlt-progress__bar {
background: #79a22e;
}
.esun-mlt-progress--warning {
background: #fff8e5;
border-color: #f0c36d;
}
.esun-mlt-progress--warning .esun-mlt-progress__bar {
background: #d6a100;
}
@media (max-width: 767px) {
.esun-mlt-modal.modal-popup {
left: 0;
right: 0;
width: 100%;
}
.esun-mlt-selector {
align-items: stretch;
flex-direction: column;
}
.esun-mlt-settings__summary {
align-items: flex-start;
flex-direction: column;
gap: 2px;
justify-content: center;
padding-right: 52px;
}
.esun-mlt-selector__provider {
position: static;
padding: 12px 18px;
border-top: 1px solid #d6d6d6;
}
.esun-mlt-selector__provider select {
width: 100%;
}
.esun-mlt-selector__field {
width: 100%;
}
}
@@ -0,0 +1,33 @@
.esun-translation-badges {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
min-width: 12rem;
}
.esun-translation-badges__badge,
.esun-translation-badges__more {
background: #e8f4ea;
border: 1px solid #9fc8a6;
border-radius: 1.2rem;
color: #276738;
cursor: default;
display: inline-block;
font-size: 1.2rem;
font-weight: 600;
line-height: 1.8rem;
min-width: 3.2rem;
padding: 0.1rem 0.7rem;
text-align: center;
}
.esun-translation-badges__more {
background: #f1f1f1;
border-color: #c9c9c9;
color: #555;
}
.esun-translation-badges__empty {
color: #777;
white-space: nowrap;
}
@@ -0,0 +1,98 @@
define([
'jquery',
'mage/translate'
], function ($, $t) {
'use strict';
return function (config) {
var $tree = $(config.treeSelector),
$form = $(config.formSelector),
$input = $form.find('[data-role=esun-category-ids]'),
$summary = $form.find('[data-role=esun-category-summary]'),
$submit = $form.find('[data-action=translate-selected]'),
selected = new Set(),
observer;
if (!$tree.length || !$form.length) {
return;
}
function updateForm() {
var ids = Array.from(selected).sort(function (left, right) {
return left - right;
});
$input.val(ids.join(','));
$submit.prop('disabled', ids.length === 0);
$summary.text(ids.length
? $t('%1 categories selected.').replace('%1', ids.length)
: $t('No categories selected.'));
}
function addCheckbox(anchor) {
var $anchor = $(anchor),
nodeId = parseInt($anchor.closest('.jstree-node').attr('id'), 10),
$checkbox;
if (!nodeId || $anchor.children('.esun-mlt-category-checkbox').length) {
return;
}
$checkbox = $('<input/>', {
type: 'checkbox',
class: 'admin__control-checkbox esun-mlt-category-checkbox',
'aria-label': $t('Select category for translation')
}).prop('checked', selected.has(nodeId));
$checkbox.on('mousedown click dblclick', function (event) {
event.stopPropagation();
});
$checkbox.on('change', function () {
if (this.checked) {
selected.add(nodeId);
} else {
selected.delete(nodeId);
}
updateForm();
});
$anchor.prepend($checkbox);
}
function renderCheckboxes() {
$tree.find('.jstree-anchor').each(function () {
addCheckbox(this);
});
}
$form.on('click', '[data-action=select-loaded]', function () {
$tree.find('.esun-mlt-category-checkbox').each(function () {
var nodeId = parseInt($(this).closest('.jstree-node').attr('id'), 10);
if (nodeId) {
selected.add(nodeId);
this.checked = true;
}
});
updateForm();
});
$form.on('click', '[data-action=clear-selection]', function () {
selected.clear();
$tree.find('.esun-mlt-category-checkbox').prop('checked', false);
updateForm();
});
$form.on('submit', function (event) {
if (selected.size === 0) {
event.preventDefault();
}
});
$tree.on('ready.jstree redraw.jstree load_node.jstree after_open.jstree', renderCheckboxes);
observer = new MutationObserver(renderCheckboxes);
observer.observe($tree.get(0), {childList: true, subtree: true});
renderCheckboxes();
updateForm();
};
});
@@ -0,0 +1,128 @@
define([
'jquery',
'Magento_Ui/js/modal/alert',
'mage/translate'
], function ($, alert, $t) {
'use strict';
return function (config, element) {
var $el = $(element),
state = {
entityType: config.entityType || 'product',
entityId: parseInt(config.entityId, 10),
sourceStoreId: parseInt(config.sourceStoreId, 10),
fieldsUrl: config.fieldsUrl,
applyUrl: config.applyUrl,
formKey: config.formKey || null,
fields: []
},
$targetStore = $('#mlt-target-store'),
$loadBtn = $('#mlt-load-btn'),
$saveBtn = $('#mlt-save-btn'),
$fields = $('#mlt-fields'),
$saveArea = $('#mlt-save-area');
function renderFields(fields) {
var html = '';
state.fields = fields || [];
if (!state.fields.length) {
html = '<div class="message message-warning">' + $t('No translatable fields found.') + '</div>';
} else {
state.fields.forEach(function (field, index) {
var safe = {
code: $('<div/>').text(field.code || '').html(),
label: $('<div/>').text(field.label || '').html(),
source: $('<div/>').text(field.source_value || '').html(),
target: $('<div/>').text(field.target_value || '').html()
};
html += '<div class="entry-edit" style="margin-bottom:18px;">'
+ '<div style="margin-bottom:8px;"><strong>' + safe.label + '</strong> (' + safe.code + ')</div>'
+ '<div style="margin-bottom:6px;"><label>'
+ $t('Source') + '</label><textarea class="admin__control-textarea" readonly rows="5">' + safe.source + '</textarea></div>'
+ '<div><label>' + $t('Target') + '</label>'
+ '<textarea class="admin__control-textarea esun-mlt-target" rows="5" data-code="' + safe.code
+ '" data-index="' + index + '">' + safe.target + '</textarea></div>'
+ '</div>';
});
}
$fields.html(html);
$saveArea.toggle(!!state.fields.length);
}
function loadFields() {
var targetStoreId = parseInt($targetStore.val(), 10);
if (!targetStoreId) {
alert({content: $t('Please select a target store view.')});
return;
}
$.getJSON(state.fieldsUrl, {
form_key: state.formKey,
entity_type: state.entityType,
entity_id: state.entityId,
source_store_id: state.sourceStoreId,
target_store_id: targetStoreId
}).done(function (response) {
if (!response || response.success !== true) {
alert({content: (response && response.message) ? response.message : $t('Failed to load fields.')});
return;
}
renderFields(response.fields || []);
}).fail(function () {
alert({content: $t('Failed to load fields. Please check permission and try again.')});
});
}
function applyFields() {
var targetStoreId = parseInt($targetStore.val(), 10),
values = {},
payload = {};
if (!targetStoreId) {
alert({content: $t('Please select a target store view.')});
return;
}
if (state.sourceStoreId === targetStoreId) {
alert({content: $t('Target store view cannot be same as source store view.')});
return;
}
$('.esun-mlt-target').each(function () {
var code = $(this).data('code');
values[code] = $(this).val();
});
payload = {
form_key: state.formKey,
entity_type: state.entityType,
entity_id: state.entityId,
target_store_id: targetStoreId,
fields: values
};
$.ajax({
url: state.applyUrl,
method: 'POST',
data: payload
}).done(function (response) {
if (!response || response.success !== true) {
alert({content: (response && response.message) ? response.message : $t('Save failed.')});
return;
}
alert({
content: response.message || $t('Saved.')
});
loadFields();
}).fail(function () {
alert({content: $t('Save failed.')});
});
}
$loadBtn.on('click', loadFields);
$saveBtn.on('click', applyFields);
};
});
@@ -0,0 +1,586 @@
define([
'jquery',
'Magento_Ui/js/modal/modal',
'Magento_Ui/js/modal/alert',
'mage/translate'
], function ($, modal, alert, $t) {
'use strict';
var modalSelector = '#esun-mlt-modal';
function showError(message) {
alert({content: message});
}
function storeHeading(store) {
var parts = [];
if (store && store.label) {
parts.push(store.label);
}
if (store && store.website && store.website !== store.label) {
parts.push(store.website);
}
return parts.join(' / ') || $t('Default Store View');
}
function ajaxJson(options) {
return new Promise(function (resolve, reject) {
$.ajax(options).done(resolve).fail(reject);
});
}
function createModal(config) {
var $existing = $(modalSelector);
if ($existing.length) {
try {
$existing.modal('closeModal').modal('destroy');
} catch (error) {
// The old element may already have been destroyed by Magento UI.
}
$existing.remove();
}
var stores = [],
providers = [],
loadedTargets = [],
$root = $('<div/>', {id: 'esun-mlt-modal'}),
$intro = $('<div/>', {class: 'esun-mlt-intro'}),
$settings = $('<details/>', {class: 'esun-mlt-settings'}),
$settingsSummary = $('<summary/>', {class: 'esun-mlt-settings__summary'}),
$settingsTitle = $('<strong/>').text($t('Translation Settings')),
$settingsMeta = $('<span/>').text($t('Loading store views...')),
$selectorArea = $('<div/>', {class: 'esun-mlt-selector'}),
$storeGroup = $('<div/>', {class: 'esun-mlt-selector__field'}),
$storeLabel = $('<label/>', {for: 'esun-mlt-target-store'}).text($t('Target Store Views')),
$storeHelp = $('<div/>', {class: 'note'}).text(
$t('All target store views are loaded automatically. You can select fewer views and reload if needed.')
),
$storeSelect = $('<select/>', {
id: 'esun-mlt-target-store',
class: 'admin__control-multiselect',
multiple: 'multiple',
size: 5
}),
$providerGroup = $('<div/>', {class: 'esun-mlt-selector__provider'}),
$providerLabel = $('<label/>', {for: 'esun-mlt-provider'}).text($t('Translation Provider')),
$providerSelect = $('<select/>', {
id: 'esun-mlt-provider',
class: 'admin__control-select'
}),
$load = $('<button/>', {type: 'button', class: 'action-primary'}).text($t('Load Fields')),
$workspace = $('<div/>', {class: 'esun-mlt-workspace'}).html(
'<div class="message message-notice">' + $t('Loading store views...') + '</div>'
),
$saveArea = $('<div/>', {class: 'esun-mlt-actions'}).hide(),
$autoTranslate = $('<button/>', {type: 'button', class: 'action-secondary'})
.text($t('Auto Translate')),
$save = $('<button/>', {type: 'button', class: 'action-primary'}).text($t('Save All Translations')),
$progress = $('<div/>', {
class: 'esun-mlt-progress',
role: 'status',
'aria-live': 'polite'
}).hide(),
$progressStatus = $('<div/>', {class: 'esun-mlt-progress__status'}),
$progressTrack = $('<div/>', {
class: 'esun-mlt-progress__track',
role: 'progressbar',
'aria-valuemin': 0,
'aria-valuemax': 100,
'aria-valuenow': 0
}),
$progressBar = $('<span/>', {class: 'esun-mlt-progress__bar'}),
$progressSummary = $('<div/>', {class: 'esun-mlt-progress__summary'});
$intro.append(
$('<strong/>').text($t('Entity ID: ') + config.entityId),
$('<span/>').text($t('Compare the source content with one or more target store views.'))
);
$settingsSummary.append($settingsTitle, $settingsMeta);
$storeGroup.append($storeLabel, $storeSelect, $storeHelp);
$providerGroup.append($providerLabel, $providerSelect);
$selectorArea.append($storeGroup, $load);
$settings.append($settingsSummary, $providerGroup, $selectorArea);
$progressTrack.append($progressBar);
$progress.append($progressStatus, $progressTrack, $progressSummary);
$saveArea.append($autoTranslate, $save);
$root.append($intro, $settings, $workspace, $progress, $saveArea);
$('body').append($root);
modal({
type: 'popup',
responsive: true,
innerScroll: true,
title: $t('Translate') + ' ' + String(config.entityType || '').toUpperCase(),
modalClass: 'esun-mlt-modal',
buttons: []
}, $root);
function sourceStore() {
var sourceId = parseInt(config.sourceStoreId || 0, 10),
match = null;
$.each(stores, function (index, store) {
if (parseInt(store.store_id, 10) === sourceId) {
match = store;
return false;
}
});
return match || {
store_id: 0,
code: 'default',
label: $t('Default Store View'),
website: ''
};
}
function updateSettingsSummary() {
var selectedCount = ($storeSelect.val() || []).length;
$settingsMeta.text(
selectedCount + ' ' + $t('Store Views Selected')
);
}
function fieldControl(field, value, readonly, targetStoreId) {
var rows = field.is_html ? 12 : 3,
$control = $('<textarea/>', {
class: 'admin__control-textarea esun-mlt-value',
rows: rows,
readonly: readonly
}).val(value || '');
$control.attr('data-code', field.code || '');
$control.attr('data-is-html', field.is_html ? '1' : '0');
if (targetStoreId) {
$control.attr('data-store-id', targetStoreId);
}
return $control;
}
function fieldCell(field, value, readonly, targetStoreId) {
var $cell = $('<div/>', {class: 'esun-mlt-cell'}),
$label = $('<label/>', {class: 'esun-mlt-field-label'}).text(field.label || field.code || ''),
$control = fieldControl(field, value, readonly, targetStoreId),
$inherit;
$label.append($('<small/>').text(field.code || ''));
$cell.append($label, $control);
if (!readonly && targetStoreId) {
$inherit = $('<label/>', {class: 'esun-mlt-use-default'}).append(
$('<input/>', {
type: 'checkbox',
class: 'esun-mlt-use-default-checkbox',
checked: field.use_default === true
}).attr({
'data-code': field.code || '',
'data-store-id': targetStoreId
}),
$('<span/>').text($t('Use Default Value'))
);
$control.prop('disabled', field.use_default === true);
$cell.append($inherit);
}
if (field.is_html) {
$cell.append($('<span/>', {class: 'esun-mlt-html-badge'}).text('HTML'));
}
return $cell;
}
function renderComparison(results) {
var sourceFields = results.length ? (results[0].fields || []) : [],
columnCount = results.length + 1,
$grid = $('<div/>', {class: 'esun-mlt-grid'}).css(
'grid-template-columns',
'repeat(' + columnCount + ', minmax(340px, 1fr))'
);
loadedTargets = results;
$workspace.empty();
$saveArea.hide();
if (!sourceFields.length) {
$workspace.append(
$('<div/>', {class: 'message message-warning'}).text($t('No translatable fields found.'))
);
return;
}
$grid.append(
$('<div/>', {class: 'esun-mlt-column-heading esun-mlt-column-heading--source'})
.append($('<strong/>').text(storeHeading(sourceStore())))
.append($('<span/>').text($t('Source')))
);
$.each(results, function (index, result) {
$grid.append(
$('<div/>', {class: 'esun-mlt-column-heading'})
.append($('<strong/>').text(storeHeading(result.store)))
.append($('<span/>').text(result.store.code))
);
});
$.each(sourceFields, function (fieldIndex, sourceField) {
$grid.append(fieldCell(sourceField, sourceField.source_value, true, null));
$.each(results, function (resultIndex, result) {
var targetField = (result.fields || [])[fieldIndex] || sourceField;
$grid.append(
fieldCell(
targetField,
targetField.target_value,
false,
result.store.store_id
)
);
});
});
$workspace.append($grid);
$saveArea.show();
}
function selectedStores() {
var selectedIds = $.map($storeSelect.val() || [], function (value) {
return parseInt(value, 10);
});
return $.grep(stores, function (store) {
return selectedIds.indexOf(parseInt(store.store_id, 10)) !== -1;
});
}
function loadFields() {
var targets = selectedStores(),
requests;
if (!targets.length) {
showError($t('Please select at least one target store view.'));
return;
}
$load.prop('disabled', true);
$workspace.html('<div class="message message-notice">' + $t('Loading fields...') + '</div>');
$saveArea.hide();
requests = $.map(targets, function (store) {
return ajaxJson({
url: config.fieldsUrl,
method: 'GET',
dataType: 'json',
data: {
form_key: window.FORM_KEY,
entity_type: config.entityType,
entity_id: config.entityId,
source_store_id: config.sourceStoreId || 0,
target_store_id: store.store_id
}
}).then(function (response) {
if (!response || response.success !== true) {
throw new Error(response && response.message ? response.message : $t('Failed to load fields.'));
}
return {store: store, fields: response.fields || []};
});
});
Promise.all(requests).then(renderComparison).catch(function (error) {
$workspace.empty();
showError(error.message || $t('Failed to load fields. Please check permission and try again.'));
}).finally(function () {
$load.prop('disabled', false);
});
}
function valuesForStore(storeId) {
var values = {};
$workspace.find('.esun-mlt-value[data-store-id="' + storeId + '"]').each(function () {
values[$(this).attr('data-code')] = $(this).val();
});
return values;
}
function sourceFieldsForTranslation() {
var values = {};
$workspace.find('.esun-mlt-value[readonly]').each(function () {
var $field = $(this),
code = $field.attr('data-code');
if (!code || $field.val() === '') {
return;
}
values[code] = {
value: $field.val(),
is_html: $field.attr('data-is-html') === '1' ? 1 : 0
};
});
return values;
}
function applyAutomaticTranslations(storeId, translations) {
$.each(translations || {}, function (code, value) {
var $control = $workspace.find(
'.esun-mlt-value[data-store-id="' + storeId + '"][data-code="' + code + '"]'
),
$inherit = $workspace.find(
'.esun-mlt-use-default-checkbox[data-store-id="' + storeId + '"][data-code="' + code + '"]'
);
$inherit.prop('checked', false);
$control.prop('disabled', false).val(value);
});
}
function updateTranslationProgress(completed, total, status, successCount, failureCount) {
var percent = total ? Math.round((completed / total) * 100) : 0;
$progress.show();
$progressStatus.text(status);
$progressBar.css('width', percent + '%');
$progressTrack.attr('aria-valuenow', percent);
$progressSummary.text(
$t('Completed') + ': ' + completed + '/' + total + ' · ' +
$t('Successful') + ': ' + successCount + ' · ' +
$t('Failed') + ': ' + failureCount
);
}
function autoTranslateFields() {
var fields = sourceFieldsForTranslation(),
provider = $providerSelect.val(),
providerLabel = $providerSelect.find(':selected').text(),
total = loadedTargets.length,
successCount = 0,
failures = [];
if (!provider) {
showError($t('Please enable and select a translation provider.'));
return;
}
if (!loadedTargets.length || $.isEmptyObject(fields)) {
showError($t('No source fields are available for automatic translation.'));
return;
}
$autoTranslate.prop('disabled', true);
$save.prop('disabled', true);
$load.prop('disabled', true);
$progress.removeClass('esun-mlt-progress--success esun-mlt-progress--warning');
updateTranslationProgress(0, total, $t('Preparing translation...'), 0, 0);
function translateStore(index) {
var result,
storeName;
if (index >= total) {
return Promise.resolve();
}
result = loadedTargets[index];
storeName = storeHeading(result.store);
$autoTranslate.text($t('Translating') + ' ' + (index + 1) + '/' + total);
updateTranslationProgress(
index,
total,
$t('Translating') + ' ' + (index + 1) + '/' + total + ': ' + storeName,
successCount,
failures.length
);
return ajaxJson({
url: config.autoTranslateUrl,
method: 'POST',
dataType: 'json',
data: {
form_key: window.FORM_KEY,
provider: provider,
entity_type: config.entityType,
entity_id: config.entityId,
source_store_id: config.sourceStoreId || 0,
target_store_id: result.store.store_id,
fields: fields
}
}).then(function (response) {
if (!response || response.success !== true) {
throw new Error(
response && response.message ? response.message : $t('Automatic translation failed.')
);
}
applyAutomaticTranslations(result.store.store_id, response.translations || {});
successCount++;
}).catch(function (error) {
failures.push({
store: storeName,
message: error.message || $t('Automatic translation failed.')
});
}).then(function () {
updateTranslationProgress(
index + 1,
total,
$t('Processed') + ' ' + (index + 1) + '/' + total + ': ' + storeName,
successCount,
failures.length
);
return translateStore(index + 1);
});
}
translateStore(0).then(function () {
var status;
if (failures.length) {
status = providerLabel + ': ' + $t('Translation finished with errors.') + ' ' +
failures.map(function (failure) {
return failure.store + ' — ' + failure.message;
}).join(' | ');
$progress.addClass('esun-mlt-progress--warning');
} else {
status = providerLabel + ': ' +
$t('Translation completed. Review the result, then save translations.');
$progress.addClass('esun-mlt-progress--success');
}
updateTranslationProgress(total, total, status, successCount, failures.length);
}).finally(function () {
$autoTranslate.prop('disabled', false).text($t('Auto Translate'));
$save.prop('disabled', false);
$load.prop('disabled', false);
});
}
function useDefaultForStore(storeId) {
var values = {};
$workspace.find('.esun-mlt-use-default-checkbox[data-store-id="' + storeId + '"]').each(function () {
values[$(this).attr('data-code')] = $(this).is(':checked') ? 1 : 0;
});
return values;
}
function saveFields() {
var requests;
if (!loadedTargets.length) {
return;
}
$save.prop('disabled', true);
requests = $.map(loadedTargets, function (result) {
return ajaxJson({
url: config.applyUrl,
method: 'POST',
dataType: 'json',
data: {
form_key: window.FORM_KEY,
entity_type: config.entityType,
entity_id: config.entityId,
source_store_id: config.sourceStoreId || 0,
target_store_id: result.store.store_id,
fields: valuesForStore(result.store.store_id),
use_default: useDefaultForStore(result.store.store_id)
}
}).then(function (response) {
if (!response || response.success !== true) {
throw new Error(response && response.message ? response.message : $t('Save failed.'));
}
return response;
});
});
Promise.all(requests).then(function () {
alert({content: $t('All translations were saved successfully.')});
loadFields();
}).catch(function (error) {
showError(error.message || $t('Save failed.'));
}).finally(function () {
$save.prop('disabled', false);
});
}
$load.on('click', function () {
updateSettingsSummary();
$settings.prop('open', false);
loadFields();
});
$storeSelect.on('change', updateSettingsSummary);
$providerSelect.on('change', updateSettingsSummary);
$autoTranslate.on('click', autoTranslateFields);
$save.on('click', saveFields);
$workspace.on('change', '.esun-mlt-use-default-checkbox', function () {
var $checkbox = $(this),
storeId = $checkbox.attr('data-store-id'),
code = $checkbox.attr('data-code');
$workspace.find(
'.esun-mlt-value[data-store-id="' + storeId + '"][data-code="' + code + '"]'
).prop('disabled', $checkbox.is(':checked'));
});
ajaxJson({
url: config.storesUrl,
method: 'GET',
dataType: 'json',
data: {form_key: window.FORM_KEY}
}).then(function (response) {
if (!response || response.success !== true) {
throw new Error($t('Failed to load store views.'));
}
stores = response.stores || [];
providers = response.providers || [];
$storeSelect.empty();
$providerSelect.empty();
$.each(providers, function (index, provider) {
$providerSelect.append(
$('<option/>', {value: provider.code})
.text(provider.label)
.prop('selected', provider.code === response.default_provider)
);
});
if (!providers.length) {
$providerSelect.append(
$('<option/>', {value: ''}).text($t('No enabled provider'))
);
$autoTranslate.prop('disabled', true);
}
var $websiteGroups = {};
$.each(stores, function (index, store) {
var isSource = parseInt(store.store_id, 10) === parseInt(config.sourceStoreId || 0, 10),
website = store.website || $t('Other Website'),
$option,
$group;
if (isSource) {
return;
}
if (!$websiteGroups[website]) {
$websiteGroups[website] = $('<optgroup/>', {label: website});
$storeSelect.append($websiteGroups[website]);
}
$group = $websiteGroups[website];
$option = $('<option/>', {value: store.store_id})
.text(store.label + ' (' + store.code + ' · ' + store.locale + ')')
.prop('selected', true);
$group.append($option);
});
updateSettingsSummary();
loadFields();
}).catch(function (error) {
showError(error.message || $t('Failed to load store views.'));
});
$root.modal('openModal');
}
return createModal;
});
@@ -0,0 +1,25 @@
define([
'jquery',
'mage/translate'
], function ($, $t) {
'use strict';
return function (config, element) {
var $form = $(element),
$selectAll = $form.find('[data-role="wiki-select-all"]'),
$items = $form.find('[data-role="wiki-category-select"]');
$selectAll.on('change', function () {
$items.prop('checked', this.checked);
});
$items.on('change', function () {
$selectAll.prop('checked', $items.length > 0 && $items.filter(':checked').length === $items.length);
});
$form.on('submit', function (event) {
if ($items.filter(':checked').length === 0) {
event.preventDefault();
window.alert($t('Select at least one Wiki category.'));
}
});
};
});