[WM][C#][Google線上文字翻譯]

  • 7781
  • 0
  • 2009-08-12

[WM][C#][Google線上文字翻譯]

繼 YAHOO  翻譯後因學員要求我在上一版 Google 裡面比較多語言的翻譯,做法如下:

 

Step1:開啟vs2008 新增 c# 智慧型裝置專案,在表單上產生二個 ComboBox 用來放我們要翻譯語言第一個為現在翻譯句子所使用語言、第二個為要把翻譯句子翻成什麼樣的語言,二個文字塊翻譯句子是用來輸入文字、轉換用來輸出翻譯好的文字,建立一個翻譯功能表按鍵。

image

 

Step2:撰寫程式碼 

public Form1()
        {
            InitializeComponent();
            DataTable dt = new DataTable();
            dt.Columns.Add("ID");
            dt.Columns.Add("Name");
            dt.Rows.Add("en", "英文");
            dt.Rows.Add("tr", "土耳其文");
            dt.Rows.Add("zh-TW", "中文(繁體)");
            dt.Rows.Add("zh-CN", "中文(簡體)");
            dt.Rows.Add("da", "丹麥文");
            dt.Rows.Add("ja", "日文");
            dt.Rows.Add("lt", "立陶宛文");
            dt.Rows.Add("hu", "匈牙利文");
            dt.Rows.Add("id", "印尼文");
            dt.Rows.Add("hi", "印度文");
            dt.Rows.Add("es", "西班牙文");
            dt.Rows.Add("ca", "西班牙加泰羅尼亞文");
            dt.Rows.Add("hr", "克羅地亞文");
            dt.Rows.Add("iw", "希伯來文");
            dt.Rows.Add("el", "希臘文");
            dt.Rows.Add("lv", "拉脫維亞文");
            dt.Rows.Add("pl", "波蘭文");
            dt.Rows.Add("fr", "法文");
            dt.Rows.Add("fi", "芬蘭文");
            dt.Rows.Add("ar", "阿拉伯文");
            dt.Rows.Add("sq", "阿爾巴尼亞文");
            dt.Rows.Add("bg", "保加利亞文");
            dt.Rows.Add("ru", "俄文");
            dt.Rows.Add("en", "英文");
            dt.Rows.Add("no", "挪威文");
            dt.Rows.Add("th", "泰文");
            dt.Rows.Add("uk", "烏克蘭文");
            dt.Rows.Add("mt", "馬耳他文");
            dt.Rows.Add("cs", "捷克文");
            dt.Rows.Add("nl", "荷蘭文");
            dt.Rows.Add("sl", "斯拉維尼亞文");
            dt.Rows.Add("sk", "斯洛伐克文");
            dt.Rows.Add("tl", "菲律賓文");
            dt.Rows.Add("vi", "越南文");
            dt.Rows.Add("sr", "塞爾維亞文");
            dt.Rows.Add("et", "愛沙尼亞文");
            dt.Rows.Add("gl", "愛爾蘭的蓋爾文");
            dt.Rows.Add("sv", "瑞典文");
            dt.Rows.Add("it", "義大利文");
            dt.Rows.Add("pt", "葡萄牙文");
            dt.Rows.Add("de", "德文");
            dt.Rows.Add("ko", "韓文");
            dt.Rows.Add("ro", "羅馬尼亞文");
            this.ComboBox1.DataSource = dt;
            this.ComboBox1.DisplayMember = "Name";
            this.ComboBox1.ValueMember = "ID";
            this.ComboBox2.DataSource = dt.Copy();
            this.ComboBox2.DisplayMember = "Name";
            this.ComboBox2.ValueMember = "ID";

        }


        private void menuItem1_Click(object sender, EventArgs e)
        {
            String Trans=ComboBox1.SelectedValue.ToString() + "|" + ComboBox2.SelectedValue.ToString();
            TextBox2.Text = TranslateText(TextBox1.Text, Trans);
        }

        private string TranslateText(string texts, string languages)
        {
            string para = "&text=" + texts + "&langpair=" + languages;
            WebRequest req = HttpWebRequest.Create(@"http://translate.google.com/translate_t?hl=zh-tw&ie=utf8");
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            Encoding encoding = Encoding.GetEncoding("UTF-8");
            Byte[] bytes = encoding.GetBytes(para);
            req.ContentLength = bytes.Length;
            Stream stream = null;
            stream = req.GetRequestStream();
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();

            if (res.StatusDescription.ToLower() != "ok")
            {
                MessageBox.Show("無法連結");
            }

            StreamReader reader = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding(0));
            string response = null;
            response = reader.ReadToEnd();
            reader.Close();
            res.Close();
            string content = response;
            string s = "<div id=result_box dir=\"ltr\">";
            int start = content.IndexOf(s);
            int end = content.IndexOf("</div>", start);
            content = content.Substring(start + s.Length, end - start - s.Length);
            return content.Replace("&nbsp;", " ").Replace("&quot;", "\"").Replace("&gt;", ">").Replace("&lt;", "<").Replace("<br>", "\r\n").Replace("&#160;", " ").Replace("&#39;", "'");
        }

      
    }

 

 

Step3:在vs2008編緝器上的功能表按下偵錯\開始偵錯開始測試我們會應用程式

Step4:先選擇現在語言中文(繁體)接著在選翻譯語言英文,在翻譯句子文字方塊裡面輸入早安二個字,接著在按下翻譯按鍵進行轉換。

image

 Step5: 源碼下載

 

參考 http://www.cnblogs.com/moses/archive/2008/04/11/1148735.html