Pages

Wednesday, August 25, 2010

Google Translate without API

Google translate is a great tool, and every one knows it well. [http://translate.google.com]

Here is how you can use it in .Net programatically without using Google API:



  Function Translate(ByVal WordToTranslate As StringAs String
    Dim wc As New System.Net.WebClient
    wc.Proxy.Credentials = Net.CredentialCache.DefaultCredentials
    Dim url As String = String.Format("http://www.google.com/" & _
                        "translate_t?hl=en&ie=UTF8&text={0}&" & _
                        "langpair=en|de"WordToTranslate)
    Dim result As String = wc.DownloadString(url)
    result = result.Substring(result.IndexOf("id=result_box") + 22)
    result = result.Substring(0, result.IndexOf("</div"))
    result = result.Substring(result.IndexOf(">") + 1)
    While result.IndexOf("<") = 0
      result = result.Substring(result.IndexOf("<"))
      result = result.Substring(result.IndexOf(">") + 1)
    End While
     TranslatedOutput As String = result.Substring(0, result.IndexOf("<"))

    Return TranslatedOutput
  End Function



This code translates English - German. What you have to do is -  just change the highlighted langpair or change the code to pass it as parameter.

Here is the list of language codes.
  • Arabic – ar
  • Bulgarian – bg
  • Chinese (Simplified) – zh-CN
  • Chinese (Traditional) – zh-TW (only available as a destination language)
  • Croatian – hr
  • Czech – cs
  • Danish – da
  • Dutch – nl
  • English – en
  • Finnish – fi
  • French – fr
  • German – de
  • Greek – el
  • Hindi – hi
  • Italian – it
  • Japanese – ja
  • Korean – ko
  • Norweigan – no
  • Polish – pl
  • Portuguese – pt
  • Romanian – ro
  • Russian – ru
  • Spanish – es
  • Swedish – sv

No comments: