John Herrell's Developer Portfolio

Translating Text from Language A to Language B

In the below code I use Google's Translation to translate from one language to the required language. One could easily change the desired culture to determine what language they speak, but I did not for this example.

Code Home Page                               Visit Google Translate

Translate Away

 

Translate from: Translate into:

Languages available for translation:
Afrikaans
Albanian
Arabic
Belarusian
Bulgarian
Catalan
Chinese
Croatian
Czech
Danish
Dutch
English
Estonian
Filipino
Finnish
French
Galician
German
Greek
Hebrew
Hindi
Hungarian
Icelandic
Indonesian
Irish
Italian
Japanese
Korean
Latvian
Lithuanian
Macedonian
Malay
Maltese
Norwegian
Persian
Polish
Portuguese
Romanian
Russian
Serbian
Slovak
Slovenian
Spanish
Swahili
Swedish
Thai
Turkish
Ukrainian
Vietnamese
Welsh
Yiddish

 

What makes the code work

The below code represents the button click which begins the process of converting the text from one language to another.

protected void btnTranslate_Click(object sender, EventArgs e)
        {
            if ((ddlTranslateFromSelect.Value == "auto") || (ddlTranslateFromSelect.Value == "separator"))
            {
                return;
            }
 
            if ((ddlTranslateToSelect.Value == "auto") || (ddlTranslateToSelect.Value == "separator"))
            {
                return;
            }
 
            string translated = TranslateToDesiredLanguage(this.txtStringToTranslate.Text, 
            ddlTranslateFromSelect.Value.ToString(), ddlTranslateToSelect.Value.ToString());
 
            if (!string.IsNullOrEmpty(translated))
            {
                lblTranslatedText.Text = "<h4>Translated Text</h4><p>" + translated + "</p>";
                lblTranslatedText.Visible = true;
            }
        }

 

This function takes the inputs, the text to be converted, the input language, and the output language, and passes it to the Google Translator via a HttpWebRequest. We take the output stream response and clean it up before returning it to the calling method.

private string TranslateToDesiredLanguage(string text, 
    string languageFromChoice, string languageToChoice){
 
    string translatedText = null;
 
    HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(
        "http://translate.google.com/translate_s?hl=en&clss=&q=" +
        text.Replace(" ", "+") + "&tq=&sl=" + 
        languageFromChoice + "&tl=" + languageToChoice);
 
        HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
 
        StreamReader sr = new StreamReader(response.GetResponseStream());
 
        string htmlOutputFromGoogle = sr.ReadToEnd();
 
        int posToStringMatch = htmlOutputFromGoogle.IndexOf("<span id=otq><b>");
 
        string strOutputWithHtml = htmlOutputFromGoogle.Substring(posToStringMatch);
 
        int posToMatchEndHtml = strOutputWithHtml.IndexOf("</b>");
 
        string strTranslatedWithStartHtml = strOutputWithHtml.Substring(0, posToMatchEndHtml);
 
        translatedText = strTranslatedWithStartHtml.Replace("<span id=otq><b>", "");
            
        return translatedText;
 
}