TranslationManager.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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\PreConditionNotMetException;
  29. use OCP\Translation\CouldNotTranslateException;
  30. use OCP\Translation\IDetectLanguageProvider;
  31. use OCP\Translation\ITranslationManager;
  32. use OCP\Translation\ITranslationProvider;
  33. use Psr\Container\ContainerExceptionInterface;
  34. use Psr\Container\NotFoundExceptionInterface;
  35. use Psr\Log\LoggerInterface;
  36. use RuntimeException;
  37. use Throwable;
  38. class TranslationManager implements ITranslationManager {
  39. /** @var ?ITranslationProvider[] */
  40. private ?array $providers = null;
  41. public function __construct(
  42. private IServerContainer $serverContainer,
  43. private Coordinator $coordinator,
  44. private LoggerInterface $logger,
  45. private IConfig $config,
  46. ) {
  47. }
  48. public function getLanguages(): array {
  49. $languages = [];
  50. foreach ($this->getProviders() as $provider) {
  51. $languages = array_merge($languages, $provider->getAvailableLanguages());
  52. }
  53. return $languages;
  54. }
  55. public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string {
  56. if (!$this->hasProviders()) {
  57. throw new PreConditionNotMetException('No translation providers available');
  58. }
  59. $providers = $this->getProviders();
  60. $json = $this->config->getAppValue('core', 'ai.translation_provider_preferences', '');
  61. if ($json !== '') {
  62. $precedence = json_decode($json, true);
  63. $newProviders = [];
  64. foreach ($precedence as $className) {
  65. $provider = current(array_filter($providers, fn ($provider) => $provider::class === $className));
  66. if ($provider !== false) {
  67. $newProviders[] = $provider;
  68. }
  69. }
  70. // Add all providers that haven't been added so far
  71. $newProviders += array_udiff($providers, $newProviders, fn ($a, $b) => $a::class > $b::class ? 1 : ($a::class < $b::class ? -1 : 0));
  72. $providers = $newProviders;
  73. }
  74. if ($fromLanguage === null) {
  75. foreach ($providers as $provider) {
  76. if ($provider instanceof IDetectLanguageProvider) {
  77. $fromLanguage = $provider->detectLanguage($text);
  78. }
  79. if ($fromLanguage !== null) {
  80. break;
  81. }
  82. }
  83. if ($fromLanguage === null) {
  84. throw new InvalidArgumentException('Could not detect language');
  85. }
  86. }
  87. if ($fromLanguage === $toLanguage) {
  88. return $text;
  89. }
  90. foreach ($providers as $provider) {
  91. try {
  92. return $provider->translate($fromLanguage, $toLanguage, $text);
  93. } catch (RuntimeException $e) {
  94. $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage} using provider {$provider->getName()}", ['exception' => $e]);
  95. }
  96. }
  97. throw new CouldNotTranslateException($fromLanguage);
  98. }
  99. public function getProviders(): array {
  100. $context = $this->coordinator->getRegistrationContext();
  101. if ($this->providers !== null) {
  102. return $this->providers;
  103. }
  104. $this->providers = [];
  105. foreach ($context->getTranslationProviders() as $providerRegistration) {
  106. $class = $providerRegistration->getService();
  107. try {
  108. $this->providers[$class] = $this->serverContainer->get($class);
  109. } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
  110. $this->logger->error('Failed to load translation provider ' . $class, [
  111. 'exception' => $e
  112. ]);
  113. }
  114. }
  115. return $this->providers;
  116. }
  117. public function hasProviders(): bool {
  118. $context = $this->coordinator->getRegistrationContext();
  119. return !empty($context->getTranslationProviders());
  120. }
  121. public function canDetectLanguage(): bool {
  122. foreach ($this->getProviders() as $provider) {
  123. if ($provider instanceof IDetectLanguageProvider) {
  124. return true;
  125. }
  126. }
  127. return false;
  128. }
  129. }