aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2023-05-01 15:14:18 +0200
committerJoas Schilling <coding@schilljs.com>2023-05-02 16:38:33 +0200
commit9d6ec68b593a62f4a17388464f8cae4ce23e8201 (patch)
tree6997ae010a21d06f504b6c73c55fd35df1e783ee /lib
parentdc67b48c28c37d77b29b9b9ed987d0664811eddf (diff)
downloadnextcloud-server-9d6ec68b593a62f4a17388464f8cae4ce23e8201.tar.gz
nextcloud-server-9d6ec68b593a62f4a17388464f8cae4ce23e8201.zip
feat(translation): Return the detected language so clients can show more details
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Translation/TranslationManager.php5
-rw-r--r--lib/public/Translation/CouldNotTranslateException.php47
-rw-r--r--lib/public/Translation/ITranslationManager.php5
5 files changed, 54 insertions, 5 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 90164534da1..0ef53c1342a 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -614,6 +614,7 @@ return array(
'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',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 43cdde51734..f1555732936 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -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',
diff --git a/lib/private/Translation/TranslationManager.php b/lib/private/Translation/TranslationManager.php
index 3fded754f6a..2a2b2cd0066 100644
--- a/lib/private/Translation/TranslationManager.php
+++ b/lib/private/Translation/TranslationManager.php
@@ -30,6 +30,7 @@ use InvalidArgumentException;
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;
@@ -58,7 +59,7 @@ class TranslationManager implements ITranslationManager {
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');
}
@@ -87,7 +88,7 @@ class TranslationManager implements ITranslationManager {
}
}
- throw new RuntimeException('Could not translate text');
+ throw new CouldNotTranslateException($fromLanguage);
}
public function getProviders(): array {
diff --git a/lib/public/Translation/CouldNotTranslateException.php b/lib/public/Translation/CouldNotTranslateException.php
new file mode 100644
index 00000000000..bf7f5b4e8b3
--- /dev/null
+++ b/lib/public/Translation/CouldNotTranslateException.php
@@ -0,0 +1,47 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com>
+ *
+ * @author Joas Schilling <coding@schilljs.com>
+ *
+ * @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;
+ }
+}
diff --git a/lib/public/Translation/ITranslationManager.php b/lib/public/Translation/ITranslationManager.php
index c6b67462152..4450f19c424 100644
--- a/lib/public/Translation/ITranslationManager.php
+++ b/lib/public/Translation/ITranslationManager.php
@@ -28,7 +28,6 @@ namespace OCP\Translation;
use InvalidArgumentException;
use OCP\PreConditionNotMetException;
-use RuntimeException;
/**
* @since 26.0.0
@@ -54,7 +53,7 @@ interface ITranslationManager {
* @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;
}