-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Respect rte allow tags in translate request
With this change the RTE configuration is determined from the field to be translated. To give deepl a list of allowed HTML tags. To allow a correct separation in the translated text with HTML tag.
- Loading branch information
Showing
8 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebVision\WvDeepltranslate\Domain\Dto; | ||
|
||
final class TranslateOptions | ||
{ | ||
protected string $splitSentences = 'nonewlines'; | ||
|
||
protected bool $outlineDetection = false; | ||
|
||
protected array $splittingTags = []; | ||
|
||
protected array $nonSplittingTags = []; | ||
|
||
protected array $ignoreTags = []; | ||
|
||
protected string $tagHandling = 'xml'; | ||
|
||
public function getSplitSentences(): string | ||
{ | ||
return $this->splitSentences; | ||
} | ||
|
||
public function setSplitSentences(string $splitSentences): void | ||
{ | ||
$this->splitSentences = $splitSentences; | ||
} | ||
|
||
public function isOutlineDetection(): bool | ||
{ | ||
return $this->outlineDetection; | ||
} | ||
|
||
public function setOutlineDetection(bool $outlineDetection): void | ||
{ | ||
$this->outlineDetection = $outlineDetection; | ||
} | ||
|
||
public function getSplittingTags(): array | ||
{ | ||
return $this->splittingTags; | ||
} | ||
|
||
public function setSplittingTags(array $splittingTags): void | ||
{ | ||
$this->splittingTags = $splittingTags; | ||
} | ||
|
||
public function getNonSplittingTags(): array | ||
{ | ||
return $this->nonSplittingTags; | ||
} | ||
|
||
public function setNonSplittingTags(array $nonSplittingTags): void | ||
{ | ||
$this->nonSplittingTags = $nonSplittingTags; | ||
} | ||
|
||
public function getIgnoreTags(): array | ||
{ | ||
return $this->ignoreTags; | ||
} | ||
|
||
public function setIgnoreTags(array $ignoreTags): void | ||
{ | ||
$this->ignoreTags = $ignoreTags; | ||
} | ||
|
||
public function getTagHandling(): string | ||
{ | ||
return $this->tagHandling; | ||
} | ||
|
||
public function setTagHandling(string $tagHandling): void | ||
{ | ||
$this->tagHandling = $tagHandling; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$param = []; | ||
$param['tag_handling'] = $this->tagHandling; | ||
|
||
if (!empty($this->splittingTags)) { | ||
$param['outlineDetection'] = $this->outlineDetection; | ||
$param['split_sentences'] = $this->splitSentences; | ||
$param['splitting_tags'] = $this->splittingTags; | ||
} | ||
|
||
return $param; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebVision\WvDeepltranslate\Resolver; | ||
|
||
use TYPO3\CMS\Backend\Utility\BackendUtility; | ||
use TYPO3\CMS\Core\Configuration\Richtext; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
|
||
class RichtextAllowTagsResolver | ||
{ | ||
private Richtext $richtext; | ||
|
||
public function __construct() | ||
{ | ||
$this->richtext = GeneralUtility::makeInstance(Richtext::class); | ||
} | ||
|
||
public function resolve(string $tableName, int $recordId): array | ||
{ | ||
if (!isset($GLOBALS['TCA'][$tableName]['columns'])) { | ||
throw new \RuntimeException('TCA Columns ist not defined', 1689950520561); | ||
} | ||
|
||
$columns = $GLOBALS['TCA'][$tableName]['columns']; | ||
|
||
$rteTextFields = []; | ||
foreach ($columns as $fieldName => $config) { | ||
if (!isset($config['config']['type'])) { | ||
continue; | ||
} | ||
|
||
if ($config['config']['type'] !== 'text') { | ||
continue; | ||
} | ||
|
||
if (!isset($config['config']['enableRichtext'])) { | ||
if ($config['config']['enableRichtext'] === false) { | ||
continue; | ||
} | ||
} elseif (!isset($GLOBALS['TCA'][$tableName]['types']['columnsOverrides'][$fieldName]['config']['enableRichtext'])) { | ||
if ($GLOBALS['TCA'][$tableName]['types']['columnsOverrides'][$fieldName]['config']['enableRichtext'] === false) { | ||
continue; | ||
} | ||
} | ||
|
||
$rteTextFields[$fieldName] = $config['config']; | ||
} | ||
|
||
if (empty($rteTextFields)) { | ||
return []; | ||
} | ||
|
||
$record = BackendUtility::getRecord($tableName, $recordId); | ||
|
||
$allowTags = []; | ||
foreach ($rteTextFields as $fieldName => $config) { | ||
$rteConfig = $this->richtext->getConfiguration($tableName, $fieldName, $record['pid'], $record['CType'], $config); | ||
if (isset($rteConfig['processing']['allowTags'])) { | ||
$allowTags = array_unique(array_merge($allowTags, $rteConfig['processing']['allowTags']), SORT_REGULAR); | ||
} | ||
} | ||
|
||
return $allowTags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<dataset> | ||
<pages> | ||
<uid>1</uid> | ||
<pid>0</pid> | ||
<title>DeepL-Functional-Tests</title> | ||
<doktype>1</doktype> | ||
<is_siteroot>1</is_siteroot> | ||
</pages> | ||
<pages> | ||
<uid>3</uid> | ||
<pid>0</pid> | ||
<title>DeepL-Functional-Tests BS</title> | ||
<doktype>1</doktype> | ||
<is_siteroot>1</is_siteroot> | ||
</pages> | ||
<tt_content> | ||
<uid>1</uid> | ||
<pid>1</pid> | ||
<header>DeepL-Functional-Test Element</header> | ||
<CType>text</CType> | ||
<bodytext></bodytext> | ||
</tt_content> | ||
</dataset> |
50 changes: 50 additions & 0 deletions
50
Tests/Functional/Resolver/RichtextAllowTagsResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Functional\Resolver; | ||
|
||
use Nimut\TestingFramework\TestCase\FunctionalTestCase; | ||
use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader; | ||
use TYPO3\CMS\Core\Utility\GeneralUtility; | ||
use WebVision\WvDeepltranslate\Resolver\RichtextAllowTagsResolver; | ||
|
||
class RichtextAllowTagsResolverTest extends FunctionalTestCase | ||
{ | ||
/** | ||
* @var string[] | ||
*/ | ||
protected $testExtensionsToLoad = [ | ||
'typo3conf/ext/wv_deepltranslate', | ||
]; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $coreExtensionsToLoad = [ | ||
'rte_ckeditor', | ||
]; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->importDataSet(__DIR__ . '/Fixtures/Pages.xml'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function findRteConfigurationAllowTagsByRecords(): void | ||
{ | ||
$richtextAllowTagsResolver = GeneralUtility::makeInstance(RichtextAllowTagsResolver::class); | ||
$allowTags = $richtextAllowTagsResolver->resolve('tt_content', 1); | ||
|
||
static::assertTrue((bool)array_search('em', $allowTags)); | ||
|
||
$yamlLoader = GeneralUtility::makeInstance(YamlFileLoader::class); | ||
$config = $yamlLoader->load('EXT:rte_ckeditor/Configuration/RTE/Processing.yaml'); | ||
|
||
static::assertSame($config['processing']['allowTags'], $allowTags); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters