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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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
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.