Skip to content

Text Translation with Azure AI Translator ๐ŸŒ

๐Ÿงฉ Problem

You need to build an application that translates text from any supported language to a target language, detecting the source language automatically.

๐Ÿ’ก Solution with Azure

Use Azure AI Translator to:

  • Translate text between supported languages
  • Auto-detect source language
  • Build client applications (C#/Python) using Azure AI Translator SDK

โš™๏ธ Components Required

  • Azure AI Translator resource
  • Azure subscription with Azure portal access
  • Visual Studio Code
  • GitHub repo: https://github.com/MicrosoftLearning/mslearn-ai-language
  • Azure AI Translator SDK:
  • C#: Azure.AI.Translation.Text 1.0.0-beta.1
  • Python: azure-ai-translation-text==1.0.0b1

๐Ÿ—๏ธ Architecture / Development

1๏ธโƒฃ Provision Azure AI Translator Resource

In Azure portal: - Create new Azure AI Translator resource - Supported region: Any available region - Pricing tier: F0 (free) or S (standard)

Retrieve: - Key - Region (โš  Required by SDK)

2๏ธโƒฃ Clone Code Repository

  • Clone repo: https://github.com/MicrosoftLearning/mslearn-ai-language
  • Open repo in Visual Studio Code: Labfiles/06b-translator-sdk/translate-text

3๏ธโƒฃ Install SDK Package

C#:

dotnet add package Azure.AI.Translation.Text --version 1.0.0-beta.1

Python:

pip install azure-ai-translation-text==1.0.0b1

4๏ธโƒฃ Configure Application

Edit config files: - C#: appsettings.json - Python: .env

Set: - translatorKey - translatorRegion (โš  Not endpoint)

5๏ธโƒฃ Add Code to Import SDK Namespaces

C#:

using Azure;
using Azure.AI.Translation.Text;

Python:

from azure.ai.translation.text import *
from azure.ai.translation.text.models import InputTextItem

6๏ธโƒฃ Create Translator Client

C#:

AzureKeyCredential credential = new(translatorKey);
TextTranslationClient client = new(credential, translatorRegion);

Python:

credential = TranslatorCredential(translatorKey, translatorRegion)
client = TextTranslationClient(credential)

7๏ธโƒฃ Get Supported Languages & Select Target Language

C#:

Response<GetLanguagesResult> languagesResponse = await client.GetLanguagesAsync(scope:"translation");
GetLanguagesResult languages = languagesResponse.Value;
string targetLanguage = Console.ReadLine();

Python:

languagesResponse = client.get_languages(scope="translation")
targetLanguage = input()

8๏ธโƒฃ Translate Text

C#:

Response<IReadOnlyList<TranslatedTextItem>> translationResponse = await client.TranslateAsync(targetLanguage, inputText);
TranslatedTextItem translation = translationResponse.Value[0];

Python:

input_text_elements = [InputTextItem(text=inputText)]
translationResponse = client.translate(content=input_text_elements, to=[targetLanguage])
translation = translationResponse[0]

9๏ธโƒฃ Run Application

C#:

dotnet run

Python:

python translate.py

๐Ÿ”ง Best Practice / Considerations

  • Use correct region parameter, not the full endpoint URL
  • Use latest SDK version supported for Azure Translator
  • Validate supported languages at runtime using GetLanguages
  • Auto-detect source language (included in SDK response)
  • Handle possible errors (invalid language codes, empty input)

โ“ Exam-like Sample Questions

Question 1:

Which SDK package is used for Azure AI Translator in C#?

A. Azure.AI.TextAnalytics
B. Azure.AI.Translation.Text
C. Azure.AI.Language

โœ… Answer: B

Question 2:

What parameter must be provided alongside the API key when creating the client?

A. Endpoint URL
B. Subscription ID
C. Region

โœ… Answer: C

Question 3:

Which method retrieves the list of supported translation languages?

A. GetLanguagesAsync
B. ListLanguages
C. FetchSupportedLanguages

โœ… Answer: A

Question 4:

What SDK version was used in this lab?

A. 1.0.0-beta.1
B. 5.3.0
C. 4.2.0

โœ… Answer: A

Question 5:

Which Python object is used to send input text for translation?

A. InputTextItem
B. TranslateRequest
C. TextItem

โœ… Answer: A