Skip to content

Commit

Permalink
[FEATURE] Add Event for Glossary detection
Browse files Browse the repository at this point in the history
  • Loading branch information
calien666 committed Dec 17, 2024
1 parent 8f0b2a9 commit ec04cb0
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 23 deletions.
41 changes: 41 additions & 0 deletions Classes/Domain/Dto/CurrentPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace WebVision\Deepltranslate\Core\Domain\Dto;

use WebVision\Deepltranslate\Core\Event\GlossaryKeyEvent;
use WebVision\Deepltranslate\Core\Exception\MissingParameterException;

/**
* This class holds the current working page
* while processing inside the DataHandler
*
* It is the main entry point for extensions during events
* detecting the right page,
* for example, while detecting a glossary.
*
* @see GlossaryKeyEvent for a usage example
*/
final class CurrentPage
{
public function __construct(
public readonly int $uid,
public readonly string $title
) {
}

/**
* @throws MissingParameterException
*/
public static function fromArray(array $record): self
{
if (!array_key_exists('uid', $record) || !array_key_exists('title', $record)) {
throw new MissingParameterException(
'Missing parameters in creating CurrentPage Domain Transfer Object',
1734445433
);
}
return new self((int)$record['uid'], $record['title']);
}
}
44 changes: 44 additions & 0 deletions Classes/Event/GlossaryKeyEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace WebVision\Deepltranslate\Core\Event;

use WebVision\Deepltranslate\Core\Domain\Dto\CurrentPage;

final class GlossaryKeyEvent
{
private ?string $glossaryId = null;

public function __construct(
private readonly string $sourceLanguage,
private readonly string $targetLanguage,
private readonly ?CurrentPage $currentPage
) {
}

public function setGlossaryId(string $glossaryId): void
{
$this->glossaryId = $glossaryId;
}

public function getGlossaryId(): ?string
{
return $this->glossaryId;
}

public function getSourceLanguage(): string
{
return $this->sourceLanguage;
}

public function getTargetLanguage(): string
{
return $this->targetLanguage;
}

public function getCurrentPage(): ?CurrentPage
{
return $this->currentPage;
}
}
9 changes: 9 additions & 0 deletions Classes/Exception/MissingParameterException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace WebVision\Deepltranslate\Core\Exception;

final class MissingParameterException extends \Exception
{
}
22 changes: 11 additions & 11 deletions Classes/Service/DeeplService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use WebVision\Deepltranslate\Core\ClientInterface;
use WebVision\Deepltranslate\Core\Domain\Dto\TranslateContext;
use WebVision\Deepltranslate\Core\Domain\Repository\GlossaryRepository;
use WebVision\Deepltranslate\Core\Event\GlossaryKeyEvent;
use WebVision\Deepltranslate\Core\Exception\ApiKeyNotSetException;
use WebVision\Deepltranslate\Core\Utility\DeeplBackendUtility;

final class DeeplService implements LoggerAwareInterface
{
use LoggerAwareTrait;

protected GlossaryRepository $glossaryRepository;

private FrontendInterface $cache;

private ClientInterface $client;
Expand All @@ -28,12 +27,11 @@ final class DeeplService implements LoggerAwareInterface
public function __construct(
FrontendInterface $cache,
ClientInterface $client,
GlossaryRepository $glossaryRepository,
ProcessingInstruction $processingInstruction
ProcessingInstruction $processingInstruction,
private readonly EventDispatcher $eventDispatcher
) {
$this->cache = $cache;
$this->client = $client;
$this->glossaryRepository = $glossaryRepository;
$this->processingInstruction = $processingInstruction;
}

Expand Down Expand Up @@ -69,14 +67,16 @@ public function translateContent(TranslateContext $translateContext): string
}
// If the source language is set to Autodetect, no glossary can be detected.
if ($translateContext->getSourceLanguageCode() !== null) {
// @todo Make glossary findable by current site.
$glossary = $this->glossaryRepository->getGlossaryBySourceAndTarget(
/** @var GlossaryKeyEvent $glossaryEvent */
$glossaryEvent = $this->eventDispatcher->dispatch(new GlossaryKeyEvent(
$translateContext->getSourceLanguageCode(),
$translateContext->getTargetLanguageCode(),
DeeplBackendUtility::detectCurrentPage($this->processingInstruction)
);

$translateContext->setGlossaryId($glossary['glossary_id']);
));
$glossaryId = $glossaryEvent->getGlossaryId();
if ($glossaryId !== null) {
$translateContext->setGlossaryId($glossaryId);
}
}

try {
Expand Down
18 changes: 6 additions & 12 deletions Classes/Utility/DeeplBackendUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use WebVision\Deepltranslate\Core\Configuration;
use WebVision\Deepltranslate\Core\Domain\Dto\CurrentPage;
use WebVision\Deepltranslate\Core\Exception\LanguageIsoCodeNotFoundException;
use WebVision\Deepltranslate\Core\Exception\LanguageRecordNotFoundException;
use WebVision\Deepltranslate\Core\Service\IconOverlayGenerator;
use WebVision\Deepltranslate\Core\Service\LanguageService;
use WebVision\Deepltranslate\Core\Service\ProcessingInstruction;
use WebVision\Deepltranslate\Core\Domain\Dto\CurrentPage;

// @todo Make class final. Overriding a static utility class does not make much sense, but better to enforce it.
class DeeplBackendUtility
Expand All @@ -33,10 +35,7 @@ class DeeplBackendUtility

private static bool $configurationLoaded = false;

/**
* @var array{uid: int, title: string}|array<empty>
*/
protected static array $currentPage;
protected static ?CurrentPage $currentPage = null;

/**
* @return string
Expand Down Expand Up @@ -238,15 +237,10 @@ private static function getBackendUserAuthentication(): BackendUserAuthenticatio
return $GLOBALS['BE_USER'];
}

/**
* @return array{uid: int, title: string}|array<empty>
*/
public static function detectCurrentPage(ProcessingInstruction $processingInstruction): array
public static function detectCurrentPage(ProcessingInstruction $processingInstruction): ?CurrentPage
{
self::$currentPage = [];

if ($processingInstruction->getProcessingTable() === 'pages') {
self::$currentPage = self::getPageRecord((int)$processingInstruction->getProcessingId());
self::$currentPage = CurrentPage::fromArray(self::getPageRecord((int)$processingInstruction->getProcessingId()));
} elseif (
$processingInstruction->getProcessingTable() !== null
&& strlen($processingInstruction->getProcessingTable()) > 0
Expand All @@ -256,7 +250,7 @@ public static function detectCurrentPage(ProcessingInstruction $processingInstru
(string)$processingInstruction->getProcessingTable(),
(int)$processingInstruction->getProcessingId()
);
self::$currentPage = self::getPageRecord($pageId);
self::$currentPage = CurrentPage::fromArray(self::getPageRecord($pageId));
}

return self::$currentPage;
Expand Down

0 comments on commit ec04cb0

Please sign in to comment.