summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulius Härtl <jus@bitgrid.net>2024-01-11 21:50:50 +0100
committerGitHub <noreply@github.com>2024-01-11 21:50:50 +0100
commit2df26608ef70e13bb35c7032e1522112d897948b (patch)
treebf704a7f4467744d448f385599c8274d272f2ac4
parent2d0991e3e7c492a031d2fc7ef7d0f4820ba46209 (diff)
parent477630ed78ac5a8f602e939dff78998891e931a5 (diff)
downloadnextcloud-server-2df26608ef70e13bb35c7032e1522112d897948b.tar.gz
nextcloud-server-2df26608ef70e13bb35c7032e1522112d897948b.zip
Merge pull request #42649 from nextcloud/enh/machine-translation-provider-with-id
-rw-r--r--apps/settings/lib/Settings/Admin/ArtificialIntelligence.php3
-rw-r--r--lib/composer/composer/autoload_classmap.php2
-rw-r--r--lib/composer/composer/autoload_static.php2
-rw-r--r--lib/private/Translation/TranslationManager.php18
-rw-r--r--lib/public/Translation/ITranslationProviderWithId.php37
-rw-r--r--lib/public/Translation/ITranslationProviderWithUserId.php38
6 files changed, 97 insertions, 3 deletions
diff --git a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
index e2862139e49..ab0dc1446bb 100644
--- a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
+++ b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
@@ -37,6 +37,7 @@ use OCP\TextProcessing\IProvider;
use OCP\TextProcessing\IProviderWithId;
use OCP\TextProcessing\ITaskType;
use OCP\Translation\ITranslationManager;
+use OCP\Translation\ITranslationProviderWithId;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -62,7 +63,7 @@ class ArtificialIntelligence implements IDelegatedSettings {
$translationPreferences = [];
foreach ($this->translationManager->getProviders() as $provider) {
$translationProviders[] = [
- 'class' => $provider::class,
+ 'class' => $provider instanceof ITranslationProviderWithId ? $provider->getId() : $provider::class,
'name' => $provider->getName(),
];
$translationPreferences[] = $provider::class;
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 201b49f3fae..63ba07e2e4f 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -711,6 +711,8 @@ return array(
'OCP\\Translation\\IDetectLanguageProvider' => $baseDir . '/lib/public/Translation/IDetectLanguageProvider.php',
'OCP\\Translation\\ITranslationManager' => $baseDir . '/lib/public/Translation/ITranslationManager.php',
'OCP\\Translation\\ITranslationProvider' => $baseDir . '/lib/public/Translation/ITranslationProvider.php',
+ 'OCP\\Translation\\ITranslationProviderWithId' => $baseDir . '/lib/public/Translation/ITranslationProviderWithId.php',
+ 'OCP\\Translation\\ITranslationProviderWithUserId' => $baseDir . '/lib/public/Translation/ITranslationProviderWithUserId.php',
'OCP\\Translation\\LanguageTuple' => $baseDir . '/lib/public/Translation/LanguageTuple.php',
'OCP\\UserInterface' => $baseDir . '/lib/public/UserInterface.php',
'OCP\\UserMigration\\IExportDestination' => $baseDir . '/lib/public/UserMigration/IExportDestination.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index ebcccb328c2..bf1a13d49de 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -744,6 +744,8 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Translation\\IDetectLanguageProvider' => __DIR__ . '/../../..' . '/lib/public/Translation/IDetectLanguageProvider.php',
'OCP\\Translation\\ITranslationManager' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationManager.php',
'OCP\\Translation\\ITranslationProvider' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProvider.php',
+ 'OCP\\Translation\\ITranslationProviderWithId' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProviderWithId.php',
+ 'OCP\\Translation\\ITranslationProviderWithUserId' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProviderWithUserId.php',
'OCP\\Translation\\LanguageTuple' => __DIR__ . '/../../..' . '/lib/public/Translation/LanguageTuple.php',
'OCP\\UserInterface' => __DIR__ . '/../../..' . '/lib/public/UserInterface.php',
'OCP\\UserMigration\\IExportDestination' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IExportDestination.php',
diff --git a/lib/private/Translation/TranslationManager.php b/lib/private/Translation/TranslationManager.php
index 48a0e2cdebd..306275121ea 100644
--- a/lib/private/Translation/TranslationManager.php
+++ b/lib/private/Translation/TranslationManager.php
@@ -30,11 +30,14 @@ use InvalidArgumentException;
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\IConfig;
use OCP\IServerContainer;
+use OCP\IUserSession;
use OCP\PreConditionNotMetException;
use OCP\Translation\CouldNotTranslateException;
use OCP\Translation\IDetectLanguageProvider;
use OCP\Translation\ITranslationManager;
use OCP\Translation\ITranslationProvider;
+use OCP\Translation\ITranslationProviderWithId;
+use OCP\Translation\ITranslationProviderWithUserId;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;
@@ -50,6 +53,7 @@ class TranslationManager implements ITranslationManager {
private Coordinator $coordinator,
private LoggerInterface $logger,
private IConfig $config,
+ private IUserSession $userSession,
) {
}
@@ -73,19 +77,26 @@ class TranslationManager implements ITranslationManager {
$precedence = json_decode($json, true);
$newProviders = [];
foreach ($precedence as $className) {
- $provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
+ $provider = current(array_filter($providers, function ($provider) use ($className) {
+ return $provider instanceof ITranslationProviderWithId ? $provider->getId() === $className : $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));
+ $newProviders += array_udiff($providers, $newProviders, function ($a, $b) {
+ return ($a instanceof ITranslationProviderWithId ? $a->getId() : $a::class) <=> ($b instanceof ITranslationProviderWithId ? $b->getId() : $b::class);
+ });
$providers = $newProviders;
}
if ($fromLanguage === null) {
foreach ($providers as $provider) {
if ($provider instanceof IDetectLanguageProvider) {
+ if ($provider instanceof ITranslationProviderWithUserId) {
+ $provider->setUserId($this->userSession->getUser()?->getUID());
+ }
$fromLanguage = $provider->detectLanguage($text);
}
@@ -105,6 +116,9 @@ class TranslationManager implements ITranslationManager {
foreach ($providers as $provider) {
try {
+ if ($provider instanceof ITranslationProviderWithUserId) {
+ $provider->setUserId($this->userSession->getUser()?->getUID());
+ }
return $provider->translate($fromLanguage, $toLanguage, $text);
} catch (RuntimeException $e) {
$this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage} using provider {$provider->getName()}", ['exception' => $e]);
diff --git a/lib/public/Translation/ITranslationProviderWithId.php b/lib/public/Translation/ITranslationProviderWithId.php
new file mode 100644
index 00000000000..fa08ef7cb17
--- /dev/null
+++ b/lib/public/Translation/ITranslationProviderWithId.php
@@ -0,0 +1,37 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2024 Marcel Klehr <mklehr@gmx.net>
+ *
+ * @author Marcel Klehr <mklehr@gmx.net>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+namespace OCP\Translation;
+
+/**
+ * @since 29.0.0
+ */
+interface ITranslationProviderWithId extends ITranslationProvider {
+ /**
+ * @since 29.0.0
+ */
+ public function getId(): string;
+}
diff --git a/lib/public/Translation/ITranslationProviderWithUserId.php b/lib/public/Translation/ITranslationProviderWithUserId.php
new file mode 100644
index 00000000000..9a573a8150e
--- /dev/null
+++ b/lib/public/Translation/ITranslationProviderWithUserId.php
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2024 Marcel Klehr <mklehr@gmx.net>
+ *
+ * @author Marcel Klehr <mklehr@gmx.net>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+namespace OCP\Translation;
+
+/**
+ * @since 29.0.0
+ */
+interface ITranslationProviderWithUserId extends ITranslationProvider {
+ /**
+ * @param string|null $userId The userId of the user requesting the current task
+ * @since 29.0.0
+ */
+ public function setUserId(?string $userId);
+}