This package contains an isomorphic SDK for TranslatorTextClient.
npm install @azure/cognitiveservices-translatortext
npm install @azure/ms-rest-azure-js
The following sample translates the given text which is in Chinese to English. To know more, refer to the Azure Documentation on Translator
import {
TranslatorTextClient,
TranslatorTextModels
} from "@azure/cognitiveservices-translatortext";
import { CognitiveServicesCredentials } from "@azure/ms-rest-azure-js";
async function main(): Promise<void> {
const translatorTextKey =
process.env["translatorTextKey"] || "<translatorTextKey>";
const translatorTextEndPoint =
process.env["translatorTextEndPoint"] || "<translatorTextEndPoint>";
const cognitiveServiceCredentials = new CognitiveServicesCredentials(
translatorTextKey
);
const client = new TranslatorTextClient(
cognitiveServiceCredentials,
translatorTextEndPoint
);
const text: TranslatorTextModels.DetectTextInput[] = [
{
text: "你好,世界"
}
];
client.translator
.detect(text)
.then(result => {
console.log("The result is: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
}
main();
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>@azure/cognitiveservices-translatortext sample</title>
<script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
<script src="node_modules/@azure/cognitiveservices-translatortext/dist/cognitiveservices-translatortext.js"></script>
<script type="text/javascript">
const translatorTextKey = "<YOUR_TRANSLATOR_TEXT_KEY>";
const translatorTextEndPoint = "<YOUR_TRANSLATOR_TEXT_ENDPOINT>";
const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
inHeader: {
"Ocp-Apim-Subscription-Key": translatorTextKey
}
});
const client = new Azure.CognitiveservicesTranslatortext.TranslatorTextClient(
cognitiveServiceCredentials,
translatorTextEndPoint
);
const text = [
{
text: "你好,世界"
}
];
client.translator
.detect(text)
.then(result => {
console.log("The result is: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
</script>
</head>
<body></body>
</html>
Generated using TypeDoc