Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Translate C#

I am working on a Natural Language Processing program in which I am trying to implement Google Translate. While looking for ways to implement Google translate in Assembly I came across the following code segment:

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

    using (WebClient webClient = new WebClient())
    {
        webClient.Encoding = encoding;
        result = webClient.DownloadString(url);
    }

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(result);
    return doc.DocumentNode.SelectSingleNode("//textarea[@name='utrans']").InnerText;
}

I am relatively new to C#, I have mainly used Java, and I unclear on the implicit parameters for

public static string Translate(string input, string languagePair, Encoding encoding)

When I look in the C# API for Encoder, there were examples as to how to use the Encoding class: (link: http://msdn.microsoft.com/en-us/library/h5y3703w(v=vs.71).aspx)

Byte[] thirdcharNoFlush = new Byte[encoder.GetByteCount(chars, 2, 1, bFlushState)];
    encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState);

What should I input in my parameters in order to translate a phrase, such as "How are you?", into Spanish using Google Translate. Any help on this matter would be greatly appreciated!

like image 254
John Smith Avatar asked Sep 21 '12 14:09

John Smith


People also ask

Is Google Translate fake?

Google Translate is free, fast, and pretty accurate. Thanks to its massive database, the software can deliver decent translations that can help you get the main idea of a text.

What happened Google Translate?

However, as of January 2019, Google has stopped providing access to new website translator widgets. In addition, the Google Translate widget (which Google calls “Google Translate's Website Translator”) is phasing out on websites where it is active now (as shown below).

What is your name in Korean Google?

What is your name? 이름이 뭐야?

What is super in French?

super, (chouette)


1 Answers

This should work:

var result = Translate("How are you?", "es|en", Encoding.UTF8);
like image 128
cdhowie Avatar answered Oct 16 '22 17:10

cdhowie