diff options
author | Julius Härtl <jus@bitgrid.net> | 2023-02-07 14:13:04 +0100 |
---|---|---|
committer | Julius Härtl <jus@bitgrid.net> | 2023-02-27 16:52:03 +0100 |
commit | 3e6329838158892e3b89c7e5116fa313c282a8a1 (patch) | |
tree | 0e55e55fc3053b1263cf3a39fdfa25665fe3388e /lib/public/Translation | |
parent | 0d67fc23f4077e7b06a01bc519957f3f13d95f10 (diff) | |
download | nextcloud-server-3e6329838158892e3b89c7e5116fa313c282a8a1.tar.gz nextcloud-server-3e6329838158892e3b89c7e5116fa313c282a8a1.zip |
feat(translations): Add translation provider API
Signed-off-by: Julius Härtl <jus@bitgrid.net>
Diffstat (limited to 'lib/public/Translation')
-rw-r--r-- | lib/public/Translation/IDetectLanguageProvider.php | 39 | ||||
-rw-r--r-- | lib/public/Translation/ITranslationManager.php | 60 | ||||
-rw-r--r-- | lib/public/Translation/ITranslationProvider.php | 50 | ||||
-rw-r--r-- | lib/public/Translation/LanguageTuple.php | 69 |
4 files changed, 218 insertions, 0 deletions
diff --git a/lib/public/Translation/IDetectLanguageProvider.php b/lib/public/Translation/IDetectLanguageProvider.php new file mode 100644 index 00000000000..f6db4f7d9c1 --- /dev/null +++ b/lib/public/Translation/IDetectLanguageProvider.php @@ -0,0 +1,39 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +namespace OCP\Translation; + +/** + * @since 26.0.0 + */ +interface IDetectLanguageProvider { + /** + * Try to detect the language of a given string + * + * @since 26.0.0 + */ + public function detectLanguage(string $text): ?string; +} diff --git a/lib/public/Translation/ITranslationManager.php b/lib/public/Translation/ITranslationManager.php new file mode 100644 index 00000000000..c6b67462152 --- /dev/null +++ b/lib/public/Translation/ITranslationManager.php @@ -0,0 +1,60 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +namespace OCP\Translation; + +use InvalidArgumentException; +use OCP\PreConditionNotMetException; +use RuntimeException; + +/** + * @since 26.0.0 + */ +interface ITranslationManager { + /** + * @since 26.0.0 + */ + public function hasProviders(): bool; + + /** + * @since 26.0.0 + */ + public function canDetectLanguage(): bool; + + /** + * @since 26.0.0 + * @return LanguageTuple[] + */ + public function getLanguages(): array; + + /** + * @since 26.0.0 + * @throws PreConditionNotMetException If no provider was registered but this method was still called + * @throws InvalidArgumentException If no matching provider was found that can detect a language + * @throws RuntimeException If the translation failed for other reasons + */ + public function translate(string $text, ?string $fromLanguage, string $toLanguage): string; +} diff --git a/lib/public/Translation/ITranslationProvider.php b/lib/public/Translation/ITranslationProvider.php new file mode 100644 index 00000000000..ac77ba2230e --- /dev/null +++ b/lib/public/Translation/ITranslationProvider.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2022 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +namespace OCP\Translation; + +use RuntimeException; + +/** + * @since 26.0.0 + */ +interface ITranslationProvider { + /** + * @since 26.0.0 + */ + public function getName(): string; + + /** + * @since 26.0.0 + */ + public function getAvailableLanguages(): array; + + /** + * @since 26.0.0 + * @throws RuntimeException If the text could not be translated + */ + public function translate(?string $fromLanguage, string $toLanguage, string $text): string; +} diff --git a/lib/public/Translation/LanguageTuple.php b/lib/public/Translation/LanguageTuple.php new file mode 100644 index 00000000000..9defb17e4b6 --- /dev/null +++ b/lib/public/Translation/LanguageTuple.php @@ -0,0 +1,69 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net> + * + * @author Julius Härtl <jus@bitgrid.net> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +namespace OCP\Translation; + +use JsonSerializable; + +/** + * @since 26.0.0 + */ +class LanguageTuple implements JsonSerializable { + /** + * @since 26.0.0 + */ + public function __construct( + private string $from, + private string $fromLabel, + private string $to, + private string $toLabel + ) { + } + + /** + * @since 26.0.0 + */ + public function jsonSerialize(): array { + return [ + 'from' => $this->from, + 'fromLabel' => $this->fromLabel, + 'to' => $this->to, + 'toLabel' => $this->toLabel, + ]; + } + + /** + * @since 26.0.0 + */ + public static function fromArray(array $data): LanguageTuple { + return new self( + $data['from'], + $data['fromLabel'], + $data['to'], + $data['toLabel'], + ); + } +} |