Calling Google Translate From ASP.NET Using Post

Calling Google Translate from ASP.NET using POST for longer documents from your code behind.


The Google Translate API is good for performing fairly complex translations, and while it is not perfect, it will at least give your readers a basic understanding of what you are trying to convey.

While the document is very good at explaining what happens when you use Java Script which can translate about 500 characters, there is very little for when you need to translate bigger documents.

When using POST you can increase this to 5000 characters, so we developed our own code to send a post request to the Google API and then receive the translation.

First the code, for those who just want that.


Protected Function GetTranslation(ByVal key As String, ByVal source As String, ByVal target As String, ByVal Text As String) As String
  Dim TranslatedString As String = ""
  Text = "q=" + Text
  Dim TranslateRequest As New Uri(String.Format("{0}&source={1}&target={2}&format=html", key, source, target))
  Dim Req As WebRequest = WebRequest.Create(TranslateRequest)
  Req.Method = "POST"
  Req.Headers.Add("X-https-Method-Override", "GET")
  Req.ContentType = "application/x-www-form-urlencoded"
  Using wriream As Stream = Req.GetRequestStream()
  Dim encoding As New UTF8Encoding()
  Dim bytes As Byte() = encoding.GetBytes(Text)
  wriream.Write(bytes, 0, bytes.Length)
  End Using
  Dim ReturnStr As String
  Using sr = New StreamReader(Req.GetResponse.GetResponseStream)
  ReturnStr = sr.ReadToEnd()
  End Using
  Dim Reader As New JavaScriptSerializer
  Dim TranslateJSON As Dictionary(Of String, Object) = Reader.DeserializeObject(ReturnStr)
  Dim TranslateData As Dictionary(Of String, Object)
  If TranslateJSON.ContainsKey("data") Then
  TranslateData = TranslateJSON("data")
  If TranslateData.ContainsKey("translations") Then
  For Each pair In TranslateData.Item("translations")(0)
  TranslatedString = pair.Value.ToString()
   End If
  End If
  Return TranslatedString
End Function

And now for a brief explanation. The function requires four inputs, these are your key, language from, language to and the text you want to translate.

We then declare a return string, create a request URL string which is quickly parsed into a new web request (Req).

We then set the request type, content type, and most importantly add a header to override the get method.

Once this is done, we then send the data as a stream to Google (wriream).

Now we declare a return string (ReturnStr) to hold the returned JSON from Google, and read the response string into it.

Next step is creating a JavaScriptSerializer, this bit was probably the most confusing, as this is the weakest area of my development skills. What this last section does is pull out each section of text until it gets to the area we want, and the sets our return text as the value returned by Google. This may not be the most elaborate code in the world, so if you work out a way of tidying it up then let me know.

You could quite easily put this code in a shared class and re-use it, however there was only one section of the site that we needed this on, so it was built into the page. 

The example below populates one AJAX Toolkit text editor with the contents from another. Please note the double decoding (from the editor and Google), and has two text boxes denoting the language to and from.


Protected Sub TranslateDoc_Click(sender As Object, e As EventArgsHandles TranslateDoc.Click
  Dim key As String = "Your Key"
  Dim source As String = LanguageBase.SelectedItem.Text.ToString
  Dim target As String = LanguageTrans.SelectedItem.Text.ToString
  Dim PageText As String = httpsUtility.HtmlDecode(ContentText.Content)
  ContentTextTran.Content = httpsUtility.HtmlDecode(GetTranslation(key, source, target, PageText))
  Str.Text = "Translated"
  Str.Text = key + "," + source + "," + target + ""
  End Try
Ousia Logo