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,41 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Block\Adminhtml\Cms\Block\Edit\Button;
use Esun\Translation\Model\Config;
use Magento\Cms\Block\Adminhtml\Block\Edit\GenericButton;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
class Translate extends GenericButton implements ButtonProviderInterface
{
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Cms\Api\BlockRepositoryInterface $blockRepository,
private readonly Config $config
) {
parent::__construct($context, $blockRepository);
}
public function getButtonData(): array
{
$blockId = (int)$this->getBlockId();
if (!$this->config->isEnabled() || $blockId <= 0) {
return [];
}
return [
'label' => __('Translate'),
'class' => 'action-secondary',
'on_click' => sprintf(
"location.href = '%s';",
$this->getUrl(
'esun_translation/translate/index',
['entity_type' => 'cms_block', 'entity_id' => $blockId]
)
),
'sort_order' => 85,
];
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Block\Adminhtml\Cms\Page\Edit\Button;
use Esun\Translation\Model\Config;
use Magento\Cms\Block\Adminhtml\Page\Edit\GenericButton;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
class Translate extends GenericButton implements ButtonProviderInterface
{
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Cms\Api\PageRepositoryInterface $pageRepository,
private readonly Config $config
) {
parent::__construct($context, $pageRepository);
}
public function getButtonData(): array
{
$pageId = (int)$this->getPageId();
if (!$this->config->isEnabled() || $pageId <= 0) {
return [];
}
return [
'label' => __('Translate'),
'class' => 'action-secondary',
'on_click' => sprintf(
"location.href = '%s';",
$this->getUrl(
'esun_translation/translate/index',
['entity_type' => 'cms_page', 'entity_id' => $pageId]
)
),
'sort_order' => 85,
];
}
}
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Block\Adminhtml\Product\Edit\Button;
use Esun\Translation\Model\Config;
use Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic;
class Translate extends Generic
{
public function __construct(
\Magento\Framework\View\Element\UiComponent\Context $context,
\Magento\Framework\Registry $registry,
private readonly Config $config
) {
parent::__construct($context, $registry);
}
public function getButtonData(): array
{
$product = $this->getProduct();
if (!$this->config->isEnabled() || !$product || !$product->getId()) {
return [];
}
return [
'label' => __('Translate'),
'class' => 'action-secondary',
'on_click' => sprintf(
"location.href = '%s';",
$this->getUrl(
'esun_translation/translate/index',
['entity_type' => 'product', 'entity_id' => (int)$product->getId()]
)
),
'sort_order' => 85,
];
}
}
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace Esun\Translation\Block\Adminhtml\Translation;
use Esun\Translation\Model\Config;
use Esun\Translation\Model\Entity\EntityPool;
use Esun\Translation\Model\StoreView\LanguageGroupProvider;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Serialize\Serializer\Json;
class Workspace extends Template
{
public function __construct(
Context $context,
private readonly Config $config,
private readonly LanguageGroupProvider $languageGroupProvider,
private readonly EntityPool $entityPool,
private readonly Json $json,
array $data = []
) {
parent::__construct($context, $data);
}
public function isEnabled(): bool
{
return $this->config->isEnabled();
}
public function getEntityType(): string
{
return (string)$this->getRequest()->getParam('entity_type', 'product');
}
public function getEntityId(): int
{
return (int)$this->getRequest()->getParam('entity_id', $this->getRequest()->getParam('product_id'));
}
public function getEntityLabel(): string
{
try {
return $this->entityPool->get($this->getEntityType())->getLabel();
} catch (\Throwable) {
return (string)__('Content');
}
}
public function getEntityTitle(): string
{
$entityId = $this->getEntityId();
if ($entityId <= 0) {
return '';
}
try {
return $this->entityPool->get($this->getEntityType())->getTitle($entityId);
} catch (\Throwable) {
return '';
}
}
public function getBackUrl(): string
{
$entityId = $this->getEntityId();
if ($entityId <= 0) {
return '#';
}
try {
$entity = $this->entityPool->get($this->getEntityType());
return $this->getUrl($entity->getEditUrlPath(), [$entity->getEditIdParam() => $entityId]);
} catch (\Throwable) {
return '#';
}
}
public function getConfigJson(): string
{
return $this->json->serialize([
'entityType' => $this->getEntityType(),
'entityId' => $this->getEntityId(),
'entityLabel' => $this->getEntityLabel(),
'entityTitle' => $this->getEntityTitle(),
'sourceLocale' => $this->config->getSourceLocale(),
'formKey' => $this->getFormKey(),
'languages' => array_map(
static fn ($group): array => $group->toArray(),
$this->languageGroupProvider->getGroups()
),
'urls' => [
'fields' => $this->getUrl('esun_translation/translate/fields'),
'translate' => $this->getUrl('esun_translation/translate/run'),
'apply' => $this->getUrl('esun_translation/translate/apply'),
],
]);
}
}