2011-05-09 87 views
0

我需要在我的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; 



     } 
+0

我在我的代码的一部分,它返回“埃罗”这意味着matchecollection是空的... – Grace 2011-05-09 12:44:50

+1

您正在使用代码教程发布于2004年(http://www.codeproject.com/KB/cpp/translation.aspx),很可能babelfish改变了它们发出的html标记,因此你的regex失败了。在那里手动翻译文本,检查html源代码并更改正则表达式以捕获所需的内容。 – Till 2011-05-09 12:51:33

+0

是的即时通讯使用该教程bcoz我没有找到任何其他one..thou我dono如何做id,否则..感谢直到 – Grace 2011-05-09 12:53:47

回答

3

多数民众赞成我落得这样做

string fromCulture = from.Name; 
      string toCulture = to.Name; 
      string translationMode = string.Concat(fromCulture, "_", toCulture); 

      string url = String.Format("http://babelfish.yahoo.com/translate_txt?lp={0}&tt=urltext&intl=1&doit=done&urltext={1}", translationMode, HttpUtility.UrlEncode(resource)); 
      WebClient webClient = new WebClient(); 
      webClient.Encoding = System.Text.Encoding.Default; 
      string page = webClient.DownloadString(url); 

      int start = page.IndexOf("<div style=\"padding:0.6em;\">") + "<div style=\"padding:0.6em;\">".Length; 
      int finish = page.IndexOf("</div>", start); 
      string retVal = page.Substring(start, finish - start); 
1

这以前的答案可能会帮助:Using c# to call google translator。它引用了这个codeplex项目:http://languagetranslator.codeplex.com/,它使用Google翻译API。

这可能需要一些时间来找到你想要做这工作的代码,但我通常会发现,它是最好的学习方式(至少对我来说!)

+0

我需要翻译与巴贝尔鱼翻译:( – Grace 2011-05-09 12:48:02

+1

你确定它是babelfish.com你需要吗?旧的Alta Vista Babelfish现在在雅虎http://babelfish.yahoo.com/。如果是,你可以尝试问babelfish.com的人,如果他们有一个API可以使用吗?我认为这样的网络抓取是不是真的允许。 – Rup 2011-05-09 13:05:12

+0

是RUP多数民众赞成在我需要:)谢谢很多 – Grace 2011-05-09 14:30:32

0

你的正则表达式是失败的,因为没有您正在下载的文本中搜索的实例。

<div style=padding 

不存在。也许有些引号?

<div style="padding //etc 
+0

仍然是一样的:S – Grace 2011-05-09 12:56:19