Outdated content, again: Exercise - Translate text with the Azure AI Translator service

Michael G 30 Reputation points
2024-12-07T17:10:15.9966667+00:00

The content of page 'Exercise - Translate text with the Azure AI Translator service' is very outdated. It largely does not work as-is and needs to be updated. I keep on finding such pages. I don't know Microsoft does not update these more often. Doesn't Microsoft want more developers to learn about how their AI offerings/products work?

Exercise URL: https://learn.microsoft.com/en-us/training/modules/translate-text-with-translator-service/6-exercise-translate-text

I have rewritten the incorrect code to reflect the latest Azure.AI.Translation.Text NuGet package:

using Azure;

using Azure.AI.Translation.Text;

using Microsoft.Extensions.Configuration;

using System;

using System.Collections.Generic;

using System.Text;

using System.Threading.Tasks;

namespace translate_text

{

class Program

{

static async Task Main(string[] args)

{

try

{

// Set console encoding to unicode

Console.InputEncoding = Encoding.Unicode;

Console.OutputEncoding = Encoding.Unicode;

// Get config settings from AppSettings

IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");

IConfigurationRoot configuration = builder.Build();

string translatorRegion = configuration["TranslatorRegion"];

string translatorKey = configuration["TranslatorKey"];

// Create client using endpoint and key

AzureKeyCredential credential = new(translatorKey);

TextTranslationClient client = new(credential, translatorRegion);

// Choose target language

Response<GetSupportedLanguagesResult> languagesResponse = await client.GetSupportedLanguagesAsync(scope: "translation");

GetSupportedLanguagesResult 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 = "";

bool languageSupported = false;

while (!languageSupported && string.IsNullOrWhiteSpace(targetLanguage))

{

targetLanguage = Console.ReadLine();

if (languages.Translation.ContainsKey(targetLanguage))

{

languageSupported = true;

}

else

{

Console.WriteLine($"{targetLanguage} is not a supported language.");

}

}

// Translate text

string inputText = "";

while (string.IsNullOrWhiteSpace(inputText) && inputText.ToLower() != "quit")

{

Console.WriteLine("Enter text to translate ('quit' to exit)");

inputText = Console.ReadLine();

if (!string.IsNullOrWhiteSpace(inputText) && inputText.ToLower() != "quit")

{

Response<IReadOnlyList<TranslatedTextItem>> translationResponse = await client.TranslateAsync(targetLanguage, inputText);

IReadOnlyList<TranslatedTextItem> translations = translationResponse.Value;

TranslatedTextItem translation = translations[0];

string sourceLanguage = translation?.DetectedLanguage?.Language;

Console.WriteLine($"'{inputText}' translated from {sourceLanguage} to {translation?.Translations[0].TargetLanguage} as '{translation?.Translations?[0]?.Text}'.");

}

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

}

}

This question is related to the following Learning Module

Azure | Azure Training
{count} votes

1 answer

Sort by: Most helpful
  1. VarunTha 14,850 Reputation points Microsoft External Staff Moderator
    2024-12-11T11:03:33.07+00:00

    Hi Michael G,
    We have reached out to the content author and received confirmation that the content will be updated shortly.
    We appreciate your understanding.

    Please accept the Answer and Upvote it to help others in the community.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.