1
0

TranslationApiController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. namespace OC\Core\Controller;
  25. use InvalidArgumentException;
  26. use OCP\AppFramework\Http;
  27. use OCP\AppFramework\Http\Attribute\ApiRoute;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\IL10N;
  30. use OCP\IRequest;
  31. use OCP\PreConditionNotMetException;
  32. use OCP\Translation\CouldNotTranslateException;
  33. use OCP\Translation\ITranslationManager;
  34. class TranslationApiController extends \OCP\AppFramework\OCSController {
  35. public function __construct(
  36. string $appName,
  37. IRequest $request,
  38. private ITranslationManager $translationManager,
  39. private IL10N $l10n,
  40. ) {
  41. parent::__construct($appName, $request);
  42. }
  43. /**
  44. * @PublicPage
  45. *
  46. * Get the list of supported languages
  47. *
  48. * @return DataResponse<Http::STATUS_OK, array{languages: array{from: string, fromLabel: string, to: string, toLabel: string}[], languageDetection: bool}, array{}>
  49. *
  50. * 200: Supported languages returned
  51. */
  52. #[ApiRoute(verb: 'GET', url: '/languages', root: '/translation')]
  53. public function languages(): DataResponse {
  54. return new DataResponse([
  55. 'languages' => array_map(fn ($lang) => $lang->jsonSerialize(), $this->translationManager->getLanguages()),
  56. 'languageDetection' => $this->translationManager->canDetectLanguage(),
  57. ]);
  58. }
  59. /**
  60. * @PublicPage
  61. * @UserRateThrottle(limit=25, period=120)
  62. * @AnonRateThrottle(limit=10, period=120)
  63. *
  64. * Translate a text
  65. *
  66. * @param string $text Text to be translated
  67. * @param string|null $fromLanguage Language to translate from
  68. * @param string $toLanguage Language to translate to
  69. * @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{}>
  70. *
  71. * 200: Translated text returned
  72. * 400: Language not detected or unable to translate
  73. * 412: Translating is not possible
  74. */
  75. #[ApiRoute(verb: 'POST', url: '/translate', root: '/translation')]
  76. public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
  77. try {
  78. $translation = $this->translationManager->translate($text, $fromLanguage, $toLanguage);
  79. return new DataResponse([
  80. 'text' => $translation,
  81. 'from' => $fromLanguage,
  82. ]);
  83. } catch (PreConditionNotMetException) {
  84. return new DataResponse(['message' => $this->l10n->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
  85. } catch (InvalidArgumentException) {
  86. return new DataResponse(['message' => $this->l10n->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
  87. } catch (CouldNotTranslateException $e) {
  88. return new DataResponse(['message' => $this->l10n->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST);
  89. }
  90. }
  91. }