diff options
author | Marcel Klehr <mklehr@gmx.net> | 2023-07-25 14:17:57 +0200 |
---|---|---|
committer | Julien Veyssier <julien-nc@posteo.net> | 2023-08-02 12:37:35 +0200 |
commit | 2a3ef102f7527041609ad96bc59d6b21f42980d4 (patch) | |
tree | 2ca08e6288ff471de9f0c7664e990c25dd81c725 /lib/private/Translation | |
parent | a840e8c6e5a0d7b7c3a371776fd395976b2c2fdf (diff) | |
download | nextcloud-server-2a3ef102f7527041609ad96bc59d6b21f42980d4.tar.gz nextcloud-server-2a3ef102f7527041609ad96bc59d6b21f42980d4.zip |
AI admin settings: Use config values in AI feature managers
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
Diffstat (limited to 'lib/private/Translation')
-rw-r--r-- | lib/private/Translation/TranslationManager.php | 25 |
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]); } } |