mirror of
https://github.com/nextcloud/server.git
synced 2025-03-13 16:03:55 +00:00
Merge pull request #42649 from nextcloud/enh/machine-translation-provider-with-id
This commit is contained in:
commit
2df26608ef
6 changed files with 97 additions and 3 deletions
apps/settings/lib/Settings/Admin
lib
composer/composer
private/Translation
public/Translation
|
@ -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;
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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]);
|
||||
|
|
37
lib/public/Translation/ITranslationProviderWithId.php
Normal file
37
lib/public/Translation/ITranslationProviderWithId.php
Normal file
|
@ -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;
|
||||
}
|
38
lib/public/Translation/ITranslationProviderWithUserId.php
Normal file
38
lib/public/Translation/ITranslationProviderWithUserId.php
Normal file
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue