Skip to content

Instantly share code, notes, and snippets.

@sunnygleason
Created December 5, 2016 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunnygleason/727b6c34cae2190ae56f4f55421c2bdf to your computer and use it in GitHub Desktop.
Save sunnygleason/727b6c34cae2190ae56f4f55421c2bdf to your computer and use it in GitHub Desktop.
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