summaryrefslogtreecommitdiffstats
path: root/core/Controller
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 /core/Controller
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 'core/Controller')
-rw-r--r--core/Controller/TranslationApiController.php12
1 files changed, 8 insertions, 4 deletions
diff --git a/core/Controller/TranslationApiController.php b/core/Controller/TranslationApiController.php
index a09f7e6aba9..2d47d99e654 100644
--- a/core/Controller/TranslationApiController.php
+++ b/core/Controller/TranslationApiController.php
@@ -32,8 +32,8 @@ use OCP\AppFramework\Http\DataResponse;
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;
@@ -68,15 +68,19 @@ class TranslationApiController extends \OCP\AppFramework\OCSController {
*/
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);
}
}
}