在 C# 中使用谷歌翻译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2246017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Google Translate in C#
提问by Max Frai
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.
我必须使用 Google 的翻译服务翻译一些文本。我发现的所有代码都不起作用。我想是因为他们改变了他们的服务。如果有人有工作代码,我会很高兴。
采纳答案by James Campbell
See if this works for you
看看这是否适合你
google-language-api-for-dotnet
google-language-api-for-dotnet
http://code.google.com/p/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
http://www.codeproject.com/KB/IP/GoogleTranslator.aspx
Translate your text using Google Api's
使用 Google Api 翻译您的文本
http://blogs.msdn.com/shahpiyush/archive/2007/06/09/3188246.aspx
http://blogs.msdn.com/shahpiyush/archive/2007/06/09/3188246.aspx
Calling Google Ajax Language API for Translation and Language Detection from C#
从 C# 调用 Google Ajax 语言 API 进行翻译和语言检测
Translation Web Service in C#
C# 中的翻译 Web 服务
http://www.codeproject.com/KB/cpp/translation.aspx
http://www.codeproject.com/KB/cpp/translation.aspx
Using Google's Translation API from .NET
从 .NET 使用 Google 的翻译 API
回答by Shane Fulmer
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.
第一个代码示例不起作用的原因是页面布局发生了变化。根据该页面上的警告:“翻译的字符串由靠近底部的 RegEx 获取。这当然可能会改变,您必须保持最新状态。” 我认为这现在应该有效,至少在他们再次更改页面之前。
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();
}
回答by jebberwocky
Google Translate Kit, an open source library http://ggltranslate.codeplex.com/
Google Translate Kit,一个开源库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();
回答by Ash Eldritch
Google is going to shut the translate API down by the end of 2011, so you should be looking at the alternatives!
Google 将在 2011 年底关闭翻译 API,因此您应该寻找替代方案!
回答by juFo
If you want to translate your resources, just download MAT (Multilingual App Toolkit) for Visual Studio. https://marketplace.visualstudio.com/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308This is the way to go to translate your projects in Visual Studio. https://blogs.msdn.microsoft.com/matdev/
如果您想翻译您的资源,只需下载 Visual Studio 的 MAT(多语言应用程序工具包)。https://marketplace.visualstudio.com/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308这是在 Visual Studio 中翻译项目的方法。 https://blogs.msdn.microsoft.com/matdev/
回答by Muhammad Mehdi
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.
当我使用上面的代码时。它向我显示翻译文本为问号(???????)。然后我从 WebClient 转换为 HttpClient 然后我得到了准确的结果。所以你可以使用这样的代码。
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).
从英语(en)到乌尔都语(ur)。
TranslateText(line, "en|ur")
回答by Yashar Aliabbasi
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";
}
}
回答by Victor
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"
结果将是“Hola”