Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback to DeepL autodetection if source language is unsupported #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Classes/Configuration/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public function getGlossaryForLanguagePair(string $sourceLangage, string $target
return $this->glossaries[$key] ?? null;
}

/**
* Fetches amount of glossaries.
*/
public function getCountGlossaries(Translator $translator): int
{
return count($translator->listGlossaries());
}

/**
* Fetches maximum number of glossaries per language pair.
*
Expand Down
20 changes: 17 additions & 3 deletions Classes/Service/DeeplTranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class DeeplTranslationService implements SingletonInterface
protected array $targetLanguages = [];

protected ?Translator $translator = null;

protected bool $autoDetectSourceLang = false;

/**
* Creates the instance of the class.
Expand Down Expand Up @@ -384,6 +386,18 @@ public function translateText(string $text, string $sourceLanguage, string $targ
TranslateTextOptions::PRESERVE_FORMATTING => true,
TranslateTextOptions::TAG_HANDLING => 'html',
];

// only necessary for autodetect with potential glossary,
// otherwise passing null below is sufficient
if ($this->autoDetectSourceLang && $this->configuration->getCountGlossaries($this->translator) > 0) {
$sourceLanguage = $this->translator->translateText(
$text,
null,
$targetLanguage,
$options
)->detectedSourceLang;
}

[$sourceLanguageForGlossary] = explode('-', $sourceLanguage);
[$targetLanguageForGlossary] = explode('-', $targetLanguage);
$glossary = $this->configuration->getGlossaryForLanguagePair($sourceLanguageForGlossary, $targetLanguageForGlossary);
Expand All @@ -393,7 +407,7 @@ public function translateText(string $text, string $sourceLanguage, string $targ

return empty($text) ? '' : $this->translator->translateText(
$text,
$sourceLanguage,
($this->autoDetectSourceLang && !$sourceLanguage) ? null : $sourceLanguage,
$targetLanguage,
$options
);
Expand Down Expand Up @@ -480,11 +494,11 @@ protected function canTranslate(SiteLanguage $sourceLanguage, SiteLanguage $targ
} elseif (!$this->isSupportedLanguage($sourceLanguage, $this->sourceLanguages)) {
$this->logger->notice(
sprintf(
'Language "%s" cannot be used as a source language because it is not supported',
'Language "%s" is not supported as a source language. Will try to proceed using DeepL autodetection',
$sourceLanguage->getLocale()->getLanguageCode()
)
);
$canTranslate = false;
$this->autoDetectSourceLang = true;
} elseif (!$this->isSupportedLanguage($targetLanguage, $this->targetLanguages)) {
$this->logger->notice(
sprintf(
Expand Down