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,128 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Backend;
use Magento\Config\Model\Config\Backend\File;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\DriverInterface;
class ServiceAccountFile extends File
{
private const REQUIRED_FIELDS = ['type', 'project_id', 'private_key', 'client_email'];
private string $oldCredentialFile = '';
private string $credentialFileToDelete = '';
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\MediaStorage\Model\File\UploaderFactory $uploaderFactory,
\Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface $requestData,
Filesystem $filesystem,
private readonly DriverInterface $fileDriver,
?\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
?\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
parent::__construct(
$context,
$registry,
$config,
$cacheTypeList,
$uploaderFactory,
$requestData,
$filesystem,
$resource,
$resourceCollection,
$data
);
$this->_mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->_maxFileSize = 64;
}
public function beforeSave(): static
{
$this->oldCredentialFile = basename((string)$this->getOldValue());
$file = $this->getFileData();
$deleteRequested = is_array($this->getValue()) && !empty($this->getValue()['delete']);
if ($file !== []) {
$this->validateServiceAccountFile((string)$file['tmp_name']);
}
parent::beforeSave();
$newFile = basename((string)$this->getValue());
if ($file !== [] && $newFile !== '') {
$this->fileDriver->changePermissions($this->getCredentialPath($newFile), 0600);
}
if ($this->oldCredentialFile !== ''
&& ($deleteRequested || ($newFile !== '' && $newFile !== $this->oldCredentialFile))
) {
$this->credentialFileToDelete = $this->oldCredentialFile;
}
return $this;
}
public function afterSave(): static
{
parent::afterSave();
if ($this->credentialFileToDelete !== '') {
$oldPath = $this->getCredentialPath($this->credentialFileToDelete);
if ($this->fileDriver->isExists($oldPath)) {
$this->fileDriver->deleteFile($oldPath);
}
}
return $this;
}
protected function _getAllowedExtensions(): array
{
return ['json'];
}
private function validateServiceAccountFile(string $path): void
{
try {
$credentials = json_decode(
$this->fileDriver->fileGetContents($path),
true,
32,
JSON_THROW_ON_ERROR
);
} catch (\Throwable $exception) {
throw new LocalizedException(__('The service account JSON file is invalid.'));
}
if (!is_array($credentials) || ($credentials['type'] ?? '') !== 'service_account') {
throw new LocalizedException(__('The uploaded JSON is not a Google service account key.'));
}
foreach (self::REQUIRED_FIELDS as $field) {
if (!is_string($credentials[$field] ?? null) || trim($credentials[$field]) === '') {
throw new LocalizedException(__('The service account JSON is missing "%1".', $field));
}
}
$privateKey = openssl_pkey_get_private($credentials['private_key']);
if ($privateKey === false) {
throw new LocalizedException(__('The service account private key is invalid.'));
}
}
private function getCredentialPath(string $filename): string
{
return $this->_mediaDirectory->getAbsolutePath(
'esun/multilocale_translation/google/' . basename($filename)
);
}
}
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config;
use Magento\Framework\App\Config\ScopeConfigInterface;
class SkippedFields
{
private const XML_PATH = 'esun_multilocale_translation/general/skipped_fields';
public function __construct(
private readonly ScopeConfigInterface $scopeConfig
) {
}
public function isSkipped(string $entityType, string $field): bool
{
$value = $this->scopeConfig->getValue(self::XML_PATH);
if (!is_string($value) || trim($value) === '') {
return false;
}
$skippedFields = array_filter(array_map('trim', explode(',', $value)));
return in_array($entityType . '.' . $field, $skippedFields, true);
}
}
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class DeepLApiUrl implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
[
'value' => 'https://api-free.deepl.com/v2/translate',
'label' => 'https://api-free.deepl.com/v2/translate',
],
[
'value' => 'https://api.deepl.com/v2/translate',
'label' => 'https://api.deepl.com/v2/translate',
],
];
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class DeepLFormality implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
['value' => 'default', 'label' => __('Default')],
['value' => 'prefer_more', 'label' => __('Prefer formal')],
['value' => 'prefer_less', 'label' => __('Prefer informal')],
];
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class GoogleLocation implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
['value' => 'global', 'label' => __('Global (standard translation)')],
['value' => 'us-central1', 'label' => __('US Central 1 (required for Glossary)')],
];
}
}
@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class LogStatus implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
['value' => 'success', 'label' => __('Success')],
['value' => 'failed', 'label' => __('Failed')],
['value' => 'copied', 'label' => __('Copied (same language)')],
];
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class QueueStatus implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
['value' => 'pending', 'label' => __('Pending')],
['value' => 'processing', 'label' => __('Processing')],
['value' => 'success', 'label' => __('Success')],
['value' => 'failed', 'label' => __('Failed')],
];
}
}
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class SkippedFields implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
$this->group('Category', 'category', [
'name' => 'Name',
'description' => 'Description',
'meta_title' => 'Meta Title',
'meta_keywords' => 'Meta Keywords',
'meta_description' => 'Meta Description',
]),
$this->group('Product', 'product', [
'name' => 'Name',
'description' => 'Description',
'short_description' => 'Short Description',
'meta_title' => 'Meta Title',
'meta_keyword' => 'Meta Keywords',
'meta_description' => 'Meta Description',
]),
$this->group('CMS Page', 'cms_page', [
'title' => 'Title',
'content_heading' => 'Content Heading',
'content' => 'Content',
'meta_title' => 'Meta Title',
'meta_keywords' => 'Meta Keywords',
'meta_description' => 'Meta Description',
]),
$this->group('CMS Block', 'cms_block', [
'title' => 'Title',
'content' => 'Content',
]),
$this->group('Blog Post', 'blog_post', [
'name' => 'Post Name',
'short_description' => 'Short Description',
'post_content' => 'Content',
'meta_title' => 'Meta Title',
'meta_keywords' => 'Meta Keywords',
'meta_description' => 'Meta Description',
]),
$this->group('Wiki Category', 'wiki_category', [
'title' => 'Title',
'summary' => 'Summary',
'content' => 'Content',
'meta_title' => 'Meta Title',
'meta_description' => 'Meta Description',
'meta_keywords' => 'Meta Keywords',
]),
$this->group('Wiki Homepage', 'wiki_homepage', [
'title' => 'Title',
'content' => 'Content',
'meta_title' => 'Meta Title',
'meta_description' => 'Meta Description',
'meta_keywords' => 'Meta Keywords',
]),
];
}
private function group(string $label, string $entityType, array $fields): array
{
$options = [];
foreach ($fields as $field => $fieldLabel) {
$options[] = [
'value' => $entityType . '.' . $field,
'label' => __($fieldLabel),
];
}
return ['label' => __($label), 'value' => $options];
}
}
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Esun\MultiLocaleTranslation\Model\StoreViewProvider;
use Magento\Framework\Data\OptionSourceInterface;
class StoreViews implements OptionSourceInterface
{
public function __construct(private readonly StoreViewProvider $storeViewProvider)
{
}
public function toOptionArray(): array
{
$options = [['value' => 0, 'label' => __('Default Store View (0)')]];
foreach ($this->storeViewProvider->getStoreViewOptions() as $store) {
$options[] = [
'value' => $store['store_id'],
'label' => sprintf(
'%s / %s (%s)',
$store['label'],
$store['website'],
$store['code']
),
];
}
return $options;
}
}
@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace Esun\MultiLocaleTranslation\Model\Config\Source;
use Magento\Framework\Data\OptionSourceInterface;
class TranslationProvider implements OptionSourceInterface
{
public function toOptionArray(): array
{
return [
['value' => 'deepl', 'label' => __('DeepL')],
['value' => 'google', 'label' => __('Google Cloud Translation V3')],
];
}
}