diff options
author | Joas Schilling <213943+nickvergessen@users.noreply.github.com> | 2023-05-03 11:07:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-03 11:07:30 +0200 |
commit | 9ace8e561df1aa526f11ef6c2b6860502af4bc3c (patch) | |
tree | 550b9b00ba9658bbcf3ac5cf77746a7b2b8b9965 /core | |
parent | 8013bc91c86345a948a29290d8cc749cebf91099 (diff) | |
parent | 9d6ec68b593a62f4a17388464f8cae4ce23e8201 (diff) | |
download | nextcloud-server-9ace8e561df1aa526f11ef6c2b6860502af4bc3c.tar.gz nextcloud-server-9ace8e561df1aa526f11ef6c2b6860502af4bc3c.zip |
Merge pull request #38003 from nextcloud/bugfix/noid/improve-translations-api
Improve translations api with detecting languages
Diffstat (limited to 'core')
-rw-r--r-- | core/Controller/TranslationApiController.php | 12 |
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); } } } |