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#:
Python:
4๏ธโฃ Configure Application
Edit config files:
- C#: appsettings.json
- Python: .env
Set:
- translatorKey
- translatorRegion
(โ Not endpoint)
5๏ธโฃ Add Code to Import SDK Namespaces
C#:
Python:
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:
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#:
Python:
๐ง 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