]> source.dussan.org Git - nextcloud-server.git/commitdiff
enh(OCP\Translation): Add ITranslationProviderWithId
authorMarcel Klehr <mklehr@gmx.net>
Tue, 9 Jan 2024 09:36:52 +0000 (10:36 +0100)
committerMarcel Klehr <mklehr@gmx.net>
Wed, 10 Jan 2024 10:16:14 +0000 (11:16 +0100)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
apps/settings/lib/Settings/Admin/ArtificialIntelligence.php
lib/private/Translation/TranslationManager.php
lib/public/Translation/ITranslationProviderWithId.php [new file with mode: 0644]

index e2862139e494bed03c82556d22c977205c495736..ab0dc1446bbf7f350a010e7c0a85a5dd33d07db0 100644 (file)
@@ -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;
index 48a0e2cdebdda7bf9c32aa93a36cc4ee738f010f..546ed595aaf6c72b2fb7aa5172df7da8f3acde48 100644 (file)
@@ -35,6 +35,7 @@ use OCP\Translation\CouldNotTranslateException;
 use OCP\Translation\IDetectLanguageProvider;
 use OCP\Translation\ITranslationManager;
 use OCP\Translation\ITranslationProvider;
+use OCP\Translation\ITranslationProviderWithId;
 use Psr\Container\ContainerExceptionInterface;
 use Psr\Container\NotFoundExceptionInterface;
 use Psr\Log\LoggerInterface;
@@ -73,13 +74,17 @@ 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, fn ($provider) =>
+                                 $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, fn ($a, $b) =>
+                               ($a instanceof ITranslationProviderWithId ? $a->getId() : $a::class) <=> ($b instanceof ITranslationProviderWithId ? $b->getId() : $b::class)
+                       );
                        $providers = $newProviders;
                }
 
diff --git a/lib/public/Translation/ITranslationProviderWithId.php b/lib/public/Translation/ITranslationProviderWithId.php
new file mode 100644 (file)
index 0000000..845e997
--- /dev/null
@@ -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 {
+       /**
+        * @since 29.0.0
+        */
+       public function getId(): string;
+}