| 31 | | class LibreTranslationProvider extends AbstractTranslationProvider |
| 32 | | { |
| 33 | | |
| 34 | | |
| 35 | | |
| 36 | | public function getProviderName(): string |
| 37 | | { |
| 38 | | return 'LibreTranslate'; |
| 39 | | } |
| 40 | | |
| 41 | | |
| 42 | | |
| 43 | | |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | private function getApiUrl(): string |
| 48 | | { |
| 49 | | $baseUrl = (string) $this->configuration->get('translation.libreTranslateUrl'); |
| 50 | | |
| 51 | | if ($baseUrl === '') { |
| 52 | | throw new ApiException('LibreTranslate server URL not configured'); |
| 53 | | } |
| 54 | | |
| 55 | | return rtrim(string: $baseUrl, characters: '/') . '/translate'; |
| 56 | | } |
| 57 | | |
| 58 | | |
| 59 | | |
| 60 | | |
| 61 | | protected function doTranslate(string $text, string $sourceLang, string $targetLang): string |
| 62 | | { |
| 63 | | $apiKey = $this->configuration->get('translation.libreTranslateApiKey'); |
| 64 | | |
| 65 | | $body = [ |
| 66 | | 'q' => $text, |
| 67 | | 'source' => $this->mapLanguageCode($sourceLang), |
| 68 | | 'target' => $this->mapLanguageCode($targetLang), |
| 69 | | 'format' => 'text', |
| 70 | | ]; |
| 71 | | |
| 72 | | if ((string) $apiKey !== '') { |
| 73 | | $body['api_key'] = $apiKey; |
| 74 | | } |
| 75 | | |
| 76 | | try { |
| 77 | | $response = $this->httpClient->request('POST', $this->getApiUrl(), [ |
| 78 | | 'headers' => ['Content-Type' => 'application/json'], |
| 79 | | 'json' => $body, |
| 80 | | ]); |
| 81 | | |
| 82 | | $data = $response->toArray(); |
| 83 | | return (string) ($data['translatedText'] ?? ''); |
| 84 | | } catch (Exception $e) { |
| 85 | | throw new ApiException('LibreTranslate API error: ' . $e->getMessage()); |
| 86 | | } |
| 87 | | } |
| 88 | | |
| 89 | | |
| 90 | | |
| 91 | | |
| 92 | | protected function doTranslateBatch(array $texts, string $sourceLang, string $targetLang): array |
| 93 | | { |
| 94 | | |
| 95 | | return array_map(fn($text) => $this->doTranslate($text, $sourceLang, $targetLang), $texts); |
| 96 | | } |
| 97 | | |
| 98 | | |
| 99 | | |
| 100 | | |
| 101 | | public function translateBatch( |
| 102 | | array $texts, |
| 103 | | string $sourceLang, |
| 104 | | string $targetLang, |
| 105 | | bool $preserveHtml = false, |
| 106 | | ): array { |
| 107 | | if ($preserveHtml) { |
| 108 | | |
| 109 | | return array_map(fn($text) => $this->translate($text, $sourceLang, $targetLang, true), $texts); |
| 110 | | } |
| 111 | | |
| 112 | | return $this->doTranslateBatch($texts, $sourceLang, $targetLang); |
| 113 | | } |
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | | public function supportsLanguagePair(string $sourceLang, string $targetLang): bool |
| 119 | | { |
| 120 | | |
| 121 | | return true; |
| 122 | | } |
| 123 | | |
| 124 | | |
| 125 | | |
| 126 | | |
| 127 | | public function getSupportedLanguages(): array |
| 128 | | { |
| 129 | | |
| 130 | | return ['ar', 'de', 'en', 'es', 'fr', 'it', 'ja', 'nl', 'pl', 'pt', 'ru', 'tr', 'zh']; |
| 131 | | } |
| 132 | | |
| 133 | | |
| 134 | | |
| 135 | | |
| 136 | | protected function mapLanguageCode(string $pmfLangCode): string |
| 137 | | { |
| 138 | | |
| 139 | | return $pmfLangCode; |
| 140 | | } |
| 141 | | } |