1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* BEdita, API-first content management framework |
6
|
|
|
* Copyright 2023 Atlas Srl, Chialab Srl |
7
|
|
|
* |
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
14
|
|
|
*/ |
15
|
|
|
namespace BEdita\I18n\Microsoft\Core; |
16
|
|
|
|
17
|
|
|
use Cake\Utility\Hash; |
18
|
|
|
|
19
|
|
|
class TranslateClient |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* API endpoint. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected string $endpoint = 'https://api.cognitive.microsofttranslator.com/translate?api-version=3.0'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The headers. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected array $headers = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The options. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected array $options = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor. |
44
|
|
|
* |
45
|
|
|
* @param array $options The options |
46
|
|
|
*/ |
47
|
|
|
public function __construct(array $options) |
48
|
|
|
{ |
49
|
|
|
$this->options = $options; |
50
|
|
|
$this->headers = [ |
51
|
|
|
'Content-type' => 'application/json', |
52
|
|
|
'Ocp-Apim-Subscription-Key' => (string)Hash::get($this->options, 'auth_key'), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Translate a text from a language to another using Microsoft translator API. |
58
|
|
|
* |
59
|
|
|
* @param string $text The text to translate |
60
|
|
|
* @param string $from The source language |
61
|
|
|
* @param string $to The target language |
62
|
|
|
* @return string The translated text |
63
|
|
|
*/ |
64
|
|
|
public function translate(string $text, string $from, string $to): string |
65
|
|
|
{ |
66
|
|
|
$content = json_encode([['Text' => $text]]); |
67
|
|
|
$headers = $this->headers; |
68
|
|
|
$headers['X-ClientTraceId'] = sprintf( |
69
|
|
|
'%04X%04X-%04X-%04X-%04X-%04X%04X%04X', |
70
|
|
|
mt_rand(0, 65535), |
71
|
|
|
mt_rand(0, 65535), |
72
|
|
|
mt_rand(0, 65535), |
73
|
|
|
mt_rand(16384, 20479), |
74
|
|
|
mt_rand(32768, 49151), |
75
|
|
|
mt_rand(0, 65535), |
76
|
|
|
mt_rand(0, 65535), |
77
|
|
|
mt_rand(0, 65535) |
78
|
|
|
); |
79
|
|
|
$headers['Content-length'] = strlen($content); |
80
|
|
|
$options = [ |
81
|
|
|
'http' => [ |
82
|
|
|
'header' => implode("\r\n", array_map( |
83
|
|
|
fn ($key, $value) => sprintf('%s: %s', $key, $value), |
84
|
|
|
array_keys($headers), |
85
|
|
|
array_values($headers) |
86
|
|
|
)), |
87
|
|
|
'method' => 'POST', |
88
|
|
|
'content' => $content, |
89
|
|
|
], |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
return $this->apiCall($from, $to, $options); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Perform api call to obtain translation |
97
|
|
|
* |
98
|
|
|
* @param string $from The source language |
99
|
|
|
* @param string $to The target language |
100
|
|
|
* @param array $options The options |
101
|
|
|
* @return string The translation in json format |
102
|
|
|
*/ |
103
|
|
|
public function apiCall(string $from, string $to, array $options): string |
104
|
|
|
{ |
105
|
|
|
$translation = (string)file_get_contents( |
106
|
|
|
sprintf('%s&from=%s&to=%s', $this->endpoint, $from, $to), |
107
|
|
|
false, |
108
|
|
|
stream_context_create($options) |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
return json_encode(json_decode($translation), JSON_UNESCAPED_UNICODE); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|