Welcome to MSDN Blogs Sign in | Join | Help

Piyush Shah's Blog

Software Development Engineer @ MSDN & Technet
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 - http://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 - 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 string TranslateText(

    string input,

    string languagePair)

{

    string url = String.Format("http://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

Posted: Saturday, June 09, 2007 8:11 PM by shahpiyush
Filed under: , ,

Comments

Be Geek My Friend said:

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

# June 10, 2007 5:47 AM

ScottIsAFool said:

Very nice :) Have just created a plugin for Live Writer that uses this :)

SL

# June 14, 2007 7:04 AM

ScottIsAFool said:

# June 14, 2007 2:55 PM

shahpiyush said:

Excellent. I will try it out. Thanks.

# June 14, 2007 4:30 PM

z3nny said:

Exist some code to translate using vba excel ¿?

please help me

z3nny130@gmail.com

# June 14, 2007 7:02 PM

shahpiyush said:

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.

# June 14, 2007 7:17 PM

Ryan Nohr said:

Awesome Tutorial, very useful, thanks!

# June 27, 2007 5:15 PM

Dorado said:

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.

# August 1, 2007 8:51 PM

shahpiyush said:

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.

# August 1, 2007 8:56 PM

Dorado said:

Fantastic!  I really appreciate it.

# August 1, 2007 9:41 PM

Franklin said:

Me to. Was looking for something in javascript. I hope you would have done with it by now Shahpiyuish.

THanks.

# August 31, 2007 6:59 AM

Biffity said:

Google do block access after a few attempts so you'll have to put some clever tricks in to stop this happening.

# September 5, 2007 9:36 AM

Peque said:

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

# September 28, 2007 11:47 AM

Hiren said:

Thanks Piyush. I will try this out.

# October 12, 2007 11:08 PM

ayoub said:

dont understand how and where will i put the my website name which will be translated .

# October 15, 2007 6:38 PM

Chirag said:

hi it's good but its give me error like length parameter must be greater

# October 18, 2007 9:28 AM

shahpiyush said:

Chirag,

Please post your code.

Thanks.

# October 18, 2007 1:12 PM

shahpiyush said:

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?

# October 18, 2007 1:14 PM

Chirag said:

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

# October 19, 2007 3:01 AM

Kishore said:

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

# October 19, 2007 6:07 AM

z3nny said:

Can anyone help me to use this function with excel vba???

please try but without results...

# November 20, 2007 7:29 PM

xalnaga said:

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?

# November 27, 2007 7:26 AM

Hiren said:

Hi Piyush,

Just to let you know that I had implemented it and it helped, thanks allot!

# November 30, 2007 1:05 AM

jacob said:

is there any script that I can add to my future page to be displayed in different languges.

tank you

# April 15, 2008 3:48 AM

Abadit said:

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?

# May 16, 2008 9:56 AM

Rob said:

Check out the new improved code at

http://get-dugg.com/google-translate-api-code

# July 10, 2008 7:31 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker