|
30 | 30 | use OC\AppFramework\Bootstrap\Coordinator; |
31 | 31 | use OCP\IServerContainer; |
32 | 32 | use OCP\PreConditionNotMetException; |
| 33 | +use OCP\Translation\CouldNotTranslateException; |
33 | 34 | use OCP\Translation\IDetectLanguageProvider; |
34 | 35 | use OCP\Translation\ITranslationManager; |
35 | 36 | use OCP\Translation\ITranslationProvider; |
@@ -58,28 +59,36 @@ public function getLanguages(): array { |
58 | 59 | return $languages; |
59 | 60 | } |
60 | 61 |
|
61 | | - public function translate(string $text, ?string $fromLanguage, string $toLanguage): string { |
| 62 | + public function translate(string $text, ?string &$fromLanguage, string $toLanguage): string { |
62 | 63 | if (!$this->hasProviders()) { |
63 | 64 | throw new PreConditionNotMetException('No translation providers available'); |
64 | 65 | } |
65 | 66 |
|
66 | | - foreach ($this->getProviders() as $provider) { |
67 | | - if ($fromLanguage === null && $provider instanceof IDetectLanguageProvider) { |
68 | | - $fromLanguage = $provider->detectLanguage($text); |
| 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 | + } |
69 | 76 | } |
70 | 77 |
|
71 | 78 | if ($fromLanguage === null) { |
72 | 79 | throw new InvalidArgumentException('Could not detect language'); |
73 | 80 | } |
| 81 | + } |
74 | 82 |
|
| 83 | + foreach ($this->getProviders() as $provider) { |
75 | 84 | try { |
76 | 85 | return $provider->translate($fromLanguage, $toLanguage, $text); |
77 | 86 | } catch (RuntimeException $e) { |
78 | 87 | $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage}", ['exception' => $e]); |
79 | 88 | } |
80 | 89 | } |
81 | 90 |
|
82 | | - throw new RuntimeException('Could not translate text'); |
| 91 | + throw new CouldNotTranslateException($fromLanguage); |
83 | 92 | } |
84 | 93 |
|
85 | 94 | public function getProviders(): array { |
|
0 commit comments