You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TranslationApiController.php 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. * 200: Supported languages returned
  50. */
  51. public function languages(): DataResponse {
  52. return new DataResponse([
  53. 'languages' => array_map(fn ($lang) => $lang->jsonSerialize(), $this->translationManager->getLanguages()),
  54. 'languageDetection' => $this->translationManager->canDetectLanguage(),
  55. ]);
  56. }
  57. /**
  58. * @PublicPage
  59. * @UserRateThrottle(limit=25, period=120)
  60. * @AnonRateThrottle(limit=10, period=120)
  61. *
  62. * Translate a text
  63. *
  64. * @param string $text Text to be translated
  65. * @param string|null $fromLanguage Language to translate from
  66. * @param string $toLanguage Language to translate to
  67. * @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{}>
  68. *
  69. * 200: Translated text returned
  70. * 400: Language not detected or unable to translate
  71. * 412: Translating is not possible
  72. */
  73. public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
  74. try {
  75. $translation = $this->translationManager->translate($text, $fromLanguage, $toLanguage);
  76. return new DataResponse([
  77. 'text' => $translation,
  78. 'from' => $fromLanguage,
  79. ]);
  80. } catch (PreConditionNotMetException) {
  81. return new DataResponse(['message' => $this->l10n->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
  82. } catch (InvalidArgumentException) {
  83. return new DataResponse(['message' => $this->l10n->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
  84. } catch (CouldNotTranslateException $e) {
  85. return new DataResponse(['message' => $this->l10n->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST);
  86. }
  87. }
  88. }