Grace
Grace

Reputation: 1265

C# translator with babel fish translator

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;



        }

Upvotes: 0

Views: 2451

Answers (3)

Grace
Grace

Reputation: 1265

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

Upvotes: 3

spender
spender

Reputation: 120548

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

Upvotes: 0

Tim
Tim

Reputation: 61

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

Upvotes: 1

Related Questions