| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\AIProviders\Azure; |
| 4 |
|
| 5 |
use AATXT\App\Admin\PluginOptions; |
| 6 |
use AATXT\App\AIProviders\AITranslatorInterface; |
| 7 |
use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException; |
| 8 |
|
| 9 |
class AzureTranslator implements AITranslatorInterface |
| 10 |
{ |
| 11 |
|
| 12 |
private function __construct() |
| 13 |
{ |
| 14 |
} |
| 15 |
|
| 16 |
public static function make(): AzureTranslator |
| 17 |
{ |
| 18 |
return new self(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Translate a string sending a request to the Azure translation Api |
| 23 |
* @param string $text |
| 24 |
* @param string $language |
| 25 |
* @return string |
| 26 |
* @throws AzureTranslateInstanceException |
| 27 |
*/ |
| 28 |
public function translate(string $text, string $language): string |
| 29 |
{ |
| 30 |
$apiKey = PluginOptions::apiKeyAzureTranslateInstance(); |
| 31 |
$region = PluginOptions::regionAzureTranslateInstance(); |
| 32 |
$endpoint = PluginOptions::endpointAzureTranslateInstance(); |
| 33 |
|
| 34 |
if (empty($apiKey) || empty($region) || empty($endpoint)) { |
| 35 |
return $text; |
| 36 |
} |
| 37 |
|
| 38 |
$route = "translate?api-version=3.0&from=en&to=" . $language; |
| 39 |
|
| 40 |
$response = wp_remote_post( |
| 41 |
$endpoint . $route, |
| 42 |
[ |
| 43 |
'headers' => [ |
| 44 |
'Content-type' => 'application/json', |
| 45 |
'Ocp-Apim-Subscription-Key' => $apiKey, |
| 46 |
'Ocp-Apim-Subscription-Region' => $region, |
| 47 |
], |
| 48 |
'body' => json_encode([ |
| 49 |
[ |
| 50 |
'Text' => $text |
| 51 |
] |
| 52 |
]), |
| 53 |
'method' => 'POST', |
| 54 |
] |
| 55 |
); |
| 56 |
|
| 57 |
|
| 58 |
//$bodyResult = json_decode(wp_remote_retrieve_body($response), true); |
| 59 |
|
| 60 |
$responseBody = wp_remote_retrieve_body($response); |
| 61 |
if (empty($responseBody)) { |
| 62 |
if (is_object($response) && property_exists($response, 'errors') && array_key_exists('http_request_failed', $response->errors)) { |
| 63 |
throw new AzureTranslateInstanceException("Error: " . $response->errors['http_request_failed'][0]); |
| 64 |
} |
| 65 |
if (is_array($response) && array_key_exists('response', $response)) { |
| 66 |
throw new AzureTranslateInstanceException("Code: " . $response['response']['code'] . " - " . $response['response']['message']); |
| 67 |
} |
| 68 |
throw new AzureTranslateInstanceException("Error: please check if the Azure endpoint in plugin options is right"); |
| 69 |
} |
| 70 |
|
| 71 |
$bodyResult = json_decode($responseBody, true); |
| 72 |
|
| 73 |
if (array_key_exists('error', $bodyResult)) { |
| 74 |
throw new AzureTranslateInstanceException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']); |
| 75 |
} |
| 76 |
|
| 77 |
return $bodyResult[0]['translations'][0]['text']; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the list of supported languages from Azure Api |
| 82 |
* @return array |
| 83 |
* @throws AzureTranslateInstanceException |
| 84 |
*/ |
| 85 |
public function supportedLanguages(): array |
| 86 |
{ |
| 87 |
$apiKey = PluginOptions::apiKeyAzureTranslateInstance(); |
| 88 |
if (empty($apiKey)) { |
| 89 |
return []; |
| 90 |
} |
| 91 |
$endpoint = PluginOptions::endpointAzureTranslateInstance(); |
| 92 |
if (empty($endpoint)) { |
| 93 |
return []; |
| 94 |
} |
| 95 |
|
| 96 |
$route = 'languages?api-version=3.0'; |
| 97 |
|
| 98 |
$url = $endpoint . $route; |
| 99 |
|
| 100 |
$headers = array( |
| 101 |
'Content-type' => 'application/json', |
| 102 |
'Ocp-Apim-Subscription-Key' => $apiKey |
| 103 |
); |
| 104 |
|
| 105 |
$response = wp_remote_get( |
| 106 |
$url, |
| 107 |
array( |
| 108 |
'headers' => $headers |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
$bodyResult = json_decode(wp_remote_retrieve_body($response), true); |
| 113 |
|
| 114 |
if (empty($bodyResult)) { |
| 115 |
throw new AzureTranslateInstanceException(esc_html__('No language retrieved: maybe the translation endpoint is wrong. Please check it out and try again.', 'auto-alt-text')); |
| 116 |
} |
| 117 |
|
| 118 |
if (array_key_exists('error', $bodyResult)) { |
| 119 |
throw new AzureTranslateInstanceException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']); |
| 120 |
} |
| 121 |
|
| 122 |
return $bodyResult['translation']; |
| 123 |
} |
| 124 |
} |