|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace GpMachineTranslate\Providers; |
| 6 | + |
| 7 | +use WP_Error; |
| 8 | + |
| 9 | +class DeepLProvider extends AbstractProvider |
| 10 | +{ |
| 11 | + public const API_URL = 'https://api-free.deepl.com'; |
| 12 | + |
| 13 | + public const ENDPOINT_GLOSSARIES = '/v2/glossaries'; |
| 14 | + |
| 15 | + public const ENDPOINT_TRANSLATE = '/v2/translate'; |
| 16 | + |
| 17 | + public const IDENTIFIER = 'DeepL'; |
| 18 | + |
| 19 | + protected const LOCALE_MAPPING = [ |
| 20 | + 'ar' => 'ar', |
| 21 | + 'bg' => 'bg', |
| 22 | + 'cs' => 'cs', |
| 23 | + 'da' => 'da', |
| 24 | + 'de' => 'de', |
| 25 | + 'el' => 'el', |
| 26 | + 'en' => 'en_us', |
| 27 | + 'en_gb' => 'en_gb', |
| 28 | + 'es' => 'es', |
| 29 | + 'et' => 'et', |
| 30 | + 'fi' => 'fi', |
| 31 | + 'fr' => 'fr', |
| 32 | + 'hu' => 'hu', |
| 33 | + 'it' => 'it', |
| 34 | + 'id' => 'id', |
| 35 | + 'ja' => 'ja', |
| 36 | + 'ko' => 'ko', |
| 37 | + 'lt' => 'lt', |
| 38 | + 'lv' => 'lv', |
| 39 | + 'nb' => 'nb', |
| 40 | + 'no' => 'nb', |
| 41 | + 'nl' => 'nl', |
| 42 | + 'pl' => 'pl', |
| 43 | + 'pt' => 'pt-pt', |
| 44 | + 'pt_br' => 'pt-br', |
| 45 | + 'ro' => 'ro', |
| 46 | + 'ru' => 'ru', |
| 47 | + 'sk' => 'sk', |
| 48 | + 'sl' => 'sl', |
| 49 | + 'sv' => 'sv', |
| 50 | + 'tr' => 'tr', |
| 51 | + 'uk' => 'uk', |
| 52 | + 'zh_cn' => 'zh', |
| 53 | + ]; |
| 54 | + |
| 55 | + protected const NAME = 'DeepL - Free'; |
| 56 | + |
| 57 | + protected const REQUIRES_AUTH_CLIENT_ID = false; |
| 58 | + |
| 59 | + protected const REQUIRES_AUTH_KEY = true; |
| 60 | + |
| 61 | + private const SPECIAL_CHARACTERS = [ |
| 62 | + 'original' => [ |
| 63 | + ' & ', |
| 64 | + '»', |
| 65 | + '»', |
| 66 | + ], |
| 67 | + 'replacement' => [ |
| 68 | + ' <mask-amp> ', |
| 69 | + '<mask-raquo>', |
| 70 | + '<mask-raquo>', |
| 71 | + ], |
| 72 | + ]; |
| 73 | + |
| 74 | + /** |
| 75 | + * @return array|WP_Error |
| 76 | + */ |
| 77 | + public function batchTranslate(string $locale, array $strings) |
| 78 | + { |
| 79 | + $isValid = $this->validateTranslationArguments($locale, $strings); |
| 80 | + |
| 81 | + if ($isValid !== null) { |
| 82 | + return $isValid; |
| 83 | + } |
| 84 | + |
| 85 | + $translationData = $this->performTranslationRequest( |
| 86 | + $this->getRequestBody($strings, $locale), |
| 87 | + ); |
| 88 | + |
| 89 | + if ($translationData instanceof WP_Error) { |
| 90 | + return $translationData; |
| 91 | + } |
| 92 | + |
| 93 | + return $this->getTranslatedStringsArray($translationData, $strings); |
| 94 | + } |
| 95 | + |
| 96 | + public function validateTranslationArguments(string $locale, array $strings): ?WP_Error |
| 97 | + { |
| 98 | + $isValid = parent::validateTranslationArguments($locale, $strings); |
| 99 | + |
| 100 | + if ($isValid !== null) { |
| 101 | + return $isValid; |
| 102 | + } |
| 103 | + |
| 104 | + // If we have too many strings, throw an error. |
| 105 | + if (count($strings) > 50) { |
| 106 | + return new WP_Error('gp_machine_translate', 'Only 50 strings allowed.'); |
| 107 | + } |
| 108 | + |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + protected function escapeSpecialCharacters(string $text): string |
| 113 | + { |
| 114 | + return str_replace(self::SPECIAL_CHARACTERS['original'], self::SPECIAL_CHARACTERS['replacement'], $text); |
| 115 | + } |
| 116 | + |
| 117 | + protected function getRequestBody(array $strings, string $locale, ?string $glossaryId = null): array |
| 118 | + { |
| 119 | + $requestBody = [ |
| 120 | + 'source_lang' => 'en', |
| 121 | + 'target_lang' => $this->getLocales()[$locale], |
| 122 | + 'tag_handling' => 'xml', |
| 123 | + 'text' => [], |
| 124 | + ]; |
| 125 | + |
| 126 | + if ($glossaryId) { |
| 127 | + $requestBody['glossary_id'] = $glossaryId; |
| 128 | + } |
| 129 | + |
| 130 | + foreach ($strings as $string) { |
| 131 | + $requestBody['text'][] = $this->escapeSpecialCharacters($string); |
| 132 | + } |
| 133 | + |
| 134 | + return $requestBody; |
| 135 | + } |
| 136 | + |
| 137 | + protected function getTranslatedStringsArray(object $deepLTranslationData, array $strings) |
| 138 | + { |
| 139 | + // Setup an temporary array to use to process the response. |
| 140 | + $translations = []; |
| 141 | + $translatedStrings = array_column($deepLTranslationData->translations, 'text'); |
| 142 | + |
| 143 | + // Merge the originals and translations arrays. |
| 144 | + $items = gp_array_zip($strings, $translatedStrings); |
| 145 | + |
| 146 | + // If there are no items, throw an error. |
| 147 | + if (!$items) { |
| 148 | + return new WP_Error('gp_machine_translate', 'Error merging arrays'); |
| 149 | + } |
| 150 | + |
| 151 | + // Loop through the items and clean up the responses. |
| 152 | + foreach ($items as $item) { |
| 153 | + list($string, $translation) = $item; |
| 154 | + |
| 155 | + $translations[] = $this->unescapeSpecialCharacters( |
| 156 | + $this->normalizePlaceholders($translation), |
| 157 | + ); |
| 158 | + } |
| 159 | + |
| 160 | + return $translations; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * @return object|WP_Error |
| 165 | + */ |
| 166 | + protected function performTranslationRequest(array $requestBody) |
| 167 | + { |
| 168 | + $response = wp_remote_post( |
| 169 | + static::API_URL . self::ENDPOINT_TRANSLATE, |
| 170 | + [ |
| 171 | + 'method' => 'POST', |
| 172 | + 'headers' => [ |
| 173 | + 'Authorization' => 'DeepL-Auth-Key ' . $this->authKey, |
| 174 | + 'Content-Type' => 'application/json', |
| 175 | + ], |
| 176 | + 'body' => json_encode($requestBody), |
| 177 | + ], |
| 178 | + ); |
| 179 | + |
| 180 | + // Did we get an error? |
| 181 | + if (is_wp_error($response)) { |
| 182 | + return $response; |
| 183 | + } |
| 184 | + |
| 185 | + // Decode the response from DeepL. |
| 186 | + $json = json_decode( |
| 187 | + wp_remote_retrieve_body($response), |
| 188 | + ); |
| 189 | + |
| 190 | + // If something went wrong with the response from DeepL, throw an error. |
| 191 | + if (!$json || !isset($json->translations)) { |
| 192 | + return new WP_Error('gp_machine_translate', 'Error decoding JSON from DeepL Translate.'); |
| 193 | + } |
| 194 | + |
| 195 | + if (isset($json->error)) { |
| 196 | + return new WP_Error('gp_machine_translate', sprintf('Error auto-translating: %1$s', $json->error->errors[0]->message)); |
| 197 | + } |
| 198 | + |
| 199 | + return $json; |
| 200 | + } |
| 201 | + |
| 202 | + protected function unescapeSpecialCharacters(string $text): string |
| 203 | + { |
| 204 | + return str_replace(self::SPECIAL_CHARACTERS['replacement'], self::SPECIAL_CHARACTERS['original'], $text); |
| 205 | + } |
| 206 | +} |
0 commit comments