Welcome to MSDN Blogs Sign in | Join | Help

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

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

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

Comments

# Cosas Interesantes: 10/06/2007

Sunday, June 10, 2007 5:47 AM by Be Geek My Friend

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

# re: Translate your text using Google Api's

Thursday, June 14, 2007 7:04 AM by ScottIsAFool

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

SL

# re: Translate your text using Google Api's

Thursday, June 14, 2007 2:55 PM by ScottIsAFool

# re: Translate your text using Google Api's

Thursday, June 14, 2007 4:30 PM by shahpiyush

Excellent. I will try it out. Thanks.

# re: Translate your text using Google Api's

Thursday, June 14, 2007 7:02 PM by z3nny

Exist some code to translate using vba excel ¿?

please help me

z3nny130@gmail.com

# re: Translate your text using Google Api's

Thursday, June 14, 2007 7:17 PM by shahpiyush

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.

# re: Translate your text using Google Api's

Wednesday, June 27, 2007 5:15 PM by Ryan Nohr

Awesome Tutorial, very useful, thanks!

# re: Translate your text using Google Api's

Wednesday, August 01, 2007 8:51 PM by Dorado

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.

# re: Translate your text using Google Api's

Wednesday, August 01, 2007 8:56 PM by shahpiyush

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.

# re: Translate your text using Google Api's

Wednesday, August 01, 2007 9:41 PM by Dorado

Fantastic!  I really appreciate it.

# re: Translate your text using Google Api's

Friday, August 31, 2007 6:59 AM by Franklin

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

THanks.

# re: Translate your text using Google Api's

Wednesday, September 05, 2007 9:36 AM by Biffity

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

# re: Translate your text using Google Api's

Friday, September 28, 2007 11:47 AM by Peque

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

# re: Translate your text using Google Api's

Friday, October 12, 2007 11:08 PM by Hiren

Thanks Piyush. I will try this out.

# re: Translate your text using Google Api's

Monday, October 15, 2007 6:38 PM by ayoub

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

# re: Translate your text using Google Api's

Thursday, October 18, 2007 9:28 AM by Chirag

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

# re: Translate your text using Google Api's

Thursday, October 18, 2007 1:12 PM by shahpiyush

Chirag,

Please post your code.

Thanks.

# re: Translate your text using Google Api's

Thursday, October 18, 2007 1:14 PM by shahpiyush

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?

# re: Translate your text using Google Api's

Friday, October 19, 2007 3:01 AM by Chirag

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

# re: Translate your text using Google Api's

Friday, October 19, 2007 6:07 AM by Kishore

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

# re: Translate your text using Google Api's

Tuesday, November 20, 2007 7:29 PM by z3nny

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

please try but without results...

# re: Translate your text using Google Api's

Thursday, November 22, 2007 3:34 AM by stedawa

# re: Translate your text using Google Api's

Tuesday, November 27, 2007 7:26 AM by xalnaga

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?

# re: Translate your text using Google Api's

Friday, November 30, 2007 1:05 AM by Hiren

Hi Piyush,

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

# Google translation application &laquo; Burela&#8217;s house-o-blog

Saturday, December 01, 2007 10:27 PM by Google translation application « Burela’s house-o-blog

# re: Translate your text using Google Api's

Tuesday, April 15, 2008 3:48 AM by jacob

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

tank you

# re: Translate your text using Google Api's

Friday, May 16, 2008 9:56 AM by Abadit

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?

# re: Translate your text using Google Api's

Thursday, July 10, 2008 7:31 AM by Rob

Check out the new improved code at

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

# VB .NET - Strings mit Google uebersetzen - daheads blog??

# re: Translate your text using Google Api's

Tuesday, September 09, 2008 1:54 AM by Vibhav

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

# re: Translate your text using Google Api's

Tuesday, September 09, 2008 4:27 AM by shahpiyush

Vibhav,

My guess is the Result you are getting back is empty or does not have the correct length for indexof

# re: Translate your text using Google Api's

Tuesday, September 09, 2008 7:57 AM by Vibhav

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..

# re: Translate your text using Google Api's

Friday, December 19, 2008 9:19 AM by Online Translation Service

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.

# re: Translate your text using Google Api's

Monday, March 02, 2009 4:06 PM by John

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

# French Translator

Sunday, March 08, 2009 6:11 AM by Noir

You can also translate your texts using <a href="http://www.french-translator.org" title="French Translator">French Translator</a> .

In any language!

# re: Translate your text using Google Api's

Monday, April 06, 2009 3:15 AM by Mayur Parmar

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.

# re: Translate your text using Google Api's

Monday, April 06, 2009 12:49 PM by shahpiyush

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>

# re: Translate your text using Google Api's

Thursday, April 09, 2009 3:26 PM by Karthik

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

# re: Indic Language

Friday, April 17, 2009 3:42 AM by sampath

Hi,

Did any one know what is the langpair for english to Indic translate.

# re: Translate your text using Google Api's

Saturday, May 02, 2009 12:32 AM by return

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!

# re: Translate your text using Google Api's

Wednesday, May 13, 2009 5:05 AM by amit d

hi piyush;

Thanks for ur post

i have tried it but error is coming at secondlast line like

Length cannot be less than zero.

Parameter name: length pLzzzz help

my code->

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

   // string url = String.Format("http://translate.google.com/translate_t?hl=en&ie=UTF8#en|hi|amit");

   WebClient webClient = new WebClient();

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

    string result = webClient.DownloadString(url);

  // System.IO.Stream DatosRegresados = webClient.OpenRead(url);

  // string result = DatosRegresados.ToString();

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

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

   return result;

I m passing values->

string res=TranslateText( result_box1 .Text,"en|hi");

# re: Translate your text using Google Api's

Wednesday, May 13, 2009 5:12 AM by amit d

Plzzz Can anybdy hlp me it,s urgent

# re: Translate your text using Google Api's

Monday, May 18, 2009 5:42 AM by Karan

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.

# re: Translate your text using Google Api's

Thursday, May 28, 2009 8:11 AM by yogesh

where i can find webclient namespace in asp.net

# Piyush Shah s Blog Translate your text using Google Api s | Paid Surveys

# re: Translate your text using Google Api's

Friday, June 12, 2009 6:44 PM by Al

This is what worked for me!

result = result.Substring(result.IndexOf("id=result_box") + 24, 800);

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker