C#翻译与babel鱼翻译

时间:2011-05-09 12:40:05

标签: c# .net translation

我需要在我的C#代码中翻译,babel fish translator。到目前为止我有这个代码..但我总是得到“错误”字符串,正则表达式部分有问题,任何人都可以帮助我这一个PLZ?非常感谢
PS:如果有任何其他编码方式将是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;



        }

3 个答案:

答案 0 :(得分: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 :(得分:1)

之前的回答可能有所帮助:Using c# to call google translator。它引用了此codeplex项目:http://languagetranslator.codeplex.com/,它使用Google Translation API。

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

答案 2 :(得分:0)

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

<div style=padding

不存在。也许是一些引用?

<div style="padding //etc