]> source.dussan.org Git - nextcloud-server.git/commitdiff
enh(OCP\Translation): Introduce ITranslationProviderWithUserId
authorMarcel Klehr <mklehr@gmx.net>
Wed, 10 Jan 2024 14:17:02 +0000 (15:17 +0100)
committerMarcel Klehr <mklehr@gmx.net>
Wed, 10 Jan 2024 15:00:24 +0000 (16:00 +0100)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
lib/private/Translation/TranslationManager.php
lib/public/Translation/ITranslationProviderWithUserId.php [new file with mode: 0644]

index 546ed595aaf6c72b2fb7aa5172df7da8f3acde48..4b5facebac6f6a8cbf5b7efa3023975acf05337a 100644 (file)
@@ -30,12 +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;
@@ -51,6 +53,7 @@ class TranslationManager implements ITranslationManager {
                private Coordinator $coordinator,
                private LoggerInterface $logger,
                private IConfig $config,
+               private IUserSession $userSession,
        ) {
        }
 
@@ -91,6 +94,9 @@ class TranslationManager implements ITranslationManager {
                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);
                                }
 
@@ -110,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/ITranslationProviderWithUserId.php b/lib/public/Translation/ITranslationProviderWithUserId.php
new file mode 100644 (file)
index 0000000..9a573a8
--- /dev/null
@@ -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);
+}