fix(translation): Detect the language first and then ask all providers for translations

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2023-05-01 15:05:43 +02:00
parent ac56be10fb
commit dc67b48c28
No known key found for this signature in database
GPG key ID: C400AAF20C1BB6FC

View file

@ -63,15 +63,23 @@ class TranslationManager implements ITranslationManager {
throw new PreConditionNotMetException('No translation providers available');
}
foreach ($this->getProviders() as $provider) {
if ($fromLanguage === null && $provider instanceof IDetectLanguageProvider) {
$fromLanguage = $provider->detectLanguage($text);
if ($fromLanguage === null) {
foreach ($this->getProviders() as $provider) {
if ($provider instanceof IDetectLanguageProvider) {
$fromLanguage = $provider->detectLanguage($text);
}
if ($fromLanguage !== null) {
break;
}
}
if ($fromLanguage === null) {
throw new InvalidArgumentException('Could not detect language');
}
}
foreach ($this->getProviders() as $provider) {
try {
return $provider->translate($fromLanguage, $toLanguage, $text);
} catch (RuntimeException $e) {