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.
8 Answers
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#
Translation Web Service in C#
http://www.codeproject.com/KB/cpp/translation.aspx
Using Google's Translation API from .NET

- 170,088
- 45
- 397
- 571

- 5,057
- 2
- 35
- 54
-
7Last link seems dead. – Soner Gönül Mar 11 '18 at 13:37
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();
}

- 7,510
- 6
- 35
- 43
I found this code works for me:
public String Translate(String word)
{
var toLanguage = "en";//English
var fromLanguage = "de";//Deutsch
var url = $"https://translate.googleapis.com/translate_a/single?client=gtx&sl={fromLanguage}&tl={toLanguage}&dt=t&q={HttpUtility.UrlEncode(word)}";
var webClient = new WebClient
{
Encoding = System.Text.Encoding.UTF8
};
var result = webClient.DownloadString(url);
try
{
result = result.Substring(4, result.IndexOf("\"", 4, StringComparison.Ordinal) - 4);
return result;
}
catch
{
return "Error";
}
}

- 2,663
- 1
- 23
- 35
-
1This code works perfect but for Arabic text you just need to add this webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); – ASalameh Dec 20 '18 at 15:58
-
2Not entirely perfect though, the API will bring error 503 after heavier usage. – BigBang1112 Feb 10 '19 at 18:28
Google Translate Kit, an open source library http://ggltranslate.codeplex.com/
Translator gt = new Translator();
/*using cache*/
DemoWriter dw = new DemoWriter();
gt.KeyGen = new SimpleKeyGen();
gt.CacheManager = new SimleCacheManager();
gt.Writer = dw;
Translator.TranslatedPost post = gt.GetTranslatedPost("Hello world", LanguageConst.ENGLISH, LanguageConst.CHINESE);
Translator.TranslatedPost post2 = gt.GetTranslatedPost("I'm Jeff", LanguageConst.ENGLISH, LanguageConst.CHINESE);
this.result.InnerHtml = "<p>" + post.text +post2.text+ "</p>";
dw.WriteToFile();

- 1,123
- 3
- 11
- 24
When I used above code.It show me translated text as question mark like (???????).Then I convert from WebClient to HttpClient then I got a accurate result.So you can used code like this.
public static 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);
HttpClient httpClient = new HttpClient();
string result = httpClient.GetStringAsync(url).Result;
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();
}
Then you Call a function like.You put first two letter of any language pair.
From English(en) To Urdu(ur).
TranslateText(line, "en|ur")

- 23,309
- 10
- 44
- 76

- 432
- 4
- 12
-
2
-
Hi is this solution still working? In my case its not finding the " – Robert Smith Sep 12 '19 at 15:10
-
1This one works now: https://www.codeproject.com/Tips/5247661/Google-Translate-API-Usage-in-Csharp – Robert Smith Nov 26 '19 at 14:03
Here is my slighly different code, solving also the encoding issue:
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.Default;
string result = webClient.DownloadString(url);
result = result.Substring(result.IndexOf("TRANSLATED_TEXT"));
result = result.Substring(result.IndexOf("'")+1);
result = result.Substring(0, result.IndexOf("'"));
return result;
}
Example of the function call:
var input_language = "en";
var output_language = "es";
var result = TranslateText("Hello", input_language + "|" + output_language);
The result will be "Hola"

- 893
- 1
- 10
- 25
-
Not works. says `System.Net.WebException: The remote server returned an error: (404) Not Found.` – Gray Programmerz Apr 10 '21 at 18:42
If you want to translate your resources, just download MAT (Multilingual App Toolkit) for Visual Studio. https://marketplace.visualstudio.com/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308 This is the way to go to translate your projects in Visual Studio. https://blogs.msdn.microsoft.com/matdev/

- 17,849
- 10
- 105
- 142
Google is going to shut the translate API down by the end of 2011, so you should be looking at the alternatives!

- 1,504
- 10
- 13
-
6There is a paid for v2 version which has not been discontinued. – Christopher Edwards May 13 '12 at 12:34