Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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;
Expand All @@ -62,7 +63,7 @@ public function getForm() {
$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;
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,8 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 16 additions & 2 deletions lib/private/Translation/TranslationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@
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;
Expand All @@ -50,6 +53,7 @@ public function __construct(
private Coordinator $coordinator,
private LoggerInterface $logger,
private IConfig $config,
private IUserSession $userSession,
) {
}

Expand All @@ -73,19 +77,26 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua
$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);
}

Expand All @@ -105,6 +116,9 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua

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]);
Expand Down
37 changes: 37 additions & 0 deletions lib/public/Translation/ITranslationProviderWithId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Marcel Klehr <[email protected]>
*
* @author Marcel Klehr <[email protected]>
*
* @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 changes: 38 additions & 0 deletions lib/public/Translation/ITranslationProviderWithUserId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Marcel Klehr <[email protected]>
*
* @author Marcel Klehr <[email protected]>
*
* @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);
}