Amazon Translate is an AWS machine learning service that is based upon deep learning and let’s you translate textual data from one language, to another. Amazon Translate has support for 75 different languages and can be used for translating meeting notes, translating telephone transcripts, or providing website search constructs in various languages.
Photo by Daniel Prado on Unsplash
The Solution
In this tutorial, we’ll bring you through a simple example where we show you how to translate textual data from English to Spanish.
Remember, for any example solution from AWS with .NET, we focus on the code that exemplifies the problem we are trying to solve. We don’t include logging, input validation, exception handling, etc., and we embed the configuration data within classes instead of using environment variables, configuration files, key/value stores and the like. These items should not be skipped for proper solutions.
Prerequisites
To complete this solution, you will need the .NET CLI which is included in the .NET SDK. In addition, you will need to create an AWS IAM user with programmatic access with the appropriate permissions to interact with Amazon Translate. In addition, you will need to download the AWS CLI and configure your environment.
Warning: some AWS services may have fees associated with them.
Our Dev Environment
This tutorial was developed using Ubuntu 23.10, .NET 8 SDK and Visual Studio Code 1.86.1. Some commands/constructs may vary across systems.
Create the .NET Amazon Translate Application
The first thing we will do is create the .NET “Translator” App using the .NET CLI.
$ dotnet new console -n Translator –use-program-main
Add App Dependencies
Now, let’s add the AWS .NET SDK dependencies.
$ dotnet add package AWSSDK.Core
$ dotnet add package AWSSDK.Translate
Developing the .NET Amazon Translate App
For the first step in developing the .NET app, we’ll need to open the Program.cs file and change the “Main” function definition to the following:
static async Task Main(string[] args)
Next, we’ll set some variables. We’ll set the input language to English, the output language to Spanish and the text that we are going to translate as the input text.
string inputLanguage = "en";
string outputLanguage = "es";
string inputText = "This is test text for AWS with dot net";
Now, we can instantiate the client. We can use the client to interact with the Amazon Translate service.
AmazonTranslateClient amazonTranslateClient = new
AmazonTranslateClient();
With the client created, we can now create and send the request and await the response. And finally, we print the translated text to the console.
TranslateTextRequest translateTextRequest = new TranslateTextRequest
{
SourceLanguageCode = inputLanguage,
TargetLanguageCode = outputLanguage,
Text = inputText
};
TranslateTextResponse translateTextResponse = await
amazonTranslateClient.TranslateTextAsync(translateTextRequest);
Console.WriteLine("Translated Text: " +
translateTextResponse.TranslatedText);
Testing the .NET Amazon Translate App
With the app complete, let’s build the app and give it a run with the following commands:
$ dotnet build && dotnet run
When the app finishes execution, you should see the following response:
Translated Text: Este es un texto de prueba para AWS con dot net
Summary
We have concluded this tutorial where you have learned how to translate textual data from one language to another using the Amazon Translate service.