Skip to content

Commit

Permalink
Merge pull request #37705 from nextcloud/backport/37704/stable26
Browse files Browse the repository at this point in the history
[stable26] fix(translation): Fix several issues with the translations api
  • Loading branch information
skjnldsv authored Apr 13, 2023
2 parents f95c76d + 35b0834 commit 836c63c
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions core/Controller/TranslationApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,30 @@
use InvalidArgumentException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\IL10N;
use OCP\IRequest;
use OCP\PreConditionNotMetException;
use OCP\Translation\ITranslationManager;
use RuntimeException;

class TranslationApiController extends \OCP\AppFramework\OCSController {
private ITranslationManager $translationManager;
private IL10N $l;

public function __construct($appName, IRequest $request, ITranslationManager $translationManager) {
public function __construct(
string $appName,
IRequest $request,
ITranslationManager $translationManager,
IL10N $l,
) {
parent::__construct($appName, $request);

$this->translationManager = $translationManager;
$this->l = $l;
}

/**
* @NoAdminRequired
* @PublicPage
*/
public function languages(): DataResponse {
return new DataResponse([
Expand All @@ -54,19 +62,21 @@ public function languages(): DataResponse {
}

/**
* @NoAdminRequired
* @PublicPage
* @UserRateThrottle(limit=25, period=120)
* @AnonRateThrottle(limit=10, period=120)
*/
public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
try {
return new DataResponse([
'text' => $this->translationManager->translate($text, $fromLanguage, $toLanguage)
]);
} catch (PreConditionNotMetException) {
return new DataResponse(['message' => 'No translation provider available'], Http::STATUS_PRECONDITION_FAILED);
return new DataResponse(['message' => $this->l->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
} catch (InvalidArgumentException) {
return new DataResponse(['message' => 'Could not detect language', Http::STATUS_NOT_FOUND]);
return new DataResponse(['message' => $this->l->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
} catch (RuntimeException) {
return new DataResponse(['message' => 'Unable to translate', Http::STATUS_INTERNAL_SERVER_ERROR]);
return new DataResponse(['message' => $this->l->t('Unable to translate')], Http::STATUS_BAD_REQUEST);
}
}
}

0 comments on commit 836c63c

Please sign in to comment.