Azure AI Translation Text Client Library

1.0.1 · active · verified Thu Apr 16

The Azure AI Translation Text client library for Python is a cloud-based REST API feature of the Translator service. It uses neural machine translation technology for quick and accurate source-to-target text translation. It allows listing supported languages, performing text translation, and transliteration in real-time. The current stable version is 1.0.1, with a 2.0.0-beta.1 also available with new features and breaking changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `TextTranslationClient` using an API key and region, then perform a simple text translation from English to Spanish and German. Ensure you replace the placeholder values for `TRANSLATOR_ENDPOINT`, `TRANSLATOR_KEY`, and `TRANSLATOR_REGION` with your actual Azure Translator resource details, preferably via environment variables for security.

import os
from azure.ai.translation.text import TextTranslationClient, InputTextItem
from azure.core.credentials import AzureKeyCredential

# --- Configuration (replace with your Azure Translator resource details) ---
# Get these values from your Azure Translator resource in the Azure Portal:
# Endpoint: typically 'https://<your-resource-name>.cognitiveservices.azure.com/' for regional or custom domain
#           or 'https://api.cognitive.microsofttranslator.com/' for global.
# Key: One of the subscription keys for your Translator resource.
# Region: The region where your Translator resource is deployed (e.g., 'eastus', 'global').

endpoint = os.environ.get('TRANSLATOR_ENDPOINT', 'YOUR_TRANSLATOR_ENDPOINT')
key = os.environ.get('TRANSLATOR_KEY', 'YOUR_TRANSLATOR_KEY')
region = os.environ.get('TRANSLATOR_REGION', 'YOUR_TRANSLATOR_REGION')

if 'YOUR_TRANSLATOR_ENDPOINT' in endpoint or 'YOUR_TRANSLATOR_KEY' in key or 'YOUR_TRANSLATOR_REGION' in region:
    print("Please set the 'TRANSLATOR_ENDPOINT', 'TRANSLATOR_KEY', and 'TRANSLATOR_REGION' environment variables or replace placeholders.")
    exit(1)

def translate_text():
    try:
        credential = AzureKeyCredential(key)
        # Note: 'region' is required for TextTranslationClient when using an API key with a regional endpoint.
        # It can be omitted for the global endpoint ('https://api.cognitive.microsofttranslator.com/')
        # or if using a custom subdomain endpoint (where region is part of the endpoint URL).
        text_translator_client = TextTranslationClient(endpoint=endpoint, credential=credential, region=region)

        source_text_items = [
            InputTextItem(text="Hello, how are you?"),
            InputTextItem(text="I am fine, thank you.")
        ]

        # Translate from English to Spanish and German
        target_languages = ["es", "de"]
        response = text_translator_client.translate(content=source_text_items, to=target_languages)

        for translation_group in response:
            for translation in translation_group.translations:
                print(f"Text: '{translation_group.source_text.text}', Translated to '{translation.to}': '{translation.text}'")

    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    translate_text()

view raw JSON →