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 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\ITranslationManager;
  31. use RuntimeException;
  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. return new DataResponse([
  62. 'text' => $this->translationManager->translate($text, $fromLanguage, $toLanguage)
  63. ]);
  64. } catch (PreConditionNotMetException) {
  65. return new DataResponse(['message' => $this->l->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
  66. } catch (InvalidArgumentException) {
  67. return new DataResponse(['message' => $this->l->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
  68. } catch (RuntimeException) {
  69. return new DataResponse(['message' => $this->l->t('Unable to translate')], Http::STATUS_BAD_REQUEST);
  70. }
  71. }
  72. }