diff options
Diffstat (limited to 'lib/public/Translation')
-rw-r--r-- | lib/public/Translation/CouldNotTranslateException.php | 32 | ||||
-rw-r--r-- | lib/public/Translation/IDetectLanguageProvider.php | 24 | ||||
-rw-r--r-- | lib/public/Translation/ITranslationManager.php | 50 | ||||
-rw-r--r-- | lib/public/Translation/ITranslationProvider.php | 35 | ||||
-rw-r--r-- | lib/public/Translation/ITranslationProviderWithId.php | 22 | ||||
-rw-r--r-- | lib/public/Translation/ITranslationProviderWithUserId.php | 23 | ||||
-rw-r--r-- | lib/public/Translation/LanguageTuple.php | 55 |
7 files changed, 241 insertions, 0 deletions
diff --git a/lib/public/Translation/CouldNotTranslateException.php b/lib/public/Translation/CouldNotTranslateException.php new file mode 100644 index 00000000000..fc9f33c879a --- /dev/null +++ b/lib/public/Translation/CouldNotTranslateException.php @@ -0,0 +1,32 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\Translation; + +/** + * @since 27.0.0 + * @deprecated 30.0.0 + */ +class CouldNotTranslateException extends \RuntimeException { + /** + * @since 27.0.0 + */ + public function __construct( + protected ?string $from, + ) { + parent::__construct(); + } + + /** + * @since 27.0.0 + */ + public function getFrom(): ?string { + return $this->from; + } +} diff --git a/lib/public/Translation/IDetectLanguageProvider.php b/lib/public/Translation/IDetectLanguageProvider.php new file mode 100644 index 00000000000..18f40b1aa52 --- /dev/null +++ b/lib/public/Translation/IDetectLanguageProvider.php @@ -0,0 +1,24 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\Translation; + +/** + * @since 26.0.0 + * @deprecated 30.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..efeaa2de1e2 --- /dev/null +++ b/lib/public/Translation/ITranslationManager.php @@ -0,0 +1,50 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\Translation; + +use InvalidArgumentException; +use OCP\PreConditionNotMetException; + +/** + * @since 26.0.0 + * @deprecated 30.0.0 + */ +interface ITranslationManager { + /** + * @since 26.0.0 + */ + public function hasProviders(): bool; + + /** + * @return ITranslationProvider[] + * @since 27.1.0 + */ + public function getProviders(): array; + + /** + * @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 CouldNotTranslateException 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..39267ab9ca5 --- /dev/null +++ b/lib/public/Translation/ITranslationProvider.php @@ -0,0 +1,35 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\Translation; + +use RuntimeException; + +/** + * @since 26.0.0 + * @deprecated 30.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/ITranslationProviderWithId.php b/lib/public/Translation/ITranslationProviderWithId.php new file mode 100644 index 00000000000..93fc641ee02 --- /dev/null +++ b/lib/public/Translation/ITranslationProviderWithId.php @@ -0,0 +1,22 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\Translation; + +/** + * @since 29.0.0 + * @deprecated 30.0.0 + */ +interface ITranslationProviderWithId extends ITranslationProvider { + /** + * @since 29.0.0 + */ + public function getId(): string; +} diff --git a/lib/public/Translation/ITranslationProviderWithUserId.php b/lib/public/Translation/ITranslationProviderWithUserId.php new file mode 100644 index 00000000000..5bc2255f21f --- /dev/null +++ b/lib/public/Translation/ITranslationProviderWithUserId.php @@ -0,0 +1,23 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\Translation; + +/** + * @since 29.0.0 + * @deprecated 30.0.0 + */ +interface ITranslationProviderWithUserId extends ITranslationProvider { + /** + * @param string|null $userId The userId of the user requesting the current task + * @since 29.0.0 + */ + public function setUserId(?string $userId); +} diff --git a/lib/public/Translation/LanguageTuple.php b/lib/public/Translation/LanguageTuple.php new file mode 100644 index 00000000000..07507ece687 --- /dev/null +++ b/lib/public/Translation/LanguageTuple.php @@ -0,0 +1,55 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + + +namespace OCP\Translation; + +use JsonSerializable; + +/** + * @since 26.0.0 + * @deprecated 30.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 + * @return array{from: string, fromLabel: string, to: string, toLabel: string} + */ + 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'], + ); + } +} |