Created
December 5, 2016 18:00
-
-
Save sunnygleason/727b6c34cae2190ae56f4f55421c2bdf to your computer and use it in GitHub Desktop.
PubNub Translation BLOCK with SDL Language Cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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