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.
Amazon Translation MVC

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.
  1. A user submits a comment, written in English, to your MVC application.
  2. 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.

Create New IAM User

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.

AWSSDK.Translate

Second, add the AWSSDK – Amazon Translate package to your Visual Studio project. This package provides the functionality for processing the actual language translations through Amazon.

Creating Our Models

We need to create a ViewModel for passing in the user’s comment along with the requested language into our Controller.

We’re also going to create a ViewModel for displaying the translated text to the user.

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 Post where the actual integration with Amazon Translate resides.

Here’s Where the Magic Happens!

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

Magic HappensFirst, we create a new AmazonTranslateClient. You’ll want to include your AWS access key and secret key. You’ll also need to specify your desired AWS region endpoint.

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.



Creating Our Views

Add Comment View

The first view displays a form for the user to enter their comment. This comment form posts to the action TranslateComment.

Add Comment View

Translate Comment View

The send view displays the actual translated text..

Translate Comment View

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.

Thanks for reading, I hope you found this useful.

Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jake Martin
Jake Martin
6 years ago

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

Jill Strauss
Jill Strauss
6 years ago

I was recently tasked with integrating machine translation into our mobile app. This tutorial really helped jump start the process. Thanks for taking the time to put together this tutorial.

Milind
Milind
3 years ago

Can we use aws translate on webform where having multiple fileds. Is there any function where we can pass input text of all fields once and get the output.
in ASP.NET MVC web application

Last edited 3 years ago by Milind
web
mobile
desktop
cloud
Get in touch! We would love to discuss your next software project.
4
0
Would love your thoughts, please comment.x
()
x