TranslationManager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\IServerContainer;
  27. use OCP\PreConditionNotMetException;
  28. use OCP\Translation\CouldNotTranslateException;
  29. use OCP\Translation\IDetectLanguageProvider;
  30. use OCP\Translation\ITranslationManager;
  31. use OCP\Translation\ITranslationProvider;
  32. use Psr\Container\ContainerExceptionInterface;
  33. use Psr\Container\NotFoundExceptionInterface;
  34. use Psr\Log\LoggerInterface;
  35. use RuntimeException;
  36. use Throwable;
  37. class TranslationManager implements ITranslationManager {
  38. /** @var ?ITranslationProvider[] */
  39. private ?array $providers = null;
  40. public function __construct(
  41. private IServerContainer $serverContainer,
  42. private Coordinator $coordinator,
  43. private LoggerInterface $logger,
  44. ) {
  45. }
  46. public function getLanguages(): array {
  47. $languages = [];
  48. foreach ($this->getProviders() as $provider) {
  49. $languages = array_merge($languages, $provider->getAvailableLanguages());
  50. }
  51. return $languages;
  52. }
  53. public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string {
  54. if (!$this->hasProviders()) {
  55. throw new PreConditionNotMetException('No translation providers available');
  56. }
  57. if ($fromLanguage === null) {
  58. foreach ($this->getProviders() as $provider) {
  59. if ($provider instanceof IDetectLanguageProvider) {
  60. $fromLanguage = $provider->detectLanguage($text);
  61. }
  62. if ($fromLanguage !== null) {
  63. break;
  64. }
  65. }
  66. if ($fromLanguage === null) {
  67. throw new InvalidArgumentException('Could not detect language');
  68. }
  69. }
  70. if ($fromLanguage === $toLanguage) {
  71. return $text;
  72. }
  73. foreach ($this->getProviders() as $provider) {
  74. try {
  75. return $provider->translate($fromLanguage, $toLanguage, $text);
  76. } catch (RuntimeException $e) {
  77. $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]);
  78. }
  79. }
  80. throw new CouldNotTranslateException($fromLanguage);
  81. }
  82. public function getProviders(): array {
  83. $context = $this->coordinator->getRegistrationContext();
  84. if ($this->providers !== null) {
  85. return $this->providers;
  86. }
  87. $this->providers = [];
  88. foreach ($context->getTranslationProviders() as $providerRegistration) {
  89. $class = $providerRegistration->getService();
  90. try {
  91. $this->providers[$class] = $this->serverContainer->get($class);
  92. } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
  93. $this->logger->error('Failed to load translation provider ' . $class, [
  94. 'exception' => $e
  95. ]);
  96. }
  97. }
  98. return $this->providers;
  99. }
  100. public function hasProviders(): bool {
  101. $context = $this->coordinator->getRegistrationContext();
  102. return !empty($context->getTranslationProviders());
  103. }
  104. public function canDetectLanguage(): bool {
  105. foreach ($this->getProviders() as $provider) {
  106. if ($provider instanceof IDetectLanguageProvider) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. }