This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmgmt_oht.plugin.inc
256 lines (233 loc) · 7.59 KB
/
tmgmt_oht.plugin.inc
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
/**
* @file
* Provides OneHourTranslation translation plugin controller.
*/
/**
* OHT translation plugin controller.
*/
class TMGMTOhtPluginController extends TMGMTDefaultTranslatorPluginController {
/**
* Translation service URL.
*
* @var string
*/
protected $apiURL = 'https://api.onehourtranslation.com/';
protected $sandboxURL = 'https://sandbox.onehourtranslation.com/';
protected $supportedLanguages = array(
'sq' => 'sq-al',
'ar' => 'ar-sa',
'bn' => 'bn-bd',
'bs' => 'bs-ba',
'bg' => 'bg-bg',
'ca' => 'ca-es',
// '' => 'zh-cn-yue',
'zn-hans' => 'zh-cn-cmn-s',
'zn-hant' => 'zh-cn-cmn-t',
'hr' => 'hr-hr',
'cs' => 'cs-cz',
'da' => 'da-dk',
'fa' => 'fa-af',
'nl' => 'nl-nl',
'en-gb' => 'en-uk',
'en' => 'en-us',
'et' => 'et-ee',
// '' => 'fa-ir',
'fi' => 'fi-fi',
'fr' => 'fr-fr',
// '' => 'fr-ca',
'ka' => 'ka-ge',
'de' => 'de-de',
'el' => 'el-gr',
'he' => 'he-il',
'hi' => 'hi-in',
'hu' => 'hu-hu',
'id' => 'id-id',
'it' => 'it-it',
'jp' => 'jp-jp',
'kk' => 'kk-kz',
// '' => 'km-kh',
'ko' => 'ko-kp',
'lo' => 'lo-la',
'lv' => 'lv-lv',
'lt' => 'lt-lt',
'mk' => 'mk-mk',
'ms' => 'ms-my',
'nb' => 'no-no',
'ps' => 'ps',
'pl' => 'pl-pl',
'pt-br' => 'pt-br',
'pt-pt' => 'pt-pt',
'ro' => 'ro-ro',
'ru' => 'ru-ru',
'sr' => 'sr-rs',
'sk' => 'sk-sk',
'sl' => 'sl-si',
'es' => 'es-es',
// '' => 'es-ar',
'sv' => 'sv-se',
'tl' => 'tl-ph',
'ta' => 'ta-in',
'th' => 'th-th',
'tr' => 'tr-tr',
'uk' => 'uk-ua',
'ur' => 'ur',
'uz' => 'uz-uz',
'vi' => 'vi-vn'
);
/**
* Implements TMGMTTranslatorPluginControllerInterface::isAvailable().
*/
public function isAvailable(TMGMTTranslator $translator) {
if ($translator->getSetting('api_account_id') && $translator->getSetting('api_secret_key')) {
return TRUE;
}
return FALSE;
}
/**
* Implements TMGMTTranslatorPluginControllerInterface::canTranslation().
*
* We will check are source and target language supported.
*/
public function canTranslate(TMGMTTranslator $translator, TMGMTJob $job) {
// if we use sandbox we can translate anyway
if($translator->getSetting('use_sandbox')) {
return TRUE;
}
// if Translator is configured - check if the source and target language are supported.
if ($this->isAvailable($translator)) {
$targets = $this->getSupportedLanguages();
return isset($targets[$job->source_language]) && isset($targets[$job->target_language]);
}
return FALSE;
}
/**
* Implements TMGMTTranslatorPluginControllerInterface::requestTranslation().
*
* Here we will acutally query source and get translations.
*/
public function requestTranslation(TMGMTJob $job) {
// Pull the source data array through the job and flatten it.
$data = $this->prepareDataForSending($job);
$translator = $job->getTranslator();
$languages = $this->getSupportedLanguages();
// request translation from OHT service
$response = $this->doRequest($job->getTranslator(), 'api/1/project/new', array(
'source' => $translator->getSetting('use_sandbox') ? 'en-us' : $languages[$job->source_language],
'target' => $translator->getSetting('use_sandbox') ? 'fr-fr' : $languages[$job->target_language],
'source_text' => $data,
'callback_url' => url('tmgmt_oht_callback', array('absolute' => TRUE)),
'custom0' => $job->tjid,
), 'POST');
// handling reponse
$response_data = json_decode($response->data);
if ($response->code == '200' && empty($response_data->status_code)) {
$job->submitted('Job has been successfully submitted for translation. Project ID is: <pre>@project_id</pre>',
array('@project_id' => $response_data->project_id));
$job->addMessage('@credits credits reduced from your account.', array('@credits' => $response_data->credits));
$job->reference = $response_data->project_id;
}
else {
$job->rejected('Could not send Job to OHT. Message error: @error',
array('@error' => $response_data->status_msg), 'error');
}
}
/**
* Receives and stores a translation returned by OHT.
*/
public function receiveTranslation(TMGMTJob $job, $data) {
$data = $this->parseTranslationData($data);
$job->finished('The translation has been received.');
$job->addTranslatedData($data);
}
/**
* Returns list of languages supported by OHT service
* @param TMGMTTranslator $translator
* @param $source_language
* @return array
*/
public function getSupportedTargetLanguages(TMGMTTranslator $translator, $source_language) {
return drupal_map_assoc(array_keys($this->supportedLanguages));
}
/**
* Returns array of supported languages by OHT service
* @return array
* Returns array of the supported languages.
* The key is drupal-style language code, the value - OHT-style language code.
*/
protected function getSupportedLanguages() {
return $this->supportedLanguages;
}
/**
* Execute a request against the OHT API.
*
* @param TMGMTTranslator $translator
* The translator entity to get the settings from.
* @param $path
* The path that should be appended to the base uri, e.g. Translate or
* GetLanguagesForTranslate.
* @param $query
* (Optional) Array of GET query arguments.
* @param $options
* (Optional) Array of additional options passed to drupal_http_request().
*
* @return
* The response object returned by drupal_http_request().
*/
protected function doRequest(TMGMTTranslator $translator, $path, array $query = array(), $method = 'GET') {
$options = array();
if ($custom_url = $translator->getSetting('url')) {
$url = $custom_url;
}
else {
$url = $translator->getSetting('use_sandbox') ? $this->sandboxURL : $this->apiURL;
}
$url .= $path;
// Build query array to pass to OHT service
$query = array(
'account_id' => $translator->getSetting('api_account_id'),
'secret_key' => $translator->getSetting('api_secret_key'),
) + $query;
// If we will send GET request - add params to the URL
if ($method == 'GET') {
$url = url($url, array('query' => $query));
}
// otherwise - configure $options array
elseif ($method == 'POST') {
$options += array(
'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
'method' => 'POST',
'data' => drupal_http_build_query($query),
);
}
return drupal_http_request($url, $options);
}
/**
* Prepares data to be send to OHT service and returns XML string.
* @param TMGMTJob $job
* @return string
*/
protected function prepareDataForSending(TMGMTJob $job) {
$data = array_filter(tmgmt_flatten_data($job->getData()), '_tmgmt_filter_data');
$items = '';
foreach($data as $key => $value) {
$items .= str_replace(array('@key', '@text'), array($key, $value['#text']), '<item key="@key"><text type="text/html"><![CDATA[@text]]></text></item>');
}
return '<items>' . $items .'</items>';
}
/**
* Parses received translation from OHT and returns unflatted data
* @param $data string Base64 encode data, receveived from OHT
* @return array Unflatted data
*/
protected function parseTranslationData($data) {
$data = base64_decode($data);
$items = simplexml_load_string($data);
$data = array();
foreach($items->item as $item) {
$key = (string) $item['key'];
$data[$key]['#text'] = (string)$item->text;
}
return tmgmt_unflatten_data($data);
}
}