Using Google Translate in C#

C#Google Translate

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

Solution 2 - C#

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();
}

Solution 3 - C#

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";
    }
}

Solution 4 - C#

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();

Solution 5 - C#

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")

Solution 6 - C#

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"

Solution 7 - C#

Google is going to shut the translate API down by the end of 2011, so you should be looking at the alternatives!

Solution 8 - C#

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMax FraiView Question on Stackoverflow
Solution 1 - C#James CampbellView Answer on Stackoverflow
Solution 2 - C#Shane FulmerView Answer on Stackoverflow
Solution 3 - C#Yashar AliabbasiView Answer on Stackoverflow
Solution 4 - C#jebberwockyView Answer on Stackoverflow
Solution 5 - C#Muhammad MehdiView Answer on Stackoverflow
Solution 6 - C#VictorView Answer on Stackoverflow
Solution 7 - C#Ash EldritchView Answer on Stackoverflow
Solution 8 - C#juFoView Answer on Stackoverflow