次の方法で共有


Translate your text using Google Api's

Here is how you can translate a Text using Google's "Unofficial" API's.

The URL for Google Translate is - https://www.google.com/translate\_t?hl=en\&ie=UTF8\&text={0}\&langpair={1}

  • "text" is your input string which needs to be translated.
  • langpair is the language pairs involved in the tranlsation. E.g. "ar|en" means translate from Arabic to English.

The result when you browse to the URL is a HTML page. You will have to do screen scraping to get your translated text.

Below is a C# function which translates, scrapes and gives you the result. I am using String.Substring function but you can use Regex too.

/// <summary>

/// Translate Text using Google Translate API's

/// Google URL - https://www.google.com/translate\_t?hl=en\&ie=UTF8\&text={0}\&langpair={1}

/// </summary>

/// <param name="input">Input string</param>

/// <param name="languagePair">2 letter Language Pair, delimited by "|".

/// E.g. "ar|en" language pair means to translate from Arabic to English</param>

/// <returns>Translated to String</returns>

public string TranslateText(

    string input,

    string languagePair)

{

    string url = String.Format("https://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);

    WebClient webClient = new WebClient();

    webClient.Encoding = System.Text.Encoding.UTF8;

    string result = webClient.DownloadString(url);

    result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500);

    result = result.Substring(0, result.IndexOf("</div"));

    return result;

}

More details about this Unofficial Google Translation API can be found Here

Comments

  • Anonymous
    June 09, 2007
    Hoy en cosas interesantes: API´s de Google para traducir texto, Clusters en Windows Server 2008, El MSDTC

  • Anonymous
    June 13, 2007
    Very nice :) Have just created a plugin for Live Writer that uses this :) SL

  • Anonymous
    June 14, 2007
    Here's the plugin if you're interested :) http://gallery.live.com/liveItemDetail.aspx?li=38d0274a-1d4f-47a3-bf44-9b37b922c48b

  • Anonymous
    June 14, 2007
    Excellent. I will try it out. Thanks.

  • Anonymous
    June 14, 2007
    Exist some code to translate using vba excel ¿? please help me z3nny130@gmail.com

  • Anonymous
    June 14, 2007
    Sorry I dont know much about VBA Excel. But, here is the same code in VB.Net -    ''' <summary>    ''' Translate Text using Google Translate API's    ''' Google URL - http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}    ''' </summary>    ''' <param name="input">Input string</param>    ''' <param name="languagePair">2 letter Language Pair, delimited by "|".    ''' E.g. "ar|en" language pair means to translate from Arabic to English</param>    ''' <returns>Translated to String</returns>    Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String        Dim url As String = [String].Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)        Dim webClient As New System.Net.WebClient()        webClient.Encoding = System.Text.Encoding.UTF8        Dim result As String = webClient.DownloadString(url)        result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500)        result = result.Substring(0, result.IndexOf("</div"))        Return result    End Function You could create a COM Callable Wrapper around the class which has this method and use that with VBA.

  • Anonymous
    June 27, 2007
    Awesome Tutorial, very useful, thanks!

  • Anonymous
    August 01, 2007
    Does anyone know how to use Google API to translate from classic ASP?   Google Translation is working beautifully on my site for languages beyond English but it translates the values in my dropdown values which are used for searching the database, therefore all searches fail since the database values are in English. I was hoping to find a javascript method of Google API (NOT PHP) that would convert a value I pass it back to English.

  • Anonymous
    August 01, 2007
    For Javascript, you can use XmlHttpRequest in place of WebClient() to call Google's API and perform the translation logic. Check back in a few days I will post sample code to be used from Javascript.

  • Anonymous
    August 01, 2007
    Fantastic!  I really appreciate it.

  • Anonymous
    August 30, 2007
    Me to. Was looking for something in javascript. I hope you would have done with it by now Shahpiyuish. THanks.

  • Anonymous
    September 05, 2007
    The comment has been removed

  • Anonymous
    September 28, 2007
    I trying add this function on my application but first show me a error by Authentification, then add these code lines 'Set the system proxy with valid server address or IP and port.            Dim pry = New System.Net.WebProxy(uri)            'The DefaultCredentials automically get username and password.            pry.Credentials = CredentialCache.DefaultCredentials            GlobalProxySelection.Select = pry Now I get the error 403:Forbidden. Somebody can help me please? Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String        Try            Dim url As String = [String].Format("http://www.google.com/translate_t?hl&text={0}&langpair={1}", input, languagePair)            Dim uri As Uri = New Uri(url)            Dim webCliente As New System.Net.WebClient            'Set the system proxy with valid server address or IP and port.            Dim pry = New System.Net.WebProxy(uri)            'The DefaultCredentials automically get username and password.            pry.Credentials = CredentialCache.DefaultCredentials            GlobalProxySelection.Select = pry            Dim DatosRegresados As System.IO.Stream = webCliente.OpenRead(url)            Dim result As String = DatosRegresados.ToString            result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500)            result = result.Substring(0, result.IndexOf("</div"))            Return result        Catch ex As Exception            MsgBox(ex.Message, MsgBoxStyle.Exclamation)        End Try    End Function

  • Anonymous
    October 12, 2007
    Thanks Piyush. I will try this out.

  • Anonymous
    October 15, 2007
    dont understand how and where will i put the my website name which will be translated .

  • Anonymous
    October 18, 2007
    hi it's good but its give me error like length parameter must be greater

  • Anonymous
    October 18, 2007
    Chirag, Please post your code. Thanks.

  • Anonymous
    October 18, 2007
    ayoub, I don't get your question. This function is used to translate a string and not a webpage. Please give more specifics of what you are trying to accomplish?

  • Anonymous
    October 18, 2007
    hi piyush, Below is my code Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String        Dim url As String = [String].Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)        Dim webClient As New System.Net.WebClient        'webClient. = System.Text.Encoding.UTF8        Dim result As String        'result = webClient.Downloadstring(url)        Dim DatosRegresados As System.IO.Stream = webClient.OpenRead(url)        result = DatosRegresados.ToString        result = result.Substring(result.IndexOf("id=txt") + 22, result.IndexOf("id=txt") + 500)        result = result.Substring(0, result.IndexOf("</div"))        Return result    End Function i am passing this value TranslateText(Me.txt.Text, "en|ar") and code gives me below error Index and length must refer to a location within the string. Parameter name: length

  • Anonymous
    October 18, 2007
    I am using asp.net 1.1 , the article which u gave is not working i coded like this Public Function TranslateText(ByVal input As String="Kishore", ByVal languagePair As String="en|ru") As String        Dim url As String = [String].Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)        Dim webClient As New System.Net.WebClient        'webClient.Encoding = System.Text.Encoding.UTF8        Dim result As Byte() = webClient.DownloadData(url)        Dim result1 As String = System.Text.Encoding.GetEncoding("utf-8").GetString(result)        'result1 = result1.Substring(result1.IndexOf("id=result_box") + 22, result1.IndexOf("id=result_box") + 500)        'result1 = result1.Substring(0, result1.IndexOf("</div"))        Return result1    End Function please give me ur suggestions

  • Anonymous
    November 20, 2007
    Can anyone help me to use this function with excel vba??? please try but without results...

  • Anonymous
    November 21, 2007
    Try here http://www.dailydoseofexcel.com/archives/2004/04/30/translating-text/

  • Anonymous
    November 26, 2007
    I am trying to translate to or from russian but it doesn't translate correctly. I am using vb.net. Does anyone has an idea?

  • Anonymous
    November 29, 2007
    Hi Piyush, Just to let you know that I had implemented it and it helped, thanks allot!

  • Anonymous
    December 01, 2007
    PingBack from http://davidburela.wordpress.com/2007/12/02/google-translation-application/

  • Anonymous
    April 14, 2008
    is there any script that I can add to my future page to be displayed in different languges. tank you

  • Anonymous
    May 16, 2008
    I am developing a website. the company wants to translate the database contents from english to german as well as other text of the website. I am using asp classic for development. Can anyone guide me?

  • Anonymous
    July 09, 2008
    Check out the new improved code at http://get-dugg.com/google-translate-api-code

  • Anonymous
    July 10, 2008
    PingBack from http://habari.dahead.de/vb-net---strings-mit-google-uebersetzen

  • Anonymous
    September 08, 2008
    hi piyush, Below is the code Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String       Dim url As String = [String].Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)       Dim webClient As New System.Net.WebClient       'webClient. = System.Text.Encoding.UTF8       Dim result As String       'result = webClient.Downloadstring(url)       Dim DatosRegresados As System.IO.Stream = webClient.OpenRead(url)       result = DatosRegresados.ToString       result = result.Substring(result.IndexOf("id=txt") + 22, result.IndexOf("id=txt") + 500)       result = result.Substring(0, result.IndexOf("</div"))       Return result   End Function i am passing this value TranslateText(Me.txt.Text, "en|ar") and code gives me below error Index and length must refer to a location within the string. Parameter name: length

  • Anonymous
    September 08, 2008
    Vibhav, My guess is the Result you are getting back is empty or does not have the correct length for indexof

  • Anonymous
    September 08, 2008
    Thanks Piyush, I got the correction....Now it's working fine. I have one more query for you. In TranslateText(Me.txt.Text, "en|ar") > "en|ar" will be sort of hard coded thing. Can we change it Dynamically? On language selection..

  • Anonymous
    December 19, 2008
    I have developed http://translator.vndv.com/ page which uses Google AJAX Language API. I also used Google AJAX Language API to translate the user interface of Online Translation Service to the following languages: English, Arabic, Bulgarian, Chinese, Croatian, Czech, Danish, Dutch, Finnish, French, German, Greek, Hindi, Italian, Japanese, Korean, Norwegian, Polish, Portuguese, Romanian, Russian, Spanish, Swedish.

  • Anonymous
    March 02, 2009
    Here is som code for automation of internet explorer for google translate service. The progam is done in vba. http://vbaexcel.eu/vba-macro-code/google-translate-by-internet-explorer-automation

  • Anonymous
    March 07, 2009
    You can also translate your texts using <a href="http://www.french-translator.org" title="French Translator">French Translator</a> . In any language!

  • Anonymous
    April 05, 2009
    Hi Piyush, Here is my code as per your suggestion. I am getting the following error "Unable to connect to the remote server" However, when I posted the url with relevant values, it did give me a response i.e. page was loaded in browser. Please help


url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);            Uri u = new Uri(url);            System.Net.WebProxy pxy = new System.Net.WebProxy(u);            pxy.Credentials = System.Net.CredentialCache.DefaultCredentials;            //System.Net.GlobalProxySelection.Select = pxy;            System.Net.WebRequest.DefaultWebProxy=pxy;            System.Net.WebClient webClient = new System.Net.WebClient();            webClient.Encoding = System.Text.Encoding.UTF8;            System.IO.Stream iostr = webClient.OpenRead(url);           result = iostr.ToString();            result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500);            result = result.Substring(0, result.IndexOf("</div"));

I am using .NET framework 2.0.

  • Anonymous
    April 06, 2009
    Mayur, Make sure your web.config has the information of your proxy server - <system.net>    <defaultProxy>      <proxy           usesystemdefault="False"           proxyaddress="http://proxyserver:port"           bypassonlocal="True"     />    </defaultProxy>  </system.net>

  • Anonymous
    April 09, 2009
    Hi piyush, I am trying to get a list of strings from a excel sheet and use google translate to translate them to other languages and then populate the results back to the excal sheet. Can you please tell me how to do this? Thanks, karthik

  • Anonymous
    April 16, 2009
    Hi, Did any one know what is the langpair for english to Indic translate.

  • Anonymous
    May 01, 2009
    Hi :) I have tried your function to translate English into Vietnamese. But it doesn't display well. Example: Input text is 'type words you want to translate here...' The result is 'Ki&#7875;u ch&#7919; m b&#7841;n mu&#7889;n d&#7883;ch t&#7841;i &#273;y ...' (The right result must be: 'Kiểu chữ mà bạn muốn dịch tại đây...') I know the charset of the webResponse is 'ISO-8859-1', how can I set it to UTF8 ? Can you give me an advise? Thank you so much. Wish you have fun!

  • Anonymous
    May 12, 2009
    The comment has been removed

  • Anonymous
    May 12, 2009
    Plzzz Can anybdy hlp me it,s urgent

  • Anonymous
    May 17, 2009
    How do I implement the same thing in .Net 1.0 I suppose the methods DownloadString and Encoding are not available in .Net 1.0 Help required for the same.

  • Anonymous
    May 28, 2009
    where i can find webclient namespace in asp.net

  • Anonymous
    May 29, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=piyush-shah-s-blog-translate-your-text-using-google-api-s

  • Anonymous
    June 12, 2009
    This is what worked for me! result = result.Substring(result.IndexOf("id=result_box") + 24, 800);

  • Anonymous
    November 17, 2009
    düğün davetiyesi ve davetiye sözleri

  • Anonymous
    November 18, 2009
    düğün davetiyesi ve davetiye sözleri