我需要在我的C#代码翻译,巴贝尔鱼翻译。到目前为止,我有这个代码..但我总是得到“错误”字符串,正则表达式部分有什么问题,任何人都可以帮助我与这一个PLZ?非常感谢
PS:如果有编码的任何其他方式将是奥凯
C#翻译与巴贝尔鱼翻译器
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;
}
我在我的代码的一部分,它返回“埃罗”这意味着matchecollection是空的... – Grace 2011-05-09 12:44:50
您正在使用代码教程发布于2004年(http://www.codeproject.com/KB/cpp/translation.aspx),很可能babelfish改变了它们发出的html标记,因此你的regex失败了。在那里手动翻译文本,检查html源代码并更改正则表达式以捕获所需的内容。 – Till 2011-05-09 12:51:33
是的即时通讯使用该教程bcoz我没有找到任何其他one..thou我dono如何做id,否则..感谢直到 – Grace 2011-05-09 12:53:47