TranslationApiController.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\Core\Controller;
  24. use InvalidArgumentException;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\IL10N;
  28. use OCP\IRequest;
  29. use OCP\PreConditionNotMetException;
  30. use OCP\Translation\CouldNotTranslateException;
  31. use OCP\Translation\ITranslationManager;
  32. class TranslationApiController extends \OCP\AppFramework\OCSController {
  33. private ITranslationManager $translationManager;
  34. private IL10N $l;
  35. public function __construct(
  36. string $appName,
  37. IRequest $request,
  38. ITranslationManager $translationManager,
  39. IL10N $l,
  40. ) {
  41. parent::__construct($appName, $request);
  42. $this->translationManager = $translationManager;
  43. $this->l = $l;
  44. }
  45. /**
  46. * @PublicPage
  47. */
  48. public function languages(): DataResponse {
  49. return new DataResponse([
  50. 'languages' => $this->translationManager->getLanguages(),
  51. 'languageDetection' => $this->translationManager->canDetectLanguage(),
  52. ]);
  53. }
  54. /**
  55. * @PublicPage
  56. * @UserRateThrottle(limit=25, period=120)
  57. * @AnonRateThrottle(limit=10, period=120)
  58. */
  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->l->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
  68. } catch (InvalidArgumentException) {
  69. return new DataResponse(['message' => $this->l->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
  70. } catch (CouldNotTranslateException $e) {
  71. return new DataResponse(['message' => $this->l->t('Unable to translate'), 'from' => $e->getFrom()], Http::STATUS_BAD_REQUEST);
  72. }
  73. }
  74. }