Skip to content

Commit

Permalink
Merge pull request #68 from arition/master
Browse files Browse the repository at this point in the history
Add Azure translation API
  • Loading branch information
tinyAdapter committed Feb 6, 2020
2 parents ce74031 + 4c13d7a commit 46141f8
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions config/azureApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
AZURE_TRANSLATION_URL = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=zh-Hans&from=ja";
// Get a key at Azure, free 2 million words / month
SUBSCRIPTION_KEY = "5ce850af5f37474c91e6911ca36ddc24";

requestTranslation = () => {
return new Promise((resolve, reject) => {
Request.post(AZURE_TRANSLATION_URL, {
headers: {
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": SUBSCRIPTION_KEY
},
json: true,
body: [{ Text: text }]
}).then(json => {
if (json.error) {
callback(`Error: ${json.error.message}`);
} else {
var result = json[0].translations[0].text;
callback(result);
}
}).catch(err => {
callback(`error: ${err}`);
})
})
}
requestTranslation();

0 comments on commit 46141f8

Please sign in to comment.