Using Google Translate in C# [closed]

I have to translate some text with Google's translate service. All code I've found doesn't work. I think because they have changed their service. If someone has working code, I would be very glad.


Solution 1:

See if this works for you

google-language-api-for-dotnet

http://code.google.com/p/google-language-api-for-dotnet/

Google Translator

http://www.codeproject.com/KB/IP/GoogleTranslator.aspx

Translate your text using Google Api's

http://blogs.msdn.com/shahpiyush/archive/2007/06/09/3188246.aspx

Calling Google Ajax Language API for Translation and Language Detection from C#

http://www.esotericdelights.com/post/2008/11/Calling-Google-Ajax-Language-API-for-Translation-and-Language-Detection-from-C.aspx

Translation Web Service in C#

http://www.codeproject.com/KB/cpp/translation.aspx

Using Google's Translation API from .NET

http://www.reimers.dk/blogs/jacob_reimers_weblog/archive/2008/06/18/using-google-s-translation-api-from-net.aspx

Solution 2:

The reason the first code sample doesn't work is because the layout of the page changed. As per the warning on that page: "The translated string is fetched by the RegEx close to the bottom. This could of course change, and you have to keep it up to date." I think this should work for now, at least until they change the page again.


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("<span title=\"") + "<span title=\"".Length);
    result = result.Substring(result.IndexOf(">") + 1);
    result = result.Substring(0, result.IndexOf("</span>"));
    return result.Trim();
}