summaryrefslogtreecommitdiffstats
path: root/lib/private/Translation/TranslationManager.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Translation/TranslationManager.php')
-rw-r--r--lib/private/Translation/TranslationManager.php25
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/private/Translation/TranslationManager.php b/lib/private/Translation/TranslationManager.php
index 8456c41cdfc..48a0e2cdebd 100644
--- a/lib/private/Translation/TranslationManager.php
+++ b/lib/private/Translation/TranslationManager.php
@@ -28,6 +28,7 @@ namespace OC\Translation;
use InvalidArgumentException;
use OC\AppFramework\Bootstrap\Coordinator;
+use OCP\IConfig;
use OCP\IServerContainer;
use OCP\PreConditionNotMetException;
use OCP\Translation\CouldNotTranslateException;
@@ -48,6 +49,7 @@ class TranslationManager implements ITranslationManager {
private IServerContainer $serverContainer,
private Coordinator $coordinator,
private LoggerInterface $logger,
+ private IConfig $config,
) {
}
@@ -64,8 +66,25 @@ class TranslationManager implements ITranslationManager {
throw new PreConditionNotMetException('No translation providers available');
}
+ $providers = $this->getProviders();
+ $json = $this->config->getAppValue('core', 'ai.translation_provider_preferences', '');
+
+ if ($json !== '') {
+ $precedence = json_decode($json, true);
+ $newProviders = [];
+ foreach ($precedence as $className) {
+ $provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
+ if ($provider !== false) {
+ $newProviders[] = $provider;
+ }
+ }
+ // Add all providers that haven't been added so far
+ $newProviders += array_udiff($providers, $newProviders, fn ($a, $b) => $a::class > $b::class ? 1 : ($a::class < $b::class ? -1 : 0));
+ $providers = $newProviders;
+ }
+
if ($fromLanguage === null) {
- foreach ($this->getProviders() as $provider) {
+ foreach ($providers as $provider) {
if ($provider instanceof IDetectLanguageProvider) {
$fromLanguage = $provider->detectLanguage($text);
}
@@ -84,11 +103,11 @@ class TranslationManager implements ITranslationManager {
return $text;
}
- foreach ($this->getProviders() as $provider) {
+ foreach ($providers as $provider) {
try {
return $provider->translate($fromLanguage, $toLanguage, $text);
} catch (RuntimeException $e) {
- $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]);
+ $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage} using provider {$provider->getName()}", ['exception' => $e]);
}
}