Azure SDK for JavaScript: Translation Service
This guide provides an introduction to using the Azure Cognitive Services Translator client library for JavaScript. This library allows you to programmatically integrate translation capabilities into your applications.
Key Features
- Translate text between various languages.
- Detect the language of a given text.
- Transliterate text from one script to another (e.g., Cyrillic to Latin).
- Get a list of supported languages.
Getting Started
1. Prerequisites
- An Azure subscription.
- A Translator resource created in the Azure portal. You can find your key and endpoint in the resource's overview page.
- Node.js installed on your machine.
2. Installation
Install the Azure Text Translation client library for JavaScript:
npm install @azure/cognitiveservices-translator
3. Authentication
You'll need your Translator resource's API key and endpoint to authenticate requests.
4. Basic Usage: Text Translation
The following example demonstrates how to translate text from English to Spanish.
const { TextTranslationClient } = require("@azure/cognitiveservices-translator");
const { ApiKeyCredentials } = require("@azure/ms-rest-js");
// Replace with your Translator resource key and endpoint
const subscriptionKey = "YOUR_TRANSLATOR_SUBSCRIPTION_KEY";
const endpoint = "YOUR_TRANSLATOR_ENDPOINT";
const credentials = new ApiKeyCredentials(subscriptionKey);
const client = new TextTranslationClient(credentials, { endpoint });
async function translateText() {
const textToTranslate = "Hello, world!";
const targetLanguages = ["es"]; // Spanish
try {
const result = await client.translate(targetLanguages, textToTranslate);
console.log("Original Text:", textToTranslate);
console.log("Translations:");
for (const translation of result) {
console.log(` - To ${translation.to}: ${translation.text}`);
}
} catch (error) {
console.error("Error translating text:", error);
}
}
translateText();
5. Basic Usage: Language Detection
Detect the language of a given text:
const { TextTranslationClient } = require("@azure/cognitiveservices-translator");
const { ApiKeyCredentials } = require("@azure/ms-rest-js");
// ... (credentials and client setup as above)
async function detectLanguage() {
const textToDetect = "Bonjour le monde!";
try {
const result = await client.detect(textToDetect);
console.log("Text:", textToDetect);
console.log("Detected Languages:");
for (const detection of result) {
console.log(` - Language: ${detection.language}, Score: ${detection.score.toFixed(2)}`);
}
} catch (error) {
console.error("Error detecting language:", error);
}
}
detectLanguage();
API Reference
translate(targetLanguages: string[], text: string, options?: TranslateOptions)
Translates text from its detected source language to the specified target languages.
targetLanguages: An array of language codes (e.g.,["es"]for Spanish,["fr", "de"]for French and German).text: The text string to translate.options(optional): An object for advanced translation options, includingsourceLanguage,customizationId, etc.
detect(text: string, options?: DetectOptions)
Detects the language of the given text.
text: The text string for which to detect the language.options(optional): An object for advanced detection options.
listSupportedLanguages(options?: LanguageOptions)
Gets a list of languages supported by the Translator service.
options(optional): An object for specifying language details (e.g.,includeTranslationsto get translated names).
For a comprehensive API reference, please visit the full API documentation.
Next Steps
- Explore advanced translation features like custom translation models.
- Integrate language detection into your content processing pipelines.
- Refer to the GitHub repository for more examples and source code.