Skip to content

Commit 7e83cd9

Browse files
committed
feat(translations): Add translation provider API
Signed-off-by: Julius Härtl <[email protected]>
1 parent 1f26731 commit 7e83cd9

File tree

6 files changed

+223
-0
lines changed

6 files changed

+223
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
7+
*
8+
* @author Julius Härtl <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
27+
namespace OC\Core\Controller;
28+
29+
use OCP\AppFramework\Http\DataResponse;
30+
use OCP\IRequest;
31+
use OCP\Translation\ITranslationManager;
32+
use OCP\Translation\ITranslationProvider;
33+
34+
class TranslationApiController extends \OCP\AppFramework\OCSController {
35+
36+
private ITranslationManager $provider;
37+
38+
public function __construct($appName, IRequest $request, ITranslationManager $provider) {
39+
parent::__construct($appName, $request);
40+
41+
$this->provider = $provider;
42+
}
43+
44+
public function languages(): DataResponse {
45+
return new DataResponse([
46+
'languages' => $this->provider->getLanguages()
47+
]);
48+
}
49+
50+
public function translate(string $text, string $fromLanguage, string $toLanguage): DataResponse {
51+
return new DataResponse([
52+
'text' => $this->provider->translate($text, $fromLanguage, $toLanguage)
53+
]);
54+
}
55+
56+
}

core/routes.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@
143143
['root' => '/search', 'name' => 'UnifiedSearch#getProviders', 'url' => '/providers', 'verb' => 'GET'],
144144
['root' => '/search', 'name' => 'UnifiedSearch#search', 'url' => '/providers/{providerId}/search', 'verb' => 'GET'],
145145

146+
['root' => '/translation', 'name' => 'TranslationApi#languages', 'url' => '/languages', 'verb' => 'GET'],
147+
['root' => '/translation', 'name' => 'TranslationApi#translate', 'url' => '/translate', 'verb' => 'POST'],
146148
],
147149
]);
148150

lib/private/Server.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
use OC\Tagging\TagMapper;
150150
use OC\Talk\Broker;
151151
use OC\Template\JSCombiner;
152+
use OC\Translation\TranslationManager;
152153
use OC\User\DisplayNameCache;
153154
use OC\User\Listeners\BeforeUserDeletedListener;
154155
use OC\User\Listeners\UserChangedListener;
@@ -244,6 +245,7 @@
244245
use OCP\SystemTag\ISystemTagManager;
245246
use OCP\SystemTag\ISystemTagObjectMapper;
246247
use OCP\Talk\IBroker;
248+
use OCP\Translation\ITranslationManager;
247249
use OCP\User\Events\BeforePasswordUpdatedEvent;
248250
use OCP\User\Events\BeforeUserDeletedEvent;
249251
use OCP\User\Events\BeforeUserLoggedInEvent;
@@ -1453,6 +1455,8 @@ public function __construct($webRoot, \OC\Config $config) {
14531455

14541456
$this->registerAlias(\OCP\Share\IPublicShareTemplateFactory::class, \OC\Share20\PublicShareTemplateFactory::class);
14551457

1458+
$this->registerAlias(ITranslationManager::class, TranslationManager::class);
1459+
14561460
$this->connectDispatcher();
14571461
}
14581462

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @copyright Copyright (c) 2023 Julius Härtl <[email protected]>
6+
*
7+
* @author Julius Härtl <[email protected]>
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+
25+
26+
namespace OC\Translation;
27+
28+
use OCP\Translation\ITranslationManager;
29+
use OCP\Translation\ITranslationProvider;
30+
31+
class TranslationManager implements ITranslationManager {
32+
33+
/** @var ITranslationProvider[] */
34+
private array $providers = [];
35+
36+
public function __construct() {
37+
}
38+
39+
public function registerProvider(ITranslationProvider $provider) {
40+
$this->providers[] = $provider;
41+
}
42+
43+
public function getLanguages(): array {
44+
$languages = [];
45+
foreach ($this->providers as $provider) {
46+
$languages = array_merge($languages, $provider->getAvailableLanguages());
47+
}
48+
return $languages;
49+
}
50+
51+
public function translate(string $text, string $fromLanguage, string $toLanguage): string {
52+
foreach ($this->providers as $provider) {
53+
return $provider->translate($fromLanguage, $toLanguage, $text) ?? 'no translation';
54+
}
55+
return 'no provider';
56+
}
57+
58+
public function hasProviders(): bool {
59+
return !empty($this->providers);
60+
}
61+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Julius Härtl <[email protected]>
7+
*
8+
* @author Julius Härtl <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
27+
namespace OCP\Translation;
28+
29+
/**
30+
* @since 26.0.0
31+
*/
32+
interface ITranslationManager {
33+
34+
/**
35+
* @since 26.0.0
36+
*/
37+
public function hasProviders(): bool;
38+
39+
/**
40+
* @since 26.0.0
41+
*/
42+
public function getLanguages(): array;
43+
44+
/**
45+
* @since 26.0.0
46+
*/
47+
public function translate(string $text, string $fromLanguage, string $toLanguage): string;
48+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* @copyright Copyright (c) 2022 Julius Härtl <[email protected]>
6+
*
7+
* @author Julius Härtl <[email protected]>
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+
25+
26+
namespace OCP\Translation;
27+
28+
/**
29+
* @since 26.0.0
30+
*/
31+
interface ITranslationProvider {
32+
33+
/**
34+
* @since 26.0.0
35+
*/
36+
public function getName(): string;
37+
38+
/**
39+
* @since 26.0.0
40+
*/
41+
public function getAvailableLanguages(): array;
42+
43+
/**
44+
* @since 26.0.0
45+
*/
46+
public function detectLanguage(string $text): ?string;
47+
48+
/**
49+
* @since 26.0.0
50+
*/
51+
public function translate(?string $fromLanguage, string $toLanguage, string $text): string;
52+
}

0 commit comments

Comments
 (0)