PluginProbe
Auto Alt Text / 2.1.0
Auto Alt Text v2.1.0
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / AIProviders / Azure / AzureTranslator.php

AzureTranslator.php in Auto Alt Text 2.1.0, at src/App/AIProviders/Azure/AzureTranslator.php

124 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }