Using the Google Translator in Web API

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:

  1. First we create a Google Account.
  2. To integrate the Google Translator, login with your Gmail id from this link:
    https://translate.google.com/manager/website/
  1. Click on "Sign in".

    trans.jpg

    Sign in with your Gmail id.
  1. Then login with your id. Open a window.

    trans1.jpg
  1. Click on the tab "Add to your website now" .

    Type your website URL in the "website URL" TextBox.

    trans3.jpg

    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.
  1. Open a window in which you select "translation language", "display mode" and click on "Get Code" button.

    trans4.jpg

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.

trans5.jpg

  1. 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.

    trans10.jpg
  • From the "MVC4 Project" window select "Web API".

    trans11.jpg
  • Click the "OK" button.
  1. 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.

    trans9.jpg

Copy the Meta tag content and paste it in the "<head>" tag.

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html>  
  3.     <head>  
  4.         <title>  
  5.             c-sharpcorner  
  6.         </title>  
  7.        <meta name="google-translate-customization" content="3280487709591956-dc3fc45d489f056a-g5378ebab0cbcd0a4-12"/>  
  8.     </head> 

Now copy the block of code and paste it into the "<body>" tag.

  1. <body>  
  2. <div id="google_translate_element">  
  3.     Welcome to! MSDN Site.  
  4. </div><script type="text/javascript">  
  5.           function googleTranslateElementInit()  
  6.           {  
  7.             new google.translate.TranslateElement({ pageLanguage: 'en', layout: google.translate.TranslateElement.InlineLayout.SIMPLE }, 'google_translate_element');  
  8.           }  
  9. </script>  
  10.     <script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit">  
  11.          </script>  
  12.     </body>  
  13. </html> 
  1. Now execute the application:

trans6.jpg


Click on the Dropdown list:

trans7.jpg

Select any language. Here the output looks like this:

trans8.jpg


Similar Articles