Files
esun3d/app/code/Esun/Translation/Model/Translation/TranslationService.php
T
2026-09-15 10:35:56 +08:00

117 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace Esun\Translation\Model\Translation;
use Esun\Translation\Api\TranslatorInterface;
use Esun\Translation\Model\Ai\ProtectedTextProcessor;
use Esun\Translation\Model\Config;
use Esun\Translation\Model\Entity\EntityPool;
use Esun\Translation\Model\Html\HtmlTextExtractor;
use Esun\Translation\Model\Html\HtmlTranslationValidator;
use Esun\Translation\Model\StoreView\LanguageGroupProvider;
class TranslationService
{
public function __construct(
private readonly Config $config,
private readonly EntityPool $entityPool,
private readonly LanguageGroupProvider $languageGroupProvider,
private readonly HtmlTextExtractor $htmlTextExtractor,
private readonly HtmlTranslationValidator $htmlValidator,
private readonly ProtectedTextProcessor $protectedTextProcessor,
private readonly TranslatorInterface $translator
) {
}
/**
* @param array<string, mixed> $sourceOverrides
* @return array<string, string>
*/
public function translate(string $type, int $entityId, string $languageLabel, array $sourceOverrides = []): array
{
if (!$this->config->isEnabled()) {
throw new \RuntimeException('Esun Translation is disabled.');
}
$entity = $this->entityPool->get($type);
$group = $this->languageGroupProvider->getGroup($languageLabel);
if (!$group) {
throw new \RuntimeException('Language group was not found.');
}
$fields = $entity->getFields($entityId, $group->getStoreIds());
$plainUnits = [];
$restoreMaps = [];
$htmlFragments = [];
$fieldUnits = [];
foreach ($fields as $field) {
$code = (string)$field['code'];
$sourceValue = array_key_exists($code, $sourceOverrides)
? (string)$sourceOverrides[$code]
: (string)$field['source_value'];
if (trim($sourceValue) === '') {
continue;
}
if ((bool)$field['is_html']) {
$fragment = $this->htmlTextExtractor->extract($sourceValue);
$htmlFragments[$code] = $fragment;
foreach ($fragment->getTexts() as $nodeId => $text) {
[$protectedText, $placeholders] = $this->protectedTextProcessor->protect($text);
$unitId = $code . '::' . $nodeId;
$plainUnits[$unitId] = $protectedText;
$restoreMaps[$unitId] = $placeholders;
$fieldUnits[$code][] = $unitId;
}
continue;
}
[$protectedText, $placeholders] = $this->protectedTextProcessor->protect($sourceValue);
$plainUnits[$code] = $protectedText;
$restoreMaps[$code] = $placeholders;
}
$translatedUnits = $this->translator->translate(
$plainUnits,
$this->config->getSourceLocale(),
$group->getTargetLocale()
);
foreach ($translatedUnits as $unitId => $text) {
$translatedUnits[$unitId] = $this->protectedTextProcessor->restore($text, $restoreMaps[$unitId] ?? []);
}
$result = [];
foreach ($fields as $field) {
$code = (string)$field['code'];
if (!isset($htmlFragments[$code])) {
if (isset($translatedUnits[$code])) {
$result[$code] = $translatedUnits[$code];
}
continue;
}
$nodeTranslations = [];
foreach ($fieldUnits[$code] ?? [] as $unitId) {
[, $nodeId] = explode('::', $unitId, 2);
$nodeTranslations[$nodeId] = $translatedUnits[$unitId] ?? '';
}
$translatedHtml = $htmlFragments[$code]->applyTranslations($nodeTranslations);
$errors = $this->htmlValidator->validate($translatedHtml);
if ($errors !== []) {
throw new \RuntimeException(sprintf(
'Translated HTML for "%s" is invalid: %s',
$code,
implode('; ', $errors)
));
}
$result[$code] = $translatedHtml;
}
return $result;
}
}