Introduction
This article explains how to add the Google Translator to a Web API application.
Google Translator
The Google Translator translates the search Query to the language you want. Google Translator is a free statistical multilingual machine translation service that is provided by Google. It translates written text from one language to another.
Now you can see the process of using the Google Translator in a Web API application as in the following:
- First we create a Google Account.
- To integrate the Google Translator, login with your Gmail id from this link:
https://translate.google.com/manager/website/
- Click on "Sign in".
Sign in with your Gmail id.
- Then login with your id. Open a window.
- Click on the tab "Add to your website now" .
Type your website URL in the "website URL" TextBox.
Website URL: in this block you write your website URL.
Website language: It defines the default language for your website.
Click on the "Next" button.
- Open a window in which you select "translation language", "display mode" and click on "Get Code" button.
Translation Languages: here two options are available as in the following:
- All language: If we select all languages then it shows all the languages that are used in the Google Translator.
- Specific languages: If you select this option then you choose a specific language.
Display mode: This mode defines the list of languages in a different mode.
Now click on the "Get Code" button.
- Create a Web API application as in the following:
- Start Visual Studio 2012.
- From Start window select "New Project".
- From the new project window select "Installed" -> "Visual C#" -> "Web".
- Select "ASP.NET MVC4 Web Application" and click on the "OK" button.
- From the "MVC4 Project" window select "Web API".
- For copying the code you need the "index.cshtml" file. This file exists:
- In the "Solution Explorer".
- Expand "Home" folder.
- Select the "index.cshtml" file and add the code.
Copy the Meta tag content and paste it in the "<head>" tag.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>
c-sharpcorner
</title>
<meta name="google-translate-customization" content="3280487709591956-dc3fc45d489f056a-g5378ebab0cbcd0a4-12"/>
</head>
Now copy the block of code and paste it into the "<body>" tag.
<body>
<div id="google_translate_element">
Welcome to! MSDN Site.
</div><script type="text/javascript">
function googleTranslateElementInit()
{
new google.translate.TranslateElement({ pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE }, 'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">
</script>
</body>
</html>
- Now execute the application:
Click on the Dropdown list:
Select any language. Here the output looks like this: