Skip to content

Commit 836c63c

Browse files
authored
Merge pull request #37705 from nextcloud/backport/37704/stable26
[stable26] fix(translation): Fix several issues with the translations api
2 parents f95c76d + 35b0834 commit 836c63c

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

core/Controller/TranslationApiController.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,30 @@
2929
use InvalidArgumentException;
3030
use OCP\AppFramework\Http;
3131
use OCP\AppFramework\Http\DataResponse;
32+
use OCP\IL10N;
3233
use OCP\IRequest;
3334
use OCP\PreConditionNotMetException;
3435
use OCP\Translation\ITranslationManager;
3536
use RuntimeException;
3637

3738
class TranslationApiController extends \OCP\AppFramework\OCSController {
3839
private ITranslationManager $translationManager;
40+
private IL10N $l;
3941

40-
public function __construct($appName, IRequest $request, ITranslationManager $translationManager) {
42+
public function __construct(
43+
string $appName,
44+
IRequest $request,
45+
ITranslationManager $translationManager,
46+
IL10N $l,
47+
) {
4148
parent::__construct($appName, $request);
4249

4350
$this->translationManager = $translationManager;
51+
$this->l = $l;
4452
}
4553

4654
/**
47-
* @NoAdminRequired
55+
* @PublicPage
4856
*/
4957
public function languages(): DataResponse {
5058
return new DataResponse([
@@ -54,19 +62,21 @@ public function languages(): DataResponse {
5462
}
5563

5664
/**
57-
* @NoAdminRequired
65+
* @PublicPage
66+
* @UserRateThrottle(limit=25, period=120)
67+
* @AnonRateThrottle(limit=10, period=120)
5868
*/
5969
public function translate(string $text, ?string $fromLanguage, string $toLanguage): DataResponse {
6070
try {
6171
return new DataResponse([
6272
'text' => $this->translationManager->translate($text, $fromLanguage, $toLanguage)
6373
]);
6474
} catch (PreConditionNotMetException) {
65-
return new DataResponse(['message' => 'No translation provider available'], Http::STATUS_PRECONDITION_FAILED);
75+
return new DataResponse(['message' => $this->l->t('No translation provider available')], Http::STATUS_PRECONDITION_FAILED);
6676
} catch (InvalidArgumentException) {
67-
return new DataResponse(['message' => 'Could not detect language', Http::STATUS_NOT_FOUND]);
77+
return new DataResponse(['message' => $this->l->t('Could not detect language')], Http::STATUS_BAD_REQUEST);
6878
} catch (RuntimeException) {
69-
return new DataResponse(['message' => 'Unable to translate', Http::STATUS_INTERNAL_SERVER_ERROR]);
79+
return new DataResponse(['message' => $this->l->t('Unable to translate')], Http::STATUS_BAD_REQUEST);
7080
}
7181
}
7282
}

0 commit comments

Comments
 (0)