1
0

TranslationApiController.php 2.9 KB

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