Overview

This exercise demonstrates how to translate text using Azure AI Translator, enabling users to convert input text into different languages.


Steps & Configuration Details

1. Provision an Azure AI Translator Resource

  • Open Azure Portal (https://portal.azure.com) and sign in.
  • Search for Azure AI services → Select Create under Translator.
  • Configuration Items:
    • Subscription: Your Azure subscription.
    • Resource Group: Select or create a resource group.
    • Region: Choose any available region.
    • Name: Enter a unique name.
    • Pricing Tier: F0 (Free) or S (Standard).
    • Responsible AI Notice: Agree.

After provisioning, navigate to Keys and Endpoint in the Resource Management section.


2. Clone the Repository

  • Open Azure Cloud Shell in the Azure Portal.
  • Select PowerShell as the environment.
  • Run the following commands:
    rm -r mslearn-ai-language -f
    git clone https://github.com/MicrosoftLearning/mslearn-ai-language mslearn-ai-language
    cd mslearn-ai-language/Labfiles/06b-translator-sdk
    

3. Configure Your Application

  • Navigate to the correct folder:
    cd CSharp/translate-text  # For C#
    cd Python/translate-text   # For Python
    
  • Install dependencies:
    • C#:
      dotnet add package Azure.AI.Translation.Text --version 1.0.0-beta.1
      
    • Python:
      pip install azure-ai-translation-text==1.0.0b1
      
  • Open the configuration file:
    • C#: appsettings.json
    • Python: .env
  • Update Configuration Values:
    • Azure AI Translator Region
    • API Key
  • Save the configuration file.

4. Add Code to Translate Text

  • Open the code file:
    • C#: Program.cs
    • Python: translate.py
  • Add references:
    • C#:
      using Azure;
      using Azure.AI.Translation.Text;
      
    • Python:
      from azure.ai.translation.text import *
      from azure.ai.translation.text.models import InputTextItem
      
  • Create the AI Translator client:
    • C#:
      AzureKeyCredential credential = new (translatorKey);
      TextTranslationClient client = new (credential, translatorRegion);
      
    • Python:
      credential = TranslatorCredential(translatorKey, translatorRegion)
      client = TextTranslationClient(credential)
      
  • Choose the target language:
    • C#:
      Response<GetLanguagesResult> languagesResponse = await client.GetLanguagesAsync(scope: "translation").ConfigureAwait(false);
      GetLanguagesResult languages = languagesResponse.Value;
      Console.WriteLine($"{languages.Translation.Count} languages available.");
      Console.WriteLine("Enter a target language code for translation (for example, 'en'):");
      string targetLanguage = Console.ReadLine();
      
    • Python:
      languagesResponse = client.get_languages(scope="translation")
      print("{} languages supported.".format(len(languagesResponse.translation)))
      print("Enter a target language code for translation (for example, 'en'):")
      targetLanguage = input()
      
  • Translate text:
    • C#:
      string inputText = "";
      while (inputText.ToLower() != "quit") {
          Console.WriteLine("Enter text to translate ('quit' to exit)");
          inputText = Console.ReadLine();
          if (inputText.ToLower() != "quit") {
              Response<IReadOnlyList<TranslatedTextItem>> translationResponse = await client.TranslateAsync(targetLanguage, inputText).ConfigureAwait(false);
              TranslatedTextItem translation = translationResponse.Value[0];
              Console.WriteLine($"'{inputText}' translated to '{translation.Translations[0].Text}'.");
          }
      }
      
    • Python:
      inputText = ""
      while inputText.lower() != "quit":
          inputText = input("Enter text to translate ('quit' to exit):")
          if inputText != "quit":
              input_text_elements = [InputTextItem(text=inputText)]
              translationResponse = client.translate(content=input_text_elements, to=[targetLanguage])
              translation = translationResponse[0]
              print(f"'{inputText}' translated to '{translation.translations[0].text}'.")
      

5. Run Your Application

  • C#:
    dotnet run
    
  • Python:
    python translate.py
    
  • Example prompt:
    Translate "Hello, how are you?" to French.
    
  • The response should display the translated text.

6. Clean Up

  • Delete Azure resources to avoid unnecessary costs:

Related Posts