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
12 changes: 8 additions & 4 deletions core/Controller/TranslationApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
use OCP\IL10N;
use OCP\IRequest;
use OCP\PreConditionNotMetException;
use OCP\Translation\CouldNotTranslateException;
use OCP\Translation\ITranslationManager;
use RuntimeException;

class TranslationApiController extends \OCP\AppFramework\OCSController {
private ITranslationManager $translationManager;
Expand Down Expand Up @@ -68,15 +68,19 @@ public function languages(): DataResponse {
*/
public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
try {
$translation = $this->translationManager->translate($text, $fromLanguage, $toLanguage);

return new DataResponse([
'text' => $this->translationManager->translate($text, $fromLanguage, $toLanguage)
'text' => $translation,
'from' => $fromLanguage,

]);
} catch (PreConditionNotMetException) {
return new DataResponse(['message' => $this->l->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
} catch (InvalidArgumentException) {
return new DataResponse(['message' => $this->l->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
} catch (RuntimeException) {
return new DataResponse(['message' => $this->l->t('Unable to translate')], Http::STATUS_BAD_REQUEST);
} catch (CouldNotTranslateException $e) {
return new DataResponse(['message' => $this->l->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST);
}
}
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@
'OCP\\Talk\\IConversationOptions' => $baseDir . '/lib/public/Talk/IConversationOptions.php',
'OCP\\Talk\\ITalkBackend' => $baseDir . '/lib/public/Talk/ITalkBackend.php',
'OCP\\Template' => $baseDir . '/lib/public/Template.php',
'OCP\\Translation\\CouldNotTranslateException' => $baseDir . '/lib/public/Translation/CouldNotTranslateException.php',
'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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Talk\\IConversationOptions' => __DIR__ . '/../../..' . '/lib/public/Talk/IConversationOptions.php',
'OCP\\Talk\\ITalkBackend' => __DIR__ . '/../../..' . '/lib/public/Talk/ITalkBackend.php',
'OCP\\Template' => __DIR__ . '/../../..' . '/lib/public/Template.php',
'OCP\\Translation\\CouldNotTranslateException' => __DIR__ . '/../../..' . '/lib/public/Translation/CouldNotTranslateException.php',
'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',
Expand Down
19 changes: 14 additions & 5 deletions lib/private/Translation/TranslationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\IServerContainer;
use OCP\PreConditionNotMetException;
use OCP\Translation\CouldNotTranslateException;
use OCP\Translation\IDetectLanguageProvider;
use OCP\Translation\ITranslationManager;
use OCP\Translation\ITranslationProvider;
Expand Down Expand Up @@ -58,28 +59,36 @@ public function getLanguages(): array {
return $languages;
}

public function translate(string $text, ?string $fromLanguage, string $toLanguage): string {
public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string {
if (!$this->hasProviders()) {
throw new PreConditionNotMetException('No translation providers available');
}

foreach ($this->getProviders() as $provider) {
if ($fromLanguage === null && $provider instanceof IDetectLanguageProvider) {
$fromLanguage = $provider->detectLanguage($text);
if ($fromLanguage === null) {
foreach ($this->getProviders() as $provider) {
if ($provider instanceof IDetectLanguageProvider) {
$fromLanguage = $provider->detectLanguage($text);
}

if ($fromLanguage !== null) {
break;
}
}

if ($fromLanguage === null) {
throw new InvalidArgumentException('Could not detect language');
}
}

foreach ($this->getProviders() as $provider) {
try {
return $provider->translate($fromLanguage, $toLanguage, $text);
} catch (RuntimeException $e) {
$this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]);
}
}

throw new RuntimeException('Could not translate text');
throw new CouldNotTranslateException($fromLanguage);
}

public function getProviders(): array {
Expand Down
47 changes: 47 additions & 0 deletions lib/public/Translation/CouldNotTranslateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023 Joas Schilling <[email protected]>
*
* @author Joas Schilling <[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 27.0.0
*/
class CouldNotTranslateException extends \RuntimeException {
/**
* @since 27.0.0
*/
public function __construct(
protected ?string $from,
) {
parent::__construct();
}

/**
* @since 27.0.0
*/
public function getFrom(): ?string {
return $this->from;
}
}
5 changes: 2 additions & 3 deletions lib/public/Translation/ITranslationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

use InvalidArgumentException;
use OCP\PreConditionNotMetException;
use RuntimeException;

/**
* @since 26.0.0
Expand All @@ -54,7 +53,7 @@ public function getLanguages(): array;
* @since 26.0.0
* @throws PreConditionNotMetException If no provider was registered but this method was still called
* @throws InvalidArgumentException If no matching provider was found that can detect a language
* @throws RuntimeException If the translation failed for other reasons
* @throws CouldNotTranslateException If the translation failed for other reasons
*/
public function translate(string $text, ?string $fromLanguage, string $toLanguage): string;
public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string;
}