Wednesday, 19 June 2013

How to Implement Google Translator API in ASP.NET

How to Implement Google Translator API in ASP.NET


This Article you will learn how to implement Google Translator API in your ASP.NETWeb ApplicationGoogle Translator is used to translate the message or word or some texts from one language to another language. This translator is sometime very useful when you are developing a multi language support application in ASP.NET. To implementGoogle Translator I have used some jQuery Scripts that I am attaching later in this project.
Controls and Properties:
1. Web Form: name=”Translator.aspx”.
2. Html control ‘Textarea’: id=”text2bt”.
3. Html control ‘Select’ : id=”frl”.
4. Html control ‘Textarea’ : id=”textt”.
Translatro.aspx
Source code:
<form id="form1" runat="server">
  <div>
  <b>Enter Text here: </b>
  <textarea id="text2bt" style="width:100%" cols="30" rows="3"></textarea>
<b>From</b>
<select style="font-size:8pt;" id="frl" onClick="showLanguageDropDown('frl','en');"></select>
<b>Select the Language</b>
<select style="font-size:8pt;" id="tol" onClick="showLanguageDropDown('tol','ur');"></select>
<input type=button onClick="translate();" value="Translate"/>
<b>Translated Text</b>
<textarea id="textt" style="width:100%" cols="30" rows="3"></textarea>
</div>
</form>

JavaScriptCode:
 <script src="Scripts/jsapi" type="text/javascript"></script>

<script type="text/javascript">
    google.load("language", "1");

    /**
 The translate function, which does the translation using Google Ajax Language API, when the user
 clicks the “Translate Now” button
 */
    function translate() {
        // get textarea that contains the text needs to be translated
        var text2bt = document.getElementById('text2bt');
        // get the drop-down that shows the chosen language to be translated from
        var frl = document.getElementById('frl');
        // get the drop-down that shows the chosen language to be translated to
        var tol = document.getElementById('tol');
        // then call the google translate method
        google.language.translate(text2bt.value, frl.value, tol.value, function (result) {
            if (!result.error) {
                // assign the translated text to the textarea
                var container = document.getElementById("textt");
                container.value = result.translation;
            }
        });
    }

    function showLanguageDropDown(wsel, se) {

        var frl = document.getElementById(wsel);
        if (frl.length > 0) return;
        var a = google.language.Languages;
        var r = 0;
        for (var key in a) {
            var lan = key.toUpperCase();
            var lcode = a[key];
            if (google.language.isTranslatable(lcode)) {
                if (lcode == se)
                    frl.options[r] = new Option(lan, lcode, true, true);
                else
                    frl.options[r] = new Option(lan, lcode, false, false);
            }
            r++;
        }
    }
</script>

No comments:

Post a Comment