Skip to content

Commit

Permalink
[TASK] Refactor formality translation context handling.
Browse files Browse the repository at this point in the history
The problem with global formality configuration is true.
That a target language has not been checked to see whether formality is supported.
An error response from the DeepL API is returned for unsupported languages,
which states ‘formality not supported’.

To enable better handling for formality, only supported target languages.
An additional formality field is added in the SiteConfiguration,
which reflects the options of the API.
The Formality field only appears for supported target languages,
which is checked against the supported languages API.

An upgrade wizard has been provided to simplify the conversion of the Formality configuration to the SiteConfiguration.

Additionally, a DTO TranslateContext has been introduced,
to centralise parameter handling for better handling of translation parameters.
  • Loading branch information
NarkNiro committed Sep 3, 2024
1 parent dea7d0d commit 65a8fd2
Show file tree
Hide file tree
Showing 30 changed files with 1,111 additions and 309 deletions.
93 changes: 69 additions & 24 deletions Build/phpstan/Core11/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ parameters:
count: 1
path: ../../../Classes/Hooks/TCEmainHook.php

-
message: "#^Parameter \\#2 \\$currentRecordId of method WebVision\\\\WvDeepltranslate\\\\Service\\\\LanguageService\\:\\:getCurrentSite\\(\\) expects int, int\\|string given\\.$#"
count: 1
path: ../../../Classes/Hooks/TranslateHook.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Override\\\\Core11\\\\DatabaseRecordList\\:\\:makeLocalizationPanel\\(\\) has parameter \\$translations with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -150,11 +145,6 @@ parameters:
count: 1
path: ../../../Classes/Service/LanguageService.php

-
message: "#^Offset 'pid' does not exist on array\\|null\\.$#"
count: 1
path: ../../../Classes/Service/LanguageService.php

-
message: "#^Property WebVision\\\\WvDeepltranslate\\\\Service\\\\LanguageService\\:\\:\\$possibleLangMatches type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -320,11 +310,6 @@ parameters:
count: 1
path: ../../../Tests/Functional/Hooks/TranslateHookTest.php

-
message: "#^Offset 'site' does not exist on array\\{site\\: TYPO3\\\\CMS\\\\Core\\\\Site\\\\Entity\\\\Site, pageUid\\: int\\}\\|null\\.$#"
count: 2
path: ../../../Tests/Functional/Hooks/TranslateHookTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Hooks\\\\TranslateHookTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
Expand All @@ -340,11 +325,6 @@ parameters:
count: 7
path: ../../../Tests/Functional/Services/DeeplServiceTest.php

-
message: "#^Cannot access property \\$text on array\\<DeepL\\\\TextResult\\>\\|DeepL\\\\TextResult\\|null\\.$#"
count: 3
path: ../../../Tests/Functional/Services/DeeplServiceTest.php

-
message: "#^Class TYPO3\\\\CMS\\\\Core\\\\Configuration\\\\SiteConfiguration constructor invoked with 3 parameters, 1\\-2 required\\.$#"
count: 1
Expand Down Expand Up @@ -406,19 +386,84 @@ parameters:
path: ../../../Tests/Functional/Services/LanguageServiceTest.php

-
message: "#^Offset 'site' does not exist on array\\{site\\: TYPO3\\\\CMS\\\\Core\\\\Site\\\\Entity\\\\Site, pageUid\\: int\\}\\|null\\.$#"
count: 5
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Services\\\\LanguageServiceTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
path: ../../../Tests/Functional/Services/LanguageServiceTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Services\\\\LanguageServiceTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
message: "#^Parameter \\#2 \\$coreCache of class TYPO3\\\\CMS\\\\Core\\\\Configuration\\\\SiteConfiguration constructor expects TYPO3\\\\CMS\\\\Core\\\\Cache\\\\Frontend\\\\PhpFrontend\\|null, Psr\\\\EventDispatcher\\\\EventDispatcherInterface given\\.$#"
count: 1
path: ../../../Tests/Functional/Services/LanguageServiceTest.php

-
message: "#^Class TYPO3\\\\CMS\\\\Core\\\\Configuration\\\\SiteConfiguration constructor invoked with 3 parameters, 1\\-2 required\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildDefaultLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildErrorHandlingConfiguration\\(\\) has parameter \\$codes with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildErrorHandlingConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildLanguageConfiguration\\(\\) has parameter \\$fallbackIdentifiers with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildSiteConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:failIfArrayIsNotEmpty\\(\\) has parameter \\$items with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:mergeSiteConfiguration\\(\\) has parameter \\$overrides with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$errorHandling with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$site with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Parameter \\#2 \\$coreCache of class TYPO3\\\\CMS\\\\Core\\\\Configuration\\\\SiteConfiguration constructor expects TYPO3\\\\CMS\\\\Core\\\\Cache\\\\Frontend\\\\PhpFrontend\\|null, Psr\\\\EventDispatcher\\\\EventDispatcherInterface given\\.$#"
count: 1
path: ../../../Tests/Functional/Services/LanguageServiceTest.php
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Argument of an invalid type string supplied for foreach, only iterables are supported\\.$#"
Expand Down
83 changes: 59 additions & 24 deletions Build/phpstan/Core12/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ parameters:
count: 1
path: ../../../Classes/Hooks/TCEmainHook.php

-
message: "#^Parameter \\#2 \\$currentRecordId of method WebVision\\\\WvDeepltranslate\\\\Service\\\\LanguageService\\:\\:getCurrentSite\\(\\) expects int, int\\|string given\\.$#"
count: 1
path: ../../../Classes/Hooks/TranslateHook.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Override\\\\Core12\\\\DatabaseRecordList\\:\\:makeLocalizationPanel\\(\\) has parameter \\$translations with no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -145,11 +140,6 @@ parameters:
count: 1
path: ../../../Classes/Service/DeeplGlossaryService.php

-
message: "#^Offset 'pid' does not exist on array\\|null\\.$#"
count: 1
path: ../../../Classes/Service/LanguageService.php

-
message: "#^Property WebVision\\\\WvDeepltranslate\\\\Service\\\\LanguageService\\:\\:\\$possibleLangMatches type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -310,11 +300,6 @@ parameters:
count: 1
path: ../../../Tests/Functional/Hooks/TranslateHookTest.php

-
message: "#^Offset 'site' does not exist on array\\{site\\: TYPO3\\\\CMS\\\\Core\\\\Site\\\\Entity\\\\Site, pageUid\\: int\\}\\|null\\.$#"
count: 2
path: ../../../Tests/Functional/Hooks/TranslateHookTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Hooks\\\\TranslateHookTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
Expand All @@ -325,11 +310,6 @@ parameters:
count: 7
path: ../../../Tests/Functional/Services/DeeplServiceTest.php

-
message: "#^Cannot access property \\$text on array\\<DeepL\\\\TextResult\\>\\|DeepL\\\\TextResult\\|null\\.$#"
count: 3
path: ../../../Tests/Functional/Services/DeeplServiceTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Services\\\\LanguageServiceTest\\:\\:buildDefaultLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -386,14 +366,69 @@ parameters:
path: ../../../Tests/Functional/Services/LanguageServiceTest.php

-
message: "#^Offset 'site' does not exist on array\\{site\\: TYPO3\\\\CMS\\\\Core\\\\Site\\\\Entity\\\\Site, pageUid\\: int\\}\\|null\\.$#"
count: 5
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Services\\\\LanguageServiceTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
path: ../../../Tests/Functional/Services/LanguageServiceTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Services\\\\LanguageServiceTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildDefaultLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Services/LanguageServiceTest.php
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildErrorHandlingConfiguration\\(\\) has parameter \\$codes with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildErrorHandlingConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildLanguageConfiguration\\(\\) has parameter \\$fallbackIdentifiers with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:buildSiteConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:failIfArrayIsNotEmpty\\(\\) has parameter \\$items with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:mergeSiteConfiguration\\(\\) has parameter \\$overrides with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$errorHandling with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$site with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Updates\\\\FormalityUpgradeWizardTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
path: ../../../Tests/Functional/Updates/FormalityUpgradeWizardTest.php

-
message: "#^Argument of an invalid type string supplied for foreach, only iterables are supported\\.$#"
Expand Down
2 changes: 2 additions & 0 deletions Classes/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public function setLogger(LoggerInterface $logger): void

/**
* Wrapper function to handel ApiKey exception
*
* @throws ApiKeyNotSetException
*/
protected function getTranslator(): Translator
{
Expand Down
28 changes: 26 additions & 2 deletions Classes/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DeepL\TextResult;
use DeepL\TranslateTextOptions;
use DeepL\Usage;
use WebVision\WvDeepltranslate\Exception\ApiKeyNotSetException;

/**
* @internal No public usage
Expand All @@ -20,15 +21,18 @@ final class Client extends AbstractClient
{
/**
* @return TextResult|TextResult[]|null
*
* @throws ApiKeyNotSetException
*/
public function translate(
string $content,
?string $sourceLang,
string $targetLang,
string $glossary = ''
string $glossary = '',
string $formality = ''
) {
$options = [
TranslateTextOptions::FORMALITY => $this->configuration->getFormality(),
TranslateTextOptions::FORMALITY => $formality ?: 'default',
TranslateTextOptions::TAG_HANDLING => 'xml',
];

Expand Down Expand Up @@ -56,6 +60,8 @@ public function translate(

/**
* @return Language[]
*
* @throws ApiKeyNotSetException
*/
public function getSupportedLanguageByType(string $type = 'target'): array
{
Expand All @@ -76,6 +82,8 @@ public function getSupportedLanguageByType(string $type = 'target'): array

/**
* @return GlossaryLanguagePair[]
*
* @throws ApiKeyNotSetException
*/
public function getGlossaryLanguagePairs(): array
{
Expand All @@ -94,6 +102,8 @@ public function getGlossaryLanguagePairs(): array

/**
* @return GlossaryInfo[]
*
* @throws ApiKeyNotSetException
*/
public function getAllGlossaries(): array
{
Expand All @@ -110,6 +120,9 @@ public function getAllGlossaries(): array
return [];
}

/**
* @throws ApiKeyNotSetException
*/
public function getGlossary(string $glossaryId): ?GlossaryInfo
{
try {
Expand All @@ -127,6 +140,8 @@ public function getGlossary(string $glossaryId): ?GlossaryInfo

/**
* @param array<int, array{source: string, target: string}> $entries
*
* @throws ApiKeyNotSetException
*/
public function createGlossary(
string $glossaryName,
Expand Down Expand Up @@ -168,6 +183,9 @@ public function createGlossary(
}
}

/**
* @throws ApiKeyNotSetException
*/
public function deleteGlossary(string $glossaryId): void
{
try {
Expand All @@ -181,6 +199,9 @@ public function deleteGlossary(string $glossaryId): void
}
}

/**
* @throws ApiKeyNotSetException
*/
public function getGlossaryEntries(string $glossaryId): ?GlossaryEntries
{
try {
Expand All @@ -196,6 +217,9 @@ public function getGlossaryEntries(string $glossaryId): ?GlossaryEntries
return null;
}

/**
* @throws ApiKeyNotSetException
*/
public function getUsage(): ?Usage
{
try {
Expand Down
Loading

0 comments on commit 65a8fd2

Please sign in to comment.