@@ -41,93 +41,93 @@ |
||
41 | 41 | use Throwable; |
42 | 42 | |
43 | 43 | class TranslationManager implements ITranslationManager { |
44 | - /** @var ?ITranslationProvider[] */ |
|
45 | - private ?array $providers = null; |
|
46 | - |
|
47 | - public function __construct( |
|
48 | - private IServerContainer $serverContainer, |
|
49 | - private Coordinator $coordinator, |
|
50 | - private LoggerInterface $logger, |
|
51 | - ) { |
|
52 | - } |
|
53 | - |
|
54 | - public function getLanguages(): array { |
|
55 | - $languages = []; |
|
56 | - foreach ($this->getProviders() as $provider) { |
|
57 | - $languages = array_merge($languages, $provider->getAvailableLanguages()); |
|
58 | - } |
|
59 | - return $languages; |
|
60 | - } |
|
61 | - |
|
62 | - public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string { |
|
63 | - if (!$this->hasProviders()) { |
|
64 | - throw new PreConditionNotMetException('No translation providers available'); |
|
65 | - } |
|
66 | - |
|
67 | - if ($fromLanguage === null) { |
|
68 | - foreach ($this->getProviders() as $provider) { |
|
69 | - if ($provider instanceof IDetectLanguageProvider) { |
|
70 | - $fromLanguage = $provider->detectLanguage($text); |
|
71 | - } |
|
72 | - |
|
73 | - if ($fromLanguage !== null) { |
|
74 | - break; |
|
75 | - } |
|
76 | - } |
|
77 | - |
|
78 | - if ($fromLanguage === null) { |
|
79 | - throw new InvalidArgumentException('Could not detect language'); |
|
80 | - } |
|
81 | - } |
|
82 | - |
|
83 | - if ($fromLanguage === $toLanguage) { |
|
84 | - return $text; |
|
85 | - } |
|
86 | - |
|
87 | - foreach ($this->getProviders() as $provider) { |
|
88 | - try { |
|
89 | - return $provider->translate($fromLanguage, $toLanguage, $text); |
|
90 | - } catch (RuntimeException $e) { |
|
91 | - $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - throw new CouldNotTranslateException($fromLanguage); |
|
96 | - } |
|
97 | - |
|
98 | - public function getProviders(): array { |
|
99 | - $context = $this->coordinator->getRegistrationContext(); |
|
100 | - |
|
101 | - if ($this->providers !== null) { |
|
102 | - return $this->providers; |
|
103 | - } |
|
104 | - |
|
105 | - $this->providers = []; |
|
106 | - foreach ($context->getTranslationProviders() as $providerRegistration) { |
|
107 | - $class = $providerRegistration->getService(); |
|
108 | - try { |
|
109 | - $this->providers[$class] = $this->serverContainer->get($class); |
|
110 | - } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) { |
|
111 | - $this->logger->error('Failed to load translation provider ' . $class, [ |
|
112 | - 'exception' => $e |
|
113 | - ]); |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - return $this->providers; |
|
118 | - } |
|
119 | - |
|
120 | - public function hasProviders(): bool { |
|
121 | - $context = $this->coordinator->getRegistrationContext(); |
|
122 | - return !empty($context->getTranslationProviders()); |
|
123 | - } |
|
124 | - |
|
125 | - public function canDetectLanguage(): bool { |
|
126 | - foreach ($this->getProviders() as $provider) { |
|
127 | - if ($provider instanceof IDetectLanguageProvider) { |
|
128 | - return true; |
|
129 | - } |
|
130 | - } |
|
131 | - return false; |
|
132 | - } |
|
44 | + /** @var ?ITranslationProvider[] */ |
|
45 | + private ?array $providers = null; |
|
46 | + |
|
47 | + public function __construct( |
|
48 | + private IServerContainer $serverContainer, |
|
49 | + private Coordinator $coordinator, |
|
50 | + private LoggerInterface $logger, |
|
51 | + ) { |
|
52 | + } |
|
53 | + |
|
54 | + public function getLanguages(): array { |
|
55 | + $languages = []; |
|
56 | + foreach ($this->getProviders() as $provider) { |
|
57 | + $languages = array_merge($languages, $provider->getAvailableLanguages()); |
|
58 | + } |
|
59 | + return $languages; |
|
60 | + } |
|
61 | + |
|
62 | + public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string { |
|
63 | + if (!$this->hasProviders()) { |
|
64 | + throw new PreConditionNotMetException('No translation providers available'); |
|
65 | + } |
|
66 | + |
|
67 | + if ($fromLanguage === null) { |
|
68 | + foreach ($this->getProviders() as $provider) { |
|
69 | + if ($provider instanceof IDetectLanguageProvider) { |
|
70 | + $fromLanguage = $provider->detectLanguage($text); |
|
71 | + } |
|
72 | + |
|
73 | + if ($fromLanguage !== null) { |
|
74 | + break; |
|
75 | + } |
|
76 | + } |
|
77 | + |
|
78 | + if ($fromLanguage === null) { |
|
79 | + throw new InvalidArgumentException('Could not detect language'); |
|
80 | + } |
|
81 | + } |
|
82 | + |
|
83 | + if ($fromLanguage === $toLanguage) { |
|
84 | + return $text; |
|
85 | + } |
|
86 | + |
|
87 | + foreach ($this->getProviders() as $provider) { |
|
88 | + try { |
|
89 | + return $provider->translate($fromLanguage, $toLanguage, $text); |
|
90 | + } catch (RuntimeException $e) { |
|
91 | + $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + throw new CouldNotTranslateException($fromLanguage); |
|
96 | + } |
|
97 | + |
|
98 | + public function getProviders(): array { |
|
99 | + $context = $this->coordinator->getRegistrationContext(); |
|
100 | + |
|
101 | + if ($this->providers !== null) { |
|
102 | + return $this->providers; |
|
103 | + } |
|
104 | + |
|
105 | + $this->providers = []; |
|
106 | + foreach ($context->getTranslationProviders() as $providerRegistration) { |
|
107 | + $class = $providerRegistration->getService(); |
|
108 | + try { |
|
109 | + $this->providers[$class] = $this->serverContainer->get($class); |
|
110 | + } catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable $e) { |
|
111 | + $this->logger->error('Failed to load translation provider ' . $class, [ |
|
112 | + 'exception' => $e |
|
113 | + ]); |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + return $this->providers; |
|
118 | + } |
|
119 | + |
|
120 | + public function hasProviders(): bool { |
|
121 | + $context = $this->coordinator->getRegistrationContext(); |
|
122 | + return !empty($context->getTranslationProviders()); |
|
123 | + } |
|
124 | + |
|
125 | + public function canDetectLanguage(): bool { |
|
126 | + foreach ($this->getProviders() as $provider) { |
|
127 | + if ($provider instanceof IDetectLanguageProvider) { |
|
128 | + return true; |
|
129 | + } |
|
130 | + } |
|
131 | + return false; |
|
132 | + } |
|
133 | 133 | } |