Translate Text

Azure AI Translator is a service that enables you to translate text between languages. In this exercise, you’ll use it to create a simple app that translates input in any supported language to the target language of your choice.

Provision an Azure AI Translator resource

If you don’t already have one in your subscription, you’ll need to provision an Azure AI Translator resource.

  1. Open the Azure portal at https://portal.azure.com, and sign in using the Microsoft account associated with your Azure subscription.
  2. In the search field at the top, search for Azure AI services and press Enter, then select Create under Translator in the results.
  3. Create a resource with the following settings:
    • Subscription: Your Azure subscription
    • Resource group: Choose or create a resource group
    • Region: Choose any available region
    • Name: Enter a unique name
    • Pricing tier: Select F0 (free), or S (standard) if F is not available.
    • Responsible AI Notice: Agree.
  4. Select Review + create, then select Create to provision the resource.
  5. Wait for deployment to complete, and then go to the deployed resource.
  6. View the Keys and Endpoint page. You will need the information on this page later in the exercise.

Prepare to develop an app in Visual Studio Code

You’ll develop your text translation app using Visual Studio Code. The code files for your app have been provided in a GitHub repo.

Tip: If you have already cloned the mslearn-ai-language repo, open it in Visual Studio code. Otherwise, follow these steps to clone it to your development environment.

  1. Start Visual Studio Code.
  2. Open the palette (SHIFT+CTRL+P) and run a Git: Clone command to clone the https://github.com/MicrosoftLearning/mslearn-ai-language repository to a local folder (it doesn’t matter which folder).
  3. When the repository has been cloned, open the folder in Visual Studio Code.

    Note: If Visual Studio Code shows you a pop-up message to prompt you to trust the code you are opening, click on Yes, I trust the authors option in the pop-up.

  4. Wait while additional files are installed to support the C# code projects in the repo.

    Note: If you are prompted to add required assets to build and debug, select Not Now.

Configure your application

Applications for both C# and Python have been provided. Both apps feature the same functionality. First, you’ll complete some key parts of the application to enable it to use your Azure AI Translator resource.

  1. In Visual Studio Code, in the Explorer pane, browse to the Labfiles/06b-translator-sdk folder and expand the CSharp or Python folder depending on your language preference and the translate-text folder it contains. Each folder contains the language-specific code files for an app into which you’re you’re going to integrate Azure AI Translator functionality.
  2. Right-click the translate-text folder containing your code files and open an integrated terminal. Then install the Azure AI Translator SDK package by running the appropriate command for your language preference:

    C#:

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

    Python:

     pip install azure-ai-translation-text==1.0.0b1
    
  3. In the Explorer pane, in the translate-text folder, open the configuration file for your preferred language

    • C#: appsettings.json
    • Python: .env
  4. Update the configuration values to include the region and a key from the Azure AI Translator resource you created (available on the Keys and Endpoint page for your Azure AI Translator resource in the Azure portal).

    NOTE: Be sure to add the region for your resource, not the endpoint!

  5. Save the configuration file.

Add code to translate text

Now you’re ready to use Azure AI Translator to translate text.

  1. Note that the translate-text folder contains a code file for the client application:

    • C#: Program.cs
    • Python: translate.py

    Open the code file and at the top, under the existing namespace references, find the comment Import namespaces. Then, under this comment, add the following language-specific code to import the namespaces you will need to use the Text Analytics SDK:

    C#: Programs.cs

     // import namespaces
     using Azure;
     using Azure.AI.Translation.Text;
    

    Python: translate.py

     # import namespaces
     from azure.ai.translation.text import *
     from azure.ai.translation.text.models import InputTextItem
    
  2. In the Main function, note that the existing code reads the configuration settings.
  3. Find the comment Create client using endpoint and key and add the following code:

    C#: Programs.cs

     // Create client using endpoint and key
     AzureKeyCredential credential = new(translatorKey);
     TextTranslationClient client = new(credential, translatorRegion);
    

    Python: translate.py

     # Create client using endpoint and key
     credential = TranslatorCredential(translatorKey, translatorRegion)
     client = TextTranslationClient(credential)
    
  4. Find the comment Choose target language and add the following code, which uses the Text Translator service to return list of supported languages for translation, and prompts the user to select a language code for the target language.

    C#: Programs.cs

     // Choose target language
     Response<GetLanguagesResult> languagesResponse = await client.GetLanguagesAsync(scope:"translation").ConfigureAwait(false);
     GetLanguagesResult languages = languagesResponse.Value;
     Console.WriteLine($"{languages.Translation.Count} languages available.\n(See https://learn.microsoft.com/azure/ai-services/translator/language-support#translation)");
     Console.WriteLine("Enter a target language code for translation (for example, 'en'):");
     string targetLanguage = "xx";
     bool languageSupported = false;
     while (!languageSupported)
     {
         targetLanguage = Console.ReadLine();
         if (languages.Translation.ContainsKey(targetLanguage))
         {
             languageSupported = true;
         }
         else
         {
             Console.WriteLine($"{targetLanguage} is not a supported language.");
         }
    
     }
    

    Python: translate.py

     # Choose target language
     languagesResponse = client.get_languages(scope="translation")
     print("{} languages supported.".format(len(languagesResponse.translation)))
     print("(See https://learn.microsoft.com/azure/ai-services/translator/language-support#translation)")
     print("Enter a target language code for translation (for example, 'en'):")
     targetLanguage = "xx"
     supportedLanguage = False
     while supportedLanguage == False:
         targetLanguage = input()
         if  targetLanguage in languagesResponse.translation.keys():
             supportedLanguage = True
         else:
             print("{} is not a supported language.".format(targetLanguage))
    
  5. Find the comment Translate text and add the following code, which repeatedly prompts the user for text to be translated, uses the Azure AI Translator service to translate it to the target language (detecting the source language automatically), and displays the results until the user enters quit.

    C#: Programs.cs

     // Translate text
     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);
             IReadOnlyList<TranslatedTextItem> translations = translationResponse.Value;
             TranslatedTextItem translation = translations[0];
             string sourceLanguage = translation?.DetectedLanguage?.Language;
             Console.WriteLine($"'{inputText}' translated from {sourceLanguage} to {translation?.Translations[0].To} as '{translation?.Translations?[0]?.Text}'.");
         }
     } 
    

    Python: translate.py

     # Translate text
     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] if translationResponse else None
             if translation:
                 sourceLanguage = translation.detected_language
                 for translated_text in translation.translations:
                     print(f"'{inputText}' was translated from {sourceLanguage.language} to {translated_text.to} as '{translated_text.text}'.")
    
  6. Save the changes to your code file.

Test your application

Now your application is ready to test.

  1. In the integrated terminal for the Translate text folder, and enter the following command to run the program:

    • C#: dotnet run
    • Python: python translate.py

    Tip: You can use the Maximize panel size (^) icon in the terminal toolbar to see more of the console text.

  2. When prompted, enter a valid target language from the list displayed.
  3. Enter a phrase to be translated (for example This is a test or C'est un test) and view the results, which should detect the source language and translate the text to the target language.
  4. When you’re done, enter quit. You can run the application again and choose a different target language.

Clean up

When you don’t need your project anymore, you can delete the Azure AI Translator resource in the Azure portal.