| 32 | | class GoogleTranslationProvider extends AbstractTranslationProvider |
| 33 | | { |
| 34 | | private const string API_URL = 'https://translation.googleapis.com/language/translate/v2'; |
| 35 | | |
| 36 | | |
| 37 | | |
| 38 | | |
| 39 | | public function getProviderName(): string |
| 40 | | { |
| 41 | | return 'Google Cloud Translation'; |
| 42 | | } |
| 43 | | |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | protected function doTranslate(string $text, string $sourceLang, string $targetLang): string |
| 48 | | { |
| 49 | | $apiKey = $this->configuration->get('translation.googleApiKey'); |
| 50 | | |
| 51 | | if ((string) $apiKey === '') { |
| 52 | | throw new ApiException('Google Cloud Translation API key not configured'); |
| 53 | | } |
| 54 | | |
| 55 | | try { |
| 56 | | $response = $this->httpClient->request('POST', self::API_URL, [ |
| 57 | | 'query' => ['key' => $apiKey], |
| 58 | | 'json' => [ |
| 59 | | 'q' => $text, |
| 60 | | 'source' => $this->mapLanguageCode($sourceLang), |
| 61 | | 'target' => $this->mapLanguageCode($targetLang), |
| 62 | | 'format' => 'text', |
| 63 | | ], |
| 64 | | ]); |
| 65 | | |
| 66 | | $data = $response->toArray(); |
| 67 | | $payload = $data['data'] ?? []; |
| 68 | | $translations = is_array($payload) ? $payload['translations'] ?? [] : []; |
| 69 | | $firstTranslation = is_array($translations) ? $translations[0] ?? [] : []; |
| 70 | | |
| 71 | | return is_array($firstTranslation) ? (string) ($firstTranslation['translatedText'] ?? '') : ''; |
| 72 | | } catch (Exception|TransportExceptionInterface $e) { |
| 73 | | throw new ApiException('Google Translation API error: ' . $e->getMessage()); |
| 74 | | } |
| 75 | | } |
| 76 | | |
| 77 | | |
| 78 | | |
| 79 | | |
| 80 | | protected function doTranslateBatch(array $texts, string $sourceLang, string $targetLang): array |
| 81 | | { |
| 82 | | $apiKey = $this->configuration->get('translation.googleApiKey'); |
| 83 | | |
| 84 | | if ((string) $apiKey === '') { |
| 85 | | throw new ApiException('Google Cloud Translation API key not configured'); |
| 86 | | } |
| 87 | | |
| 88 | | try { |
| 89 | | $response = $this->httpClient->request('POST', self::API_URL, [ |
| 90 | | 'query' => ['key' => $apiKey], |
| 91 | | 'json' => [ |
| 92 | | 'q' => $texts, |
| 93 | | 'source' => $this->mapLanguageCode($sourceLang), |
| 94 | | 'target' => $this->mapLanguageCode($targetLang), |
| 95 | | 'format' => 'text', |
| 96 | | ], |
| 97 | | ]); |
| 98 | | |
| 99 | | $data = $response->toArray(); |
| 100 | | $payload = $data['data'] ?? []; |
| 101 | | $translations = is_array($payload) ? $payload['translations'] ?? [] : []; |
| 102 | | |
| 103 | | return array_map( |
| 104 | | static fn(mixed $translation): string => is_array($translation) |
| 105 | | ? (string) ($translation['translatedText'] ?? '') |
| 106 | | : '', |
| 107 | | is_array($translations) ? array_values($translations) : [], |
| 108 | | ); |
| 109 | | } catch (Exception|TransportExceptionInterface $e) { |
| 110 | | throw new ApiException('Google Translation API error: ' . $e->getMessage()); |
| 111 | | } |
| 112 | | } |
| 113 | | |
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | public function translateBatch( |
| 118 | | array $texts, |
| 119 | | string $sourceLang, |
| 120 | | string $targetLang, |
| 121 | | bool $preserveHtml = false, |
| 122 | | ): array { |
| 123 | | if ($preserveHtml) { |
| 124 | | |
| 125 | | return array_map(fn($text) => $this->translate($text, $sourceLang, $targetLang, true), $texts); |
| 126 | | } |
| 127 | | |
| 128 | | return $this->doTranslateBatch($texts, $sourceLang, $targetLang); |
| 129 | | } |
| 130 | | |
| 131 | | |
| 132 | | |
| 133 | | |
| 134 | | public function supportsLanguagePair(string $sourceLang, string $targetLang): bool |
| 135 | | { |
| 136 | | |
| 137 | | return true; |
| 138 | | } |
| 139 | | |
| 140 | | |
| 141 | | |
| 142 | | |
| 143 | | public function getSupportedLanguages(): array |
| 144 | | { |
| 145 | | |
| 146 | | return [ |
| 147 | | 'ar', |
| 148 | | 'bn', |
| 149 | | 'bs', |
| 150 | | 'cs', |
| 151 | | 'cy', |
| 152 | | 'da', |
| 153 | | 'de', |
| 154 | | 'el', |
| 155 | | 'en', |
| 156 | | 'es', |
| 157 | | 'eu', |
| 158 | | 'fa', |
| 159 | | 'fi', |
| 160 | | 'fr', |
| 161 | | 'he', |
| 162 | | 'hi', |
| 163 | | 'hu', |
| 164 | | 'id', |
| 165 | | 'it', |
| 166 | | 'ja', |
| 167 | | 'ko', |
| 168 | | 'lt', |
| 169 | | 'lv', |
| 170 | | 'mn', |
| 171 | | 'ms', |
| 172 | | 'nb', |
| 173 | | 'nl', |
| 174 | | 'pl', |
| 175 | | 'pt', |
| 176 | | 'ro', |
| 177 | | 'ru', |
| 178 | | 'sk', |
| 179 | | 'sl', |
| 180 | | 'sr', |
| 181 | | 'sv', |
| 182 | | 'th', |
| 183 | | 'tr', |
| 184 | | 'uk', |
| 185 | | 'ur', |
| 186 | | 'vi', |
| 187 | | 'zh', |
| 188 | | ]; |
| 189 | | } |
| 190 | | |
| 191 | | |
| 192 | | |
| 193 | | |
| 194 | | protected function mapLanguageCode(string $pmfLangCode): string |
| 195 | | { |
| 196 | | |
| 197 | | return match ($pmfLangCode) { |
| 198 | | 'zh' => 'zh-CN', |
| 199 | | 'pt' => 'pt-BR', |
| 200 | | 'nb' => 'no', |
| 201 | | default => $pmfLangCode, |
| 202 | | }; |
| 203 | | } |
| 204 | | } |