Passed
Push — master ( 14685e...6cfd53 )
by Julius
15:11 queued 13s
created

TranslationManager::translate()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
nc 8
nop 3
dl 0
loc 22
c 1
b 0
f 0
cc 7
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2023 Julius Härtl <[email protected]>
7
 *
8
 * @author Julius Härtl <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
27
namespace OC\Translation;
28
29
use InvalidArgumentException;
30
use OC\AppFramework\Bootstrap\Coordinator;
31
use OCP\IServerContainer;
32
use OCP\PreConditionNotMetException;
33
use OCP\Translation\IDetectLanguageProvider;
34
use OCP\Translation\ITranslationManager;
35
use OCP\Translation\ITranslationProvider;
36
use Psr\Container\ContainerExceptionInterface;
37
use Psr\Container\NotFoundExceptionInterface;
38
use Psr\Log\LoggerInterface;
39
use RuntimeException;
40
use Throwable;
41
42
class TranslationManager implements ITranslationManager {
43
	/** @var ?ITranslationProvider[] */
44
	private ?array $providers = null;
45
46
	public function __construct(
47
		private IServerContainer $serverContainer,
48
		private Coordinator $coordinator,
49
		private LoggerInterface $logger,
50
	) {
51
	}
52
53
	public function getLanguages(): array {
54
		$languages = [];
55
		foreach ($this->getProviders() as $provider) {
56
			$languages = array_merge($languages, $provider->getAvailableLanguages());
57
		}
58
		return $languages;
59
	}
60
61
	public function translate(string $text, ?string $fromLanguage, string $toLanguage): string {
62
		if (!$this->hasProviders()) {
63
			throw new PreConditionNotMetException('No translation providers available');
64
		}
65
66
		foreach ($this->getProviders() as $provider) {
67
			if ($fromLanguage === null && $provider instanceof IDetectLanguageProvider) {
68
				$fromLanguage = $provider->detectLanguage($text);
69
			}
70
71
			if ($fromLanguage === null) {
72
				throw new InvalidArgumentException('Could not detect language');
73
			}
74
75
			try {
76
				return $provider->translate($fromLanguage, $toLanguage, $text);
77
			} catch (RuntimeException $e) {
78
				$this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]);
79
			}
80
		}
81
82
		throw new RuntimeException('Could not translate text');
83
	}
84
85
	public function getProviders(): array {
86
		$context = $this->coordinator->getRegistrationContext();
87
88
		if ($this->providers !== null) {
89
			return $this->providers;
90
		}
91
92
		$this->providers = [];
93
		foreach ($context->getTranslationProviders() as $providerRegistration) {
94
			$class = $providerRegistration->getService();
95
			try {
96
				$this->providers[$class] = $this->serverContainer->get($class);
97
			} catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) {
98
				$this->logger->error('Failed to load translation provider ' . $class, [
99
					'exception' => $e
100
				]);
101
			}
102
		}
103
104
		return $this->providers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->providers returns the type null which is incompatible with the type-hinted return array.
Loading history...
105
	}
106
107
	public function hasProviders(): bool {
108
		$context = $this->coordinator->getRegistrationContext();
109
		return !empty($context->getTranslationProviders());
110
	}
111
112
	public function canDetectLanguage(): bool {
113
		foreach ($this->getProviders() as $provider) {
114
			if ($provider instanceof IDetectLanguageProvider) {
115
				return true;
116
			}
117
		}
118
		return false;
119
	}
120
}
121