使用 babel Fish 翻译器的 C# 翻译器
我需要对我的 C# 代码进行翻译,即 babel Fish 翻译器。到目前为止,我有这段代码..但我总是收到“错误”字符串,正则表达式部分有问题,任何人都可以帮我解决这个问题吗?多谢
PS:如果有其他编码方式也可以
public string Translate(string resource, System.Globalization.CultureInfo from, System.Globalization.CultureInfo to)
{
string[] VALIDTRANSLATIONMODES = new string[]
{"en_zh", "en_fr", "en_de", "en_it", "en_ja", "en_ko", "en_pt", "en_es",
"zh_en", "fr_en", "fr_de", "de_en", "de_fr", "it_en", "ja_en", "ko_en",
"pt_en", "ru_en", "es_en"};
Uri uri = new Uri("http://www.babelfish.com");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
//request.Referer = BABELFISHREFERER;
string postsourcedata;
string translationmode = "en_fr";
postsourcedata = "lp=" + translationmode +
"&tt=urltext&intl=1&doit=done&urltext=" +
HttpUtility.UrlEncode(resource);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postsourcedata.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
Stream writeStream = request.GetRequestStream();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postsourcedata);
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
string page = readStream.ReadToEnd();
Regex reg = new Regex(@"<div style=padding:10px; lang=..>(.*?)</div>");
MatchCollection matches = reg.Matches(page);
if (matches.Count != 1 || matches[0].Groups.Count != 2)
{
return "erro";
}
return matches[0].Groups[1].Value;
}
I need a translation in my C# code, babel fish translator. So far I have this code..but I always get the "error" string, something wrong with the regex part, can anyone help me with this one plz ?? thanks alot
PS: IF there's any other way of coding will be oke
public string Translate(string resource, System.Globalization.CultureInfo from, System.Globalization.CultureInfo to)
{
string[] VALIDTRANSLATIONMODES = new string[]
{"en_zh", "en_fr", "en_de", "en_it", "en_ja", "en_ko", "en_pt", "en_es",
"zh_en", "fr_en", "fr_de", "de_en", "de_fr", "it_en", "ja_en", "ko_en",
"pt_en", "ru_en", "es_en"};
Uri uri = new Uri("http://www.babelfish.com");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
//request.Referer = BABELFISHREFERER;
string postsourcedata;
string translationmode = "en_fr";
postsourcedata = "lp=" + translationmode +
"&tt=urltext&intl=1&doit=done&urltext=" +
HttpUtility.UrlEncode(resource);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postsourcedata.Length;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
Stream writeStream = request.GetRequestStream();
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(postsourcedata);
writeStream.Write(bytes, 0, bytes.Length);
writeStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
string page = readStream.ReadToEnd();
Regex reg = new Regex(@"<div style=padding:10px; lang=..>(.*?)</div>");
MatchCollection matches = reg.Matches(page);
if (matches.Count != 1 || matches[0].Groups.Count != 2)
{
return "erro";
}
return matches[0].Groups[1].Value;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我最终所做的
Thats what i ended up doing
之前的答案可能会有所帮助:使用 c# 调用 google 翻译。它引用了此 codeplex 项目:http://languagetranslator.codeplex.com/,该项目使用 Google Translation API。
可能需要一些时间才能找到完成您想要的工作的代码,但我通常发现这是最好的学习方式(至少对我来说!)
This previous answer might help: Using c# to call google translator. It references this codeplex project: http://languagetranslator.codeplex.com/ which uses the Google Translation API.
It might take a few moments to find the code which does the job you want but I usually find that it's the best way to learn (for me at least!)
您的正则表达式失败,因为在下载的文本中没有您要搜索的内容的实例。
不存在。也许有一些引言?
Your regex is failing because there are no instances of what you are searching for in the downloaded text.
does not exist. Perhaps some quotes?