1
0

TranslationApiController.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use InvalidArgumentException;
  9. use OCP\AppFramework\Http;
  10. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\Attribute\PublicPage;
  13. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  14. use OCP\AppFramework\Http\DataResponse;
  15. use OCP\IL10N;
  16. use OCP\IRequest;
  17. use OCP\PreConditionNotMetException;
  18. use OCP\Translation\CouldNotTranslateException;
  19. use OCP\Translation\ITranslationManager;
  20. class TranslationApiController extends \OCP\AppFramework\OCSController {
  21. public function __construct(
  22. string $appName,
  23. IRequest $request,
  24. private ITranslationManager $translationManager,
  25. private IL10N $l10n,
  26. ) {
  27. parent::__construct($appName, $request);
  28. }
  29. /**
  30. * Get the list of supported languages
  31. *
  32. * @return DataResponse<Http::STATUS_OK, array{languages: array{from: string, fromLabel: string, to: string, toLabel: string}[], languageDetection: bool}, array{}>
  33. *
  34. * 200: Supported languages returned
  35. */
  36. #[PublicPage]
  37. #[ApiRoute(verb: 'GET', url: '/languages', root: '/translation')]
  38. public function languages(): DataResponse {
  39. return new DataResponse([
  40. 'languages' => array_map(fn ($lang) => $lang->jsonSerialize(), $this->translationManager->getLanguages()),
  41. 'languageDetection' => $this->translationManager->canDetectLanguage(),
  42. ]);
  43. }
  44. /**
  45. * Translate a text
  46. *
  47. * @param string $text Text to be translated
  48. * @param string|null $fromLanguage Language to translate from
  49. * @param string $toLanguage Language to translate to
  50. * @return DataResponse<Http::STATUS_OK, array{text: string, from: ?string}, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_PRECONDITION_FAILED|Http::STATUS_INTERNAL_SERVER_ERROR, array{message: string, from?: ?string}, array{}>
  51. *
  52. * 200: Translated text returned
  53. * 400: Language not detected or unable to translate
  54. * 412: Translating is not possible
  55. */
  56. #[PublicPage]
  57. #[UserRateLimit(limit: 25, period: 120)]
  58. #[AnonRateLimit(limit: 10, period: 120)]
  59. #[ApiRoute(verb: 'POST', url: '/translate', root: '/translation')]
  60. public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
  61. try {
  62. $translation = $this->translationManager->translate($text, $fromLanguage, $toLanguage);
  63. return new DataResponse([
  64. 'text' => $translation,
  65. 'from' => $fromLanguage,
  66. ]);
  67. } catch (PreConditionNotMetException) {
  68. return new DataResponse(['message' => $this->l10n->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
  69. } catch (InvalidArgumentException) {
  70. return new DataResponse(['message' => $this->l10n->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
  71. } catch (CouldNotTranslateException $e) {
  72. return new DataResponse(['message' => $this->l10n->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST);
  73. }
  74. }
  75. }