Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PubNub Translation BLOCK with SDL Language Cloud
export default request => {
let xhr = require('xhr');
let clientToken = '00000000000000000000000000000000';
let apiUrl = 'https://lc-api.sdl.com/translate';
const text = request.message.text;
const from = request.message.from;
const to = request.message.to;
if (!text || !from || !to) {
return request.ok();
}
const payload = JSON.stringify({ text, from, to });
let httpOptions = {
as: 'json',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'LC apiKey=' + clientToken
},
body: payload,
method: 'post'
};
return xhr.fetch(apiUrl, httpOptions)
.then((response) => {
return response.json()
.then((parsedResponse) => {
request.message.translation = parsedResponse.translation;
return request.ok();
})
.catch((err) => {
console.log('error happened on JSON parse', err);
return request.ok();
});
})
.catch((err) => {
console.log('error happened for XHR.fetch', err);
return request.ok();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment