Amazon Translate Using .NET MVC & C#

Machine language translation services, like Amazon Translate or Google Translate, are becoming smarter and more accurate every day. Integrating machine translation functionality into your own web or mobile applications isn’t as difficult as you might think.

For this tutorial, let’s pretend you have an ASP.NET MVC application written in C# that accepts user comments. Imagine the power in leveraging Amazon Translate to accurately and efficiently translate user comments into other languages.

The purpose of this article is to provide a high-level overview on implementing Amazon Translate using C# within an ASP.NET MVC application.

This tutorial is meant to act as a general guide. This article is not intended to be a step by step tutorial. Most likely, implementation details will vary based on your application.

A user submits a comment, written in English, to your MVC application.

The user’s comment is translated to Chinese, French, and German using Amazon Translate.

What Is Amazon Translate?

Amazon Translate is a neural network capable of accurately translating from English to Arabic, Chinese (Simplified), Chinese (Traditional), Czech, French, German, Italian, Japanese, Portuguese, Russian, Spanish, and Turkish. Amazon translate is also capable of translating text for any of the supported languages back to English.

Amazon Translate leverages sophisticated attention mechanisms to comprehend context. This allows Amazon Translate to better decide which elements in the source are most important for deciding the next target word. Attention mechanisms help to ensure that the API accurately translates ambiguous phrases or words.

Getting Your Amazon Translation API Key

You’re going to need an AWS Access Key ID and a AWS Secret Access Key to send requests to Amazon Translate. For the purposes of this article, I’m assuming you have a AWS account already.

Create an IAM User

We need to create a new user within AWS Identity and Access Management (IAM). We’re going to check the Programmatic access option, which will provide this user with the access id and secret key required to make calls to Amazon Translate.

You’re going to specify a username. In the screenshot below, my user’s name is amazon-translate-user, but you can name the user whatever you please.

Upon successful creation of your new IAM user, you’ll have the option to download the credentials required to call the Amazon Translate service.

Preparing Your MVC Project

In an effort to drive adoption, Amazon publishes a suite of packages to make integration with AWS as painless as possible. For our purposes, there are two NUGET packages we’re going to include to jump start our integration.

AWSSDK.Core

First, add the AWS Core package to your Visual Studio project. This package provides the necessary framework for integrating with AWS, which includes the functionality needed for authentication.

Note the difference between TranslateCommentViewModel and TranslatedCommentViewModel. I probably should not have named these ViewModels so close to one another. They are, in fact, two separate objects.

The TranslateCommentViewModel is part of the user’s request and is designed to accept the submitter’s name, the actual text to be translated named CommentText, and the language we’ll be translating to, which I’m calling TargetLanguage.

The TranslatedCommentViewModel is the response to the user and includes the submitter’s name, the original comment text, the target language, and, most importantly, the TranslateTextResponse. The TranslateTextResponse is being returned from the Amazon.Translate.Model namespace.

In the real-world, I probably wouldn’t return the actual Amazon model TranslateTextResponse as part of the ViewModel, but let’s assume it’s OK for the purpose of this article.

Creating Our Controller Actions

We’re going to have two actions within our Controller. The first action, called AddComment, will be a Get used to display the comment form to the user. The second action, we’ll call TranslateComment, will be a Postwhere the actual integration with Amazon Translate resides.

1

2

3

4

5

publicActionResult AddComment()

{

varmodel=newTranslateCommentViewModel();

returnView(model);

}

Here’s Where the Magic Happens!

The code below is where the text is sent to Amazon Translate for the actual translation.

Next, we need to instantiate the TranslateTextRequest that we’ll be sending to the Amazon Translate API. For the sake of this demo, we’re always translating from English. Therefore, we’re setting the SourceLanguageCode as “en”. However, this could easily be expanding for translating from other source languages. We also need to specify the TargetLanguageCode, which was selected by the user.

Leveraging Amazon Translate In Your Applications

Well, there you have it! As you can see, it’s not too difficult to integrate Amazon Translate into your applications. I’ll let you in on a secret, the Google Translate integration is the exact same approach.

Josh Greenberg is a developer, partner, and founder at Codemoto based in Boulder, Colorado. I’ve been developing commercial web applications for the last twenty years. With a long history in C#, ASP.NET, MVC, I’ve been mostly focused on NodeJS, Angular, Express, and MongoDB for the past few years.

3

Comments

2Comment threads

1Thread replies

0Followers

Most reacted comment

Hottest comment thread

3Comment authors

Recent comment authors

Subscribe

newestoldestmost voted

Notify of

Guest

Jake Martin

Nice article! There isn’t a ton of info on integrating machine translation into apps yet. Much appreciated.