Type In Hindi Or Any Other Language Using Google Translate

Introduction

Many times, there are problems when typing in English and converting that to any other language text. Today, in this article, I will explain how to type in Hindi using the Google API. We use the Google API because it is free, easy to use, and can be used for the transliteration of any language into any other language.

So, let’s start

First, create an empty website in Visual Studio. Then, add a web form. In the source view of webform, type in the following inside <head> </head> HTML tag.

  1. <title>English to Hindi Transliterate</title>  
  2.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>  
  3.     <script type="text/javascript" src="https://www.google.com/jsapi"></script>  

This references the Google jQuery file for transliteration at runtime. After that, in the Head section, please type the following JavaScript.

  1. <script type="text/javascript">  
  2.   
  3.         // Load the Google Transliterate API  
  4.         google.load("elements""1", {  
  5.             packages: "transliteration"  
  6.         });  
  7.         // Set the Source and Destination Languages for Writing and Transliterating to  
  8.         function onLoad() {  
  9.             var options = {  
  10.                 sourceLanguage:  
  11.                 google.elements.transliteration.LanguageCode.ENGLISH,  
  12.                 destinationLanguage:  
  13.                 [google.elements.transliteration.LanguageCode.HINDI],  
  14.                 transliterationEnabled: true  
  15.             };  
  16.   
  17.             // Create an instance on TransliterationControl with the required  
  18.             // options.  
  19.             var control =  
  20.             new google.elements.transliteration.TransliterationControl(options);  
  21.   
  22.             // Enable transliteration in the textbox with id  
  23.             // 'transliterateTextarea'.  
  24.             control.makeTransliteratable(['TextBox1']);  
  25.         }  
  26.         google.setOnLoadCallback(onLoad);  
  27.     </script>  
  28. Here the first line loads the google Transliterate API:-  
  29. google.load("elements""1", {  
  30.             packages: "transliteration"  
  31.         });  

In the next line of code, we set the source and destination languages for typing. I have set the source language as English and destination language as Hindi (type in Hindi words in English and transliterate them to Hindi fonts) and create an object of the type “control” (You can change the input and output languages here to any other language of your choice).

  1. var options = {  
  2.                 sourceLanguage:  
  3.                 google.elements.transliteration.LanguageCode.ENGLISH,  
  4.                 destinationLanguage:  
  5.                 [google.elements.transliteration.LanguageCode.HINDI],  
  6.                 transliterationEnabled: true  
  7.             };  
  8. var control =  
  9.             new google.elements.transliteration.TransliterationControl(options);  

In the next line, we set the control to which transliteration should be applied.

  1. control.makeTransliteratable(['TextBox1']);  

We have set it to TextBox1 which we will add in the body section of the webform.

In the next line.

  1. google.setOnLoadCallback(onLoad);  

Here, we set Google to set a callback to our OnLoad function as soon as the page is active.

Now, let’s add a textbox to our webform in the body section.

  1. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  

This is the control which is set to be typed in English and transliterated to Hindi.

Now, let’s run the program and type a Hindi word in English. Then, type a space and it will be written in Hindi.

It is important that you have an active internet connection for this program to work because we are accessing the CDN of Google API jQuery library. You can check the attached project file. You can play with it and change it to other languages.

That’s all folks. Thank you!!!


Similar Articles