TranslationApiController.php 3.4 KB

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