-
Notifications
You must be signed in to change notification settings - Fork 26
/
MicrosoftTranslator.php
200 lines (168 loc) · 5.29 KB
/
MicrosoftTranslator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace MatthiasNoback\MicrosoftTranslator;
use MatthiasNoback\MicrosoftOAuth\AccessTokenProviderInterface;
use MatthiasNoback\MicrosoftTranslator\ApiCall;
use Buzz\Browser;
class MicrosoftTranslator
{
const ACCESS_TOKEN_SCOPE = 'http://api.microsofttranslator.com';
const ACCESS_TOKEN_GRANT_TYPE = 'client_credentials';
/**
* @var \Buzz\Browser
*/
private $browser;
/**
* @var \MatthiasNoback\MicrosoftOAuth\AccessTokenProviderInterface
*/
private $accessTokenProvider;
public function __construct(Browser $browser, AccessTokenProviderInterface $accessTokenProvider)
{
$this->browser = $browser;
$this->accessTokenProvider = $accessTokenProvider;
}
/**
* Translates a given text to the given language
*
* The language of the given text is optional, and will be auto-detected
* The category will default to "general"
*
* @param string $text
* @param string $to
* @param string|null $from
* @param string|null $category
* @return string
*/
public function translate($text, $to, $from = null, $category = null)
{
$apiCall = new ApiCall\Translate($text, $to, $from, $category);
return $this->call($apiCall);
}
/**
* Translates an array of texts
*
* @see MicrosoftTranslator::translate()
*
* @param array $texts
* @param string $to
* @param string|null $from
* @return array An array of translated strings
*/
public function translateArray(array $texts, $to, $from = null)
{
$apiCall = new ApiCall\TranslateArray($texts, $to, $from);
return $this->call($apiCall);
}
/**
* Detects the language of a given text
*
* @param string $text
* @return string The language code
*/
public function detect($text)
{
$apiCall = new ApiCall\Detect($text);
return $this->call($apiCall);
}
/**
* Detect the languages of multiple texts at once
*
* @param array $texts
* @return array An array of language codes
*/
public function detectArray(array $texts)
{
$apiCall = new ApiCall\DetectArray($texts);
return $this->call($apiCall);
}
/**
* Break a given text into the sentences it contains
*
* @param string $text
* @param string $language
* @return array An array of strings
*/
public function breakSentences($text, $language)
{
$apiCall = new ApiCall\BreakSentences($text, $language);
return $this->call($apiCall);
}
/**
* Get a spoken version of the given text (in WAV or MP3 format)
*
* @param $text
* @param string $language
* @param string|null $format Either audio/wav or audio/mp3
* @param string|null $options Either MaxQuality or MinSize
* @return string Raw data for either an MP3 or a WAV file
*/
public function speak($text, $language, $format = null, $options = null)
{
$apiCall = new ApiCall\Speak($text, $language, $format, $options);
return $this->call($apiCall);
}
/**
* Get a list of available language codes for the Speak call
*
* @see MicrosoftTranslator::speak()
*
* @return array An array of language codes
*/
public function getLanguagesForSpeak()
{
$apiCall = new ApiCall\GetLanguagesForSpeak();
return $this->call($apiCall);
}
/**
* Get a list of available language codes for the Translate calls
*
* @see MicrosoftTranslator::translate()
*
* @return array An array of language codes
*/
public function getLanguagesForTranslate()
{
$apiCall = new ApiCall\GetLanguagesForTranslate();
return $this->call($apiCall);
}
/**
* Get a list of language names for the given language codes readable for the given locale
*
* @param array $languageCodes
* @param string $locale
* @return array An array of language names
*/
public function getLanguageNames(array $languageCodes, $locale)
{
$apiCall = new ApiCall\GetLanguageNames($languageCodes, $locale);
return $this->call($apiCall);
}
/**
* @param \MatthiasNoback\MicrosoftTranslator\ApiCall\ApiCallInterface $apiCall
* @return mixed
*/
private function call(ApiCall\ApiCallInterface $apiCall)
{
$url = $apiCall->getUrl();
$method = $apiCall->getHttpMethod();
$headers = array(
'Authorization: Bearer '.$this->getAccessToken(),
'Content-Type: text/xml',
);
$content = $apiCall->getRequestContent();
$response = $this->browser->call($url, $method, $headers, $content);
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf(
'API call was not successful, %d: %s',
$response->getStatusCode(),
$response->getReasonPhrase()
));
}
/* @var $response \Buzz\Message\Response */
$responseContent = $response->getContent();
return $apiCall->parseResponse($responseContent);
}
private function getAccessToken()
{
return $this->accessTokenProvider->getAccessToken(self::ACCESS_TOKEN_SCOPE, self::ACCESS_TOKEN_GRANT_TYPE);
}
}