| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\AIProviders\Azure; |
| 4 |
|
| 5 |
use AATXT\App\AIProviders\AITranslatorInterface; |
| 6 |
use AATXT\App\Configuration\AzureConfig; |
| 7 |
use AATXT\App\Exceptions\Azure\AzureTranslateInstanceException; |
| 8 |
use AATXT\App\Infrastructure\Http\HttpClientInterface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Azure Translator provider for translating text. |
| 12 |
* |
| 13 |
* This class uses dependency injection to receive HTTP client and configuration, |
| 14 |
* removing static dependencies on PluginOptions. |
| 15 |
*/ |
| 16 |
class AzureTranslator implements AITranslatorInterface |
| 17 |
{ |
| 18 |
private HttpClientInterface $httpClient; |
| 19 |
private AzureConfig $config; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor. |
| 23 |
* |
| 24 |
* @param HttpClientInterface $httpClient HTTP client for API calls |
| 25 |
* @param AzureConfig $config Azure configuration with API keys and endpoints |
| 26 |
*/ |
| 27 |
public function __construct(HttpClientInterface $httpClient, AzureConfig $config) |
| 28 |
{ |
| 29 |
$this->httpClient = $httpClient; |
| 30 |
$this->config = $config; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Translate a string sending a request to the Azure translation Api |
| 35 |
* @param string $text |
| 36 |
* @param string $language |
| 37 |
* @return string |
| 38 |
* @throws AzureTranslateInstanceException |
| 39 |
*/ |
| 40 |
public function translate(string $text, string $language): string |
| 41 |
{ |
| 42 |
$apiKey = $this->config->getTranslationApiKey(); |
| 43 |
$region = $this->config->getRegion(); |
| 44 |
$endpoint = $this->config->getTranslationEndpoint(); |
| 45 |
|
| 46 |
if (empty($apiKey) || empty($region) || empty($endpoint)) { |
| 47 |
return $text; |
| 48 |
} |
| 49 |
|
| 50 |
$route = "translate?api-version=3.0&from=en&to=" . $language; |
| 51 |
$url = $endpoint . $route; |
| 52 |
|
| 53 |
$headers = [ |
| 54 |
'Content-type' => 'application/json', |
| 55 |
'Ocp-Apim-Subscription-Key' => $apiKey, |
| 56 |
'Ocp-Apim-Subscription-Region' => $region, |
| 57 |
]; |
| 58 |
|
| 59 |
$payload = [ |
| 60 |
[ |
| 61 |
'Text' => $text |
| 62 |
] |
| 63 |
]; |
| 64 |
|
| 65 |
try { |
| 66 |
$bodyResult = $this->httpClient->post($url, $headers, $payload); |
| 67 |
} catch (\Exception $e) { |
| 68 |
throw new AzureTranslateInstanceException('HTTP request failed: ' . $e->getMessage()); |
| 69 |
} |
| 70 |
|
| 71 |
if (array_key_exists('error', $bodyResult)) { |
| 72 |
throw new AzureTranslateInstanceException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']); |
| 73 |
} |
| 74 |
|
| 75 |
return $bodyResult[0]['translations'][0]['text']; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the list of supported languages from Azure Api |
| 80 |
* @return array |
| 81 |
* @throws AzureTranslateInstanceException |
| 82 |
*/ |
| 83 |
public function supportedLanguages(): array |
| 84 |
{ |
| 85 |
$apiKey = $this->config->getTranslationApiKey(); |
| 86 |
if (empty($apiKey)) { |
| 87 |
return []; |
| 88 |
} |
| 89 |
$endpoint = $this->config->getTranslationEndpoint(); |
| 90 |
if (empty($endpoint)) { |
| 91 |
return []; |
| 92 |
} |
| 93 |
|
| 94 |
$route = 'languages?api-version=3.0'; |
| 95 |
$url = $endpoint . $route; |
| 96 |
|
| 97 |
$headers = [ |
| 98 |
'Content-type' => 'application/json', |
| 99 |
'Ocp-Apim-Subscription-Key' => $apiKey |
| 100 |
]; |
| 101 |
|
| 102 |
try { |
| 103 |
$bodyResult = $this->httpClient->get($url, $headers); |
| 104 |
} catch (\Exception $e) { |
| 105 |
throw new AzureTranslateInstanceException( |
| 106 |
esc_html__('No language retrieved: maybe the translation endpoint is wrong. Please check it out and try again.', 'auto-alt-text') |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
if (empty($bodyResult)) { |
| 111 |
throw new AzureTranslateInstanceException( |
| 112 |
esc_html__('No language retrieved: maybe the translation endpoint is wrong. Please check it out and try again.', 'auto-alt-text') |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
if (array_key_exists('error', $bodyResult)) { |
| 117 |
throw new AzureTranslateInstanceException("Error code: " . $bodyResult['error']['code'] . " - " . $bodyResult['error']['message']); |
| 118 |
} |
| 119 |
|
| 120 |
return $bodyResult['translation']; |
| 121 |
} |
| 122 |
} |