locked
Google Translation Service RRS feed

  • Question

  • User138908030 posted

    Hi,


    I want to convert my site to different languages based on the culture.(frame work .Net 3.5)When we use localization, we have to write all translated text in the resource files.I want a method which translate every text into the chosen language automatocally.Can I do it with google translator?It will work in IE?Please help me to find a solution.


    Thanks

    Anju

    Thursday, August 12, 2010 5:43 AM

Answers

  • User1508394307 posted

    Here you go.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 8:16 AM
  • User-242433875 posted

    Hi Anju,

    This is possible. Below you can find the code in which the real text served by ASP.Net page is in English but when delivered on browser, it is automatically converted to Spanish using Google Language API. In the sample, I have converted the text present in Label control called "Translation". You can further extend it to work for entire page or a part of it.

    Here goes the code

    ASPX Code

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Automatic Text Conversion using Google Language API</title>
    </head>
        <script type="text/javascript" src="//www.google.com/jsapi"></script> 
        <script type="text/javascript">
            var defLang = "";
            var targetLang = "";
            var ControlID = "Translation";
            google.load("language", "1");
            function initialize() {
                var container = document.getElementById(ControlID);
                if (container != null) {
                    google.language.translate(container.innerHTML, defLang, targetLang, function(result) {
                        if (!result.error) {
                            container.innerHTML = "";
                            container.innerHTML = result.translation;
                        }
                    });
                } else {
                    alert("Invalid ControlID provided.");
                }
            }
        </script> 
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Translation" runat="server" Text="Its Friday, let's party."></asp:Label>
        </form>
    </body>
    </html>

    Code-Behind

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'You can find the whole list of code at following link
            'http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable
    
            'Variables to hold the primary & target language code for Google Language APIs
            Dim PrimaryGoogleLanguageCode As String = "en" 'English
            Dim TargetGoogleLanguageCode As String = "es" 'Spanish
    
            ' Define the name and type of the client script on the page. 
            Dim csName As [String] = "GoogleLangAPI"
            Dim csType As Type = Me.[GetType]()
    
            ' Get a ClientScriptManager reference from the Page class. 
            Dim cs As ClientScriptManager = Page.ClientScript
    
            ' Check to see if the client script is already registered. 
            If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                Dim csText As New StringBuilder()
                csText.Append("<script type=""text/javascript"">defLang=""")
                csText.Append(PrimaryGoogleLanguageCode)
                csText.Append(""";targetLang=""")
                csText.Append(TargetGoogleLanguageCode)
                csText.Append(""";google.setOnLoadCallback(initialize);</script>")
                cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
            End If
        End Sub
    
    End Class


    For the list of language codes used by Google Language Translation service, refer the following URL : http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable

    To refer Google Language Translation API, refer : http://code.google.com/apis/ajaxlanguage/documentation/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 8:29 AM

All replies

  • User1508394307 posted

    Here you go.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 8:16 AM
  • User-242433875 posted

    Hi Anju,

    This is possible. Below you can find the code in which the real text served by ASP.Net page is in English but when delivered on browser, it is automatically converted to Spanish using Google Language API. In the sample, I have converted the text present in Label control called "Translation". You can further extend it to work for entire page or a part of it.

    Here goes the code

    ASPX Code

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Automatic Text Conversion using Google Language API</title>
    </head>
        <script type="text/javascript" src="//www.google.com/jsapi"></script> 
        <script type="text/javascript">
            var defLang = "";
            var targetLang = "";
            var ControlID = "Translation";
            google.load("language", "1");
            function initialize() {
                var container = document.getElementById(ControlID);
                if (container != null) {
                    google.language.translate(container.innerHTML, defLang, targetLang, function(result) {
                        if (!result.error) {
                            container.innerHTML = "";
                            container.innerHTML = result.translation;
                        }
                    });
                } else {
                    alert("Invalid ControlID provided.");
                }
            }
        </script> 
    <body>
        <form id="form1" runat="server">
            <asp:Label ID="Translation" runat="server" Text="Its Friday, let's party."></asp:Label>
        </form>
    </body>
    </html>

    Code-Behind

    Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            'You can find the whole list of code at following link
            'http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable
    
            'Variables to hold the primary & target language code for Google Language APIs
            Dim PrimaryGoogleLanguageCode As String = "en" 'English
            Dim TargetGoogleLanguageCode As String = "es" 'Spanish
    
            ' Define the name and type of the client script on the page. 
            Dim csName As [String] = "GoogleLangAPI"
            Dim csType As Type = Me.[GetType]()
    
            ' Get a ClientScriptManager reference from the Page class. 
            Dim cs As ClientScriptManager = Page.ClientScript
    
            ' Check to see if the client script is already registered. 
            If Not cs.IsClientScriptBlockRegistered(csType, csName) Then
                Dim csText As New StringBuilder()
                csText.Append("<script type=""text/javascript"">defLang=""")
                csText.Append(PrimaryGoogleLanguageCode)
                csText.Append(""";targetLang=""")
                csText.Append(TargetGoogleLanguageCode)
                csText.Append(""";google.setOnLoadCallback(initialize);</script>")
                cs.RegisterClientScriptBlock(csType, csName, csText.ToString())
            End If
        End Sub
    
    End Class


    For the list of language codes used by Google Language Translation service, refer the following URL : http://code.google.com/apis/ajax/playground/?exp=language#is_it_translatable

    To refer Google Language Translation API, refer : http://code.google.com/apis/ajaxlanguage/documentation/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 12, 2010 8:29 AM
  • User-1200560727 posted

    Here, you get solution: http://code.google.com/apis/ajaxlanguage/

    Thursday, August 12, 2010 8:50 AM