init
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0"?>
|
||||
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
|
||||
<update handle="styles"/>
|
||||
<body>
|
||||
<referenceContainer name="content">
|
||||
<block class="Esun\Translation\Block\Adminhtml\Translation\Workspace"
|
||||
name="esun.translation.workspace"
|
||||
template="Esun_Translation::translation/workspace.phtml"/>
|
||||
</referenceContainer>
|
||||
</body>
|
||||
</page>
|
||||
@@ -0,0 +1,317 @@
|
||||
<?php
|
||||
/** @var \Esun\Translation\Block\Adminhtml\Translation\Workspace $block */
|
||||
/** @var \Magento\Framework\Escaper $escaper */
|
||||
$configJson = $block->getConfigJson();
|
||||
$entityId = (int)$block->getEntityId();
|
||||
$entityLabel = $block->getEntityLabel();
|
||||
$entityTitle = $block->getEntityTitle();
|
||||
?>
|
||||
<div class="esun-translation" data-config='<?= $escaper->escapeHtmlAttr($configJson) ?>'>
|
||||
<div class="page-main-actions">
|
||||
<div class="page-actions">
|
||||
<?php if ($entityId): ?>
|
||||
<span class="esun-translation-context">
|
||||
<?= $escaper->escapeHtml(__('%1 #%2', $entityLabel, $entityId)) ?>
|
||||
<?php if ($entityTitle !== ''): ?>
|
||||
<span><?= $escaper->escapeHtml($entityTitle) ?></span>
|
||||
<?php endif; ?>
|
||||
</span>
|
||||
<a class="action-secondary" href="<?= $escaper->escapeUrl($block->getBackUrl()) ?>">
|
||||
<?= $escaper->escapeHtml(__('Back')) ?>
|
||||
</a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (!$block->isEnabled()): ?>
|
||||
<div class="message message-warning">
|
||||
<div><?= $escaper->escapeHtml(__('Esun Translation is disabled in configuration.')) ?></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!$entityId): ?>
|
||||
<div class="message message-warning">
|
||||
<div>
|
||||
<?= $escaper->escapeHtml(__('Open Esun Translation from a product, CMS page, or CMS block edit page/listing.')) ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="admin__fieldset">
|
||||
<div class="admin__field">
|
||||
<div class="admin__field-label">
|
||||
<label><?= $escaper->escapeHtml(__('Languages')) ?></label>
|
||||
</div>
|
||||
<div class="admin__field-control" id="esun-language-list"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="esun-translation-editor" style="display:none;">
|
||||
<div class="esun-editor-toolbar">
|
||||
<strong id="esun-editor-title"></strong>
|
||||
<button type="button" class="action-secondary" id="esun-run-translate">
|
||||
<?= $escaper->escapeHtml(__('Translate')) ?>
|
||||
</button>
|
||||
<button type="button" class="action-primary" id="esun-apply-translation">
|
||||
<?= $escaper->escapeHtml(__('Apply')) ?>
|
||||
</button>
|
||||
</div>
|
||||
<div id="esun-editor-fields"></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.esun-translation-context {
|
||||
display: inline-block;
|
||||
font-weight: 600;
|
||||
margin-right: 12px;
|
||||
padding: 7px 0;
|
||||
}
|
||||
.esun-translation-context span {
|
||||
color: #666;
|
||||
font-weight: 400;
|
||||
margin-left: 8px;
|
||||
}
|
||||
.esun-translation .esun-language-row {
|
||||
align-items: center;
|
||||
border-bottom: 1px solid #e3e3e3;
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
justify-content: space-between;
|
||||
max-width: 760px;
|
||||
padding: 12px 0;
|
||||
}
|
||||
.esun-translation .esun-language-stores {
|
||||
color: #666;
|
||||
margin-left: 10px;
|
||||
}
|
||||
.esun-translation .esun-language-row.is-disabled {
|
||||
opacity: .65;
|
||||
}
|
||||
.esun-editor-toolbar {
|
||||
align-items: center;
|
||||
border-bottom: 1px solid #ddd;
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
justify-content: flex-end;
|
||||
margin-bottom: 16px;
|
||||
padding-bottom: 12px;
|
||||
}
|
||||
.esun-editor-toolbar strong {
|
||||
margin-right: auto;
|
||||
}
|
||||
.esun-field {
|
||||
border-bottom: 1px solid #e5e5e5;
|
||||
margin-bottom: 16px;
|
||||
padding-bottom: 16px;
|
||||
}
|
||||
.esun-field-label {
|
||||
font-weight: 600;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
.esun-field-columns {
|
||||
display: grid;
|
||||
gap: 14px;
|
||||
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
|
||||
}
|
||||
.esun-field textarea {
|
||||
min-height: 130px;
|
||||
resize: vertical;
|
||||
width: 100%;
|
||||
}
|
||||
@media (max-width: 767px) {
|
||||
.esun-field-columns {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
require([
|
||||
'jquery',
|
||||
'Magento_Ui/js/modal/modal',
|
||||
'Magento_Ui/js/modal/alert',
|
||||
'mage/translate'
|
||||
], function ($, modal, alert, $t) {
|
||||
'use strict';
|
||||
|
||||
var root = $('.esun-translation');
|
||||
var config = root.data('config');
|
||||
var currentLanguage = null;
|
||||
var currentFields = [];
|
||||
|
||||
var editor = $('#esun-translation-editor').modal({
|
||||
type: 'slide',
|
||||
title: $t('Esun Translation'),
|
||||
modalClass: 'esun-translation-modal',
|
||||
buttons: []
|
||||
});
|
||||
|
||||
function entityId() {
|
||||
return parseInt(config.entityId, 10) || 0;
|
||||
}
|
||||
|
||||
function showError(message) {
|
||||
alert({title: $t('Esun Translation'), content: message});
|
||||
}
|
||||
|
||||
function renderLanguages() {
|
||||
var list = $('#esun-language-list').empty();
|
||||
var sourceLocale = String(config.sourceLocale || '').toLowerCase();
|
||||
|
||||
if (!config.languages.length) {
|
||||
list.append($('<div/>').addClass('message message-warning').text($t('No valid language groups are configured.')));
|
||||
return;
|
||||
}
|
||||
|
||||
config.languages.forEach(function (language) {
|
||||
var targetLocale = String(language.target_locale || '').toLowerCase();
|
||||
var isSourceLanguage = sourceLocale && sourceLocale === targetLocale;
|
||||
var row = $('<div/>').addClass('esun-language-row').toggleClass('is-disabled', isSourceLanguage);
|
||||
var label = $('<div/>').append($('<strong/>').text(language.label + ':'));
|
||||
label.append($('<span/>').addClass('esun-language-stores').text(language.store_codes.join(', ')));
|
||||
row.append(label);
|
||||
row.append($('<button/>', {
|
||||
type: 'button',
|
||||
disabled: isSourceLanguage,
|
||||
title: isSourceLanguage ? $t('This language matches the source language.') : ''
|
||||
}).addClass('action-secondary').text($t('Translate')).on('click', function () {
|
||||
if (!isSourceLanguage) {
|
||||
openEditor(language);
|
||||
}
|
||||
}));
|
||||
list.append(row);
|
||||
});
|
||||
}
|
||||
|
||||
function openEditor(language) {
|
||||
if (!entityId()) {
|
||||
showError($t('Enter a valid content ID first.'));
|
||||
return;
|
||||
}
|
||||
|
||||
currentLanguage = language;
|
||||
$('#esun-editor-title').text(language.label + ' - ' + language.store_codes.join(', '));
|
||||
$('#esun-editor-fields').text($t('Loading...'));
|
||||
|
||||
$.ajax({
|
||||
url: config.urls.fields,
|
||||
type: 'GET',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
entity_type: config.entityType,
|
||||
entity_id: entityId(),
|
||||
language: language.label
|
||||
}
|
||||
}).done(function (response) {
|
||||
if (!response.success) {
|
||||
showError(response.message || $t('Unable to load translatable fields.'));
|
||||
return;
|
||||
}
|
||||
currentFields = response.fields || [];
|
||||
renderFields(currentFields, {});
|
||||
editor.modal('openModal');
|
||||
}).fail(function () {
|
||||
showError($t('Unable to load translatable fields.'));
|
||||
});
|
||||
}
|
||||
|
||||
function renderFields(fields, translations, sourceOverrides) {
|
||||
var container = $('#esun-editor-fields').empty();
|
||||
sourceOverrides = sourceOverrides || {};
|
||||
|
||||
if (!fields.length) {
|
||||
container.text($t('No translatable fields are available.'));
|
||||
return;
|
||||
}
|
||||
|
||||
fields.forEach(function (field) {
|
||||
var value = translations[field.code] !== undefined ? translations[field.code] : field.target_value;
|
||||
var sourceValue = sourceOverrides[field.code] !== undefined ? sourceOverrides[field.code] : field.source_value;
|
||||
var row = $('<div/>').addClass('esun-field').attr('data-code', field.code);
|
||||
row.append($('<div/>').addClass('esun-field-label').text(field.label + ' (' + field.code + ')'));
|
||||
var columns = $('<div/>').addClass('esun-field-columns');
|
||||
columns.append($('<textarea/>').addClass('admin__control-textarea esun-source-value').val(sourceValue));
|
||||
columns.append($('<textarea/>').addClass('admin__control-textarea esun-target-value').val(value));
|
||||
row.append(columns);
|
||||
container.append(row);
|
||||
});
|
||||
}
|
||||
|
||||
function collectSourceOverrides() {
|
||||
var sourceOverrides = {};
|
||||
$('.esun-field').each(function () {
|
||||
sourceOverrides[$(this).data('code')] = $(this).find('.esun-source-value').val();
|
||||
});
|
||||
|
||||
return sourceOverrides;
|
||||
}
|
||||
|
||||
$('#esun-run-translate').on('click', function () {
|
||||
if (!currentLanguage) {
|
||||
return;
|
||||
}
|
||||
|
||||
var sourceOverrides = collectSourceOverrides();
|
||||
$('#esun-run-translate').prop('disabled', true);
|
||||
$.ajax({
|
||||
url: config.urls.translate,
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
form_key: config.formKey,
|
||||
entity_type: config.entityType,
|
||||
entity_id: entityId(),
|
||||
language: currentLanguage.label,
|
||||
source_overrides: sourceOverrides
|
||||
}
|
||||
}).done(function (response) {
|
||||
if (!response.success) {
|
||||
showError(response.message || $t('Translation failed.'));
|
||||
return;
|
||||
}
|
||||
renderFields(currentFields, response.translations || {}, sourceOverrides);
|
||||
}).fail(function () {
|
||||
showError($t('Translation failed.'));
|
||||
}).always(function () {
|
||||
$('#esun-run-translate').prop('disabled', false);
|
||||
});
|
||||
});
|
||||
|
||||
$('#esun-apply-translation').on('click', function () {
|
||||
var fields = {};
|
||||
$('.esun-field').each(function () {
|
||||
fields[$(this).data('code')] = $(this).find('.esun-target-value').val();
|
||||
});
|
||||
|
||||
$('#esun-apply-translation').prop('disabled', true);
|
||||
$.ajax({
|
||||
url: config.urls.apply,
|
||||
type: 'POST',
|
||||
dataType: 'json',
|
||||
data: {
|
||||
form_key: config.formKey,
|
||||
entity_type: config.entityType,
|
||||
entity_id: entityId(),
|
||||
language: currentLanguage.label,
|
||||
fields: fields
|
||||
}
|
||||
}).done(function (response) {
|
||||
if (!response.success) {
|
||||
showError(response.message || $t('Apply failed.'));
|
||||
return;
|
||||
}
|
||||
alert({title: $t('Esun Translation'), content: response.message || $t('Translations applied.')});
|
||||
editor.modal('closeModal');
|
||||
}).fail(function () {
|
||||
showError($t('Apply failed.'));
|
||||
}).always(function () {
|
||||
$('#esun-apply-translation').prop('disabled', false);
|
||||
});
|
||||
});
|
||||
|
||||
if (entityId()) {
|
||||
renderLanguages();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
|
||||
<settings>
|
||||
<buttons>
|
||||
<button name="esun_cms_block_translate"
|
||||
class="Esun\Translation\Block\Adminhtml\Cms\Block\Edit\Button\Translate"/>
|
||||
</buttons>
|
||||
</settings>
|
||||
</form>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
|
||||
<settings>
|
||||
<buttons>
|
||||
<button name="esun_cms_page_translate"
|
||||
class="Esun\Translation\Block\Adminhtml\Cms\Page\Edit\Button\Translate"/>
|
||||
</buttons>
|
||||
</settings>
|
||||
</form>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
|
||||
<settings>
|
||||
<buttons>
|
||||
<button name="esun_product_translate"
|
||||
class="Esun\Translation\Block\Adminhtml\Product\Edit\Button\Translate"/>
|
||||
</buttons>
|
||||
</settings>
|
||||
</form>
|
||||
Reference in New Issue
Block a user