TranslationManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Julius Härtl <jus@bitgrid.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\Translation;
  24. use InvalidArgumentException;
  25. use OC\AppFramework\Bootstrap\Coordinator;
  26. use OCP\IConfig;
  27. use OCP\IServerContainer;
  28. use OCP\IUserSession;
  29. use OCP\PreConditionNotMetException;
  30. use OCP\Translation\CouldNotTranslateException;
  31. use OCP\Translation\IDetectLanguageProvider;
  32. use OCP\Translation\ITranslationManager;
  33. use OCP\Translation\ITranslationProvider;
  34. use OCP\Translation\ITranslationProviderWithId;
  35. use OCP\Translation\ITranslationProviderWithUserId;
  36. use Psr\Container\ContainerExceptionInterface;
  37. use Psr\Container\NotFoundExceptionInterface;
  38. use Psr\Log\LoggerInterface;
  39. use RuntimeException;
  40. use Throwable;
  41. class TranslationManager implements ITranslationManager {
  42. /** @var ?ITranslationProvider[] */
  43. private ?array $providers = null;
  44. public function __construct(
  45. private IServerContainer $serverContainer,
  46. private Coordinator $coordinator,
  47. private LoggerInterface $logger,
  48. private IConfig $config,
  49. private IUserSession $userSession,
  50. ) {
  51. }
  52. public function getLanguages(): array {
  53. $languages = [];
  54. foreach ($this->getProviders() as $provider) {
  55. $languages = array_merge($languages, $provider->getAvailableLanguages());
  56. }
  57. return $languages;
  58. }
  59. public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string {
  60. if (!$this->hasProviders()) {
  61. throw new PreConditionNotMetException('No translation providers available');
  62. }
  63. $providers = $this->getProviders();
  64. $json = $this->config->getAppValue('core', 'ai.translation_provider_preferences', '');
  65. if ($json !== '') {
  66. $precedence = json_decode($json, true);
  67. $newProviders = [];
  68. foreach ($precedence as $className) {
  69. $provider = current(array_filter($providers, function ($provider) use ($className) {
  70. return $provider instanceof ITranslationProviderWithId ? $provider->getId() === $className : $provider::class === $className;
  71. }));
  72. if ($provider !== false) {
  73. $newProviders[] = $provider;
  74. }
  75. }
  76. // Add all providers that haven't been added so far
  77. $newProviders += array_udiff($providers, $newProviders, function ($a, $b) {
  78. return ($a instanceof ITranslationProviderWithId ? $a->getId() : $a::class) <=> ($b instanceof ITranslationProviderWithId ? $b->getId() : $b::class);
  79. });
  80. $providers = $newProviders;
  81. }
  82. if ($fromLanguage === null) {
  83. foreach ($providers as $provider) {
  84. if ($provider instanceof IDetectLanguageProvider) {
  85. if ($provider instanceof ITranslationProviderWithUserId) {
  86. $provider->setUserId($this->userSession->getUser()?->getUID());
  87. }
  88. $fromLanguage = $provider->detectLanguage($text);
  89. }
  90. if ($fromLanguage !== null) {
  91. break;
  92. }
  93. }
  94. if ($fromLanguage === null) {
  95. throw new InvalidArgumentException('Could not detect language');
  96. }
  97. }
  98. if ($fromLanguage === $toLanguage) {
  99. return $text;
  100. }
  101. foreach ($providers as $provider) {
  102. try {
  103. if ($provider instanceof ITranslationProviderWithUserId) {
  104. $provider->setUserId($this->userSession->getUser()?->getUID());
  105. }
  106. return $provider->translate($fromLanguage, $toLanguage, $text);
  107. } catch (RuntimeException $e) {
  108. $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage} using provider {$provider->getName()}", ['exception' => $e]);
  109. }
  110. }
  111. throw new CouldNotTranslateException($fromLanguage);
  112. }
  113. public function getProviders(): array {
  114. $context = $this->coordinator->getRegistrationContext();
  115. if ($this->providers !== null) {
  116. return $this->providers;
  117. }
  118. $this->providers = [];
  119. foreach ($context->getTranslationProviders() as $providerRegistration) {
  120. $class = $providerRegistration->getService();
  121. try {
  122. $this->providers[$class] = $this->serverContainer->get($class);
  123. } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
  124. $this->logger->error('Failed to load translation provider ' . $class, [
  125. 'exception' => $e
  126. ]);
  127. }
  128. }
  129. return $this->providers;
  130. }
  131. public function hasProviders(): bool {
  132. $context = $this->coordinator->getRegistrationContext();
  133. return !empty($context->getTranslationProviders());
  134. }
  135. public function canDetectLanguage(): bool {
  136. foreach ($this->getProviders() as $provider) {
  137. if ($provider instanceof IDetectLanguageProvider) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. }