使用 babel Fish 翻译器的 C# 翻译器

发布于 2024-11-06 14:46:40 字数 2102 浏览 2 评论 0原文

我需要对我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋叶绚丽 2024-11-13 14:46:40

这就是我最终所做的

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);

Thats what i ended up doing

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);
最单纯的乌龟 2024-11-13 14:46:40

之前的答案可能会有所帮助:使用 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!)

姜生凉生 2024-11-13 14:46:40

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

<div style=padding

不存在。也许有一些引言?

<div style="padding //etc

Your regex is failing because there are no instances of what you are searching for in the downloaded text.

<div style=padding

does not exist. Perhaps some quotes?

<div style="padding //etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文