Azure SDK for JavaScript

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

Getting Started

1. Prerequisites

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, including sourceLanguage, 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., includeTranslations to get translated names).

For a comprehensive API reference, please visit the full API documentation.

Next Steps