[WM][VB][YAHOO 線上文字翻譯]
假如想要將中文句字或是單字翻成英文怎麼辦呢,如果你是六年級生因該還沒忘了以前很夯的無敵cd 翻譯字典吧,查不到的字還要買字詞卡回來給他插一下,時代在進步不用那麼麻煩啦,今天我來告訴大家怎麼做線上翻譯還可以多國喔。利用YAHOO 網頁來查詢http://tw.babelfish.yahoo.com/translate_txt
Step1:開啟vs2008 新增vb 智慧型裝置專案,在表單上產生一個 ComboBox 用來放我們要翻譯語言,二個文字塊翻譯句子是用來輸入文字、轉換用來輸出翻譯好的文字,建立一個翻譯功能表按鍵。
Step2:撰寫程式碼
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Rows.Add("zt_en", "繁體中文至英文")
dt.Rows.Add("zt_zh", "繁體中文至簡體中文")
dt.Rows.Add("zh_en", "簡體中文至英文")
dt.Rows.Add("zh_zt", "簡體中文至繁體中文")
dt.Rows.Add("en_zt", "英文至繁體中文")
dt.Rows.Add("en_zh", "英文至簡體中文")
dt.Rows.Add("en_nl", "英文至荷蘭文")
dt.Rows.Add("en_fr", "英文至法文")
dt.Rows.Add("en_de", "英文至德文")
dt.Rows.Add("en_el", "英文至希臘文")
dt.Rows.Add("en_it", "英文至義大利文")
dt.Rows.Add("en_ja", "英文至日文")
dt.Rows.Add("en_ko", "英文至韓文")
dt.Rows.Add("en_pt", "英文至葡萄牙文")
dt.Rows.Add("en_ru", "英文至俄文")
dt.Rows.Add("en_es", "英文至西班牙文")
dt.Rows.Add("nl_en", "荷蘭文至英文")
dt.Rows.Add("nl_fr", "荷蘭文至法文")
dt.Rows.Add("fr_nl", "法文至荷蘭文")
dt.Rows.Add("fr_en", "法文至英文")
dt.Rows.Add("fr_de", "法文至德文")
dt.Rows.Add("fr_el", "法文至希臘文")
dt.Rows.Add("fr_it", "法文至義大利文")
dt.Rows.Add("fr_pt", "法文至葡萄牙文")
dt.Rows.Add("fr_es", "法文至西班牙文")
dt.Rows.Add("de_en", "德文至英文")
dt.Rows.Add("de_fr", "德文至法文")
dt.Rows.Add("el_en", "希臘文至英文")
dt.Rows.Add("el_fr", "希臘文至法文")
dt.Rows.Add("it_en", "義大利文至英文")
dt.Rows.Add("it_fr", "義大利文至法文")
dt.Rows.Add("ja_en", "日文至英文")
dt.Rows.Add("ko_en", "韓文至英文")
dt.Rows.Add("pt_en", "葡萄牙文至英文")
dt.Rows.Add("pt_fr", "葡萄牙文至法文")
dt.Rows.Add("ru_en", "俄文至英文")
dt.Rows.Add("es_en", "西班牙文至英文")
dt.Rows.Add("es_fr", "西班牙文至法文")
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"
End Sub
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
TextBox2.Text = Translation(TextBox1.Text, ComboBox1.SelectedValue)
End Sub
Public Function Translation(ByVal texts As String, ByVal languages As String) As String
Dim para As String = "more=1&ei=UTF-8&trtext=" & texts & "&lp=" & languages & ""
Dim req As WebRequest = HttpWebRequest.Create("http://tw.babelfish.yahoo.com/translate_txt")
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim encoding As Encoding = encoding.GetEncoding("UTF-8")
Dim bytes As [Byte]() = encoding.GetBytes(para)
req.ContentLength = bytes.Length
Dim ReadStream As Stream = Nothing
ReadStream = req.GetRequestStream()
ReadStream.Write(bytes, 0, bytes.Length)
ReadStream.Close()
Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
If res.StatusDescription.ToLower() <> "ok" Then
MsgBox("無法連接")
End If
Dim dataStream As Stream = res.GetResponseStream()
Dim reader As New StreamReader(dataStream, encoding.GetEncoding(0))
Dim response As String = Nothing
response = reader.ReadToEnd()
reader.Close()
dataStream.Close()
res.Close()
Dim reg As New Regex("<div style=""padding:0.6em;"">(.*?)</div>")
Dim matches As MatchCollection = reg.Matches(response)
If matches.Count <> 1 OrElse matches(0).Groups.Count <> 2 Then
Return ""
End If
Return matches(0).Groups(1).Value
End Function
Dim dt As New DataTable
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Rows.Add("zt_en", "繁體中文至英文")
dt.Rows.Add("zt_zh", "繁體中文至簡體中文")
dt.Rows.Add("zh_en", "簡體中文至英文")
dt.Rows.Add("zh_zt", "簡體中文至繁體中文")
dt.Rows.Add("en_zt", "英文至繁體中文")
dt.Rows.Add("en_zh", "英文至簡體中文")
dt.Rows.Add("en_nl", "英文至荷蘭文")
dt.Rows.Add("en_fr", "英文至法文")
dt.Rows.Add("en_de", "英文至德文")
dt.Rows.Add("en_el", "英文至希臘文")
dt.Rows.Add("en_it", "英文至義大利文")
dt.Rows.Add("en_ja", "英文至日文")
dt.Rows.Add("en_ko", "英文至韓文")
dt.Rows.Add("en_pt", "英文至葡萄牙文")
dt.Rows.Add("en_ru", "英文至俄文")
dt.Rows.Add("en_es", "英文至西班牙文")
dt.Rows.Add("nl_en", "荷蘭文至英文")
dt.Rows.Add("nl_fr", "荷蘭文至法文")
dt.Rows.Add("fr_nl", "法文至荷蘭文")
dt.Rows.Add("fr_en", "法文至英文")
dt.Rows.Add("fr_de", "法文至德文")
dt.Rows.Add("fr_el", "法文至希臘文")
dt.Rows.Add("fr_it", "法文至義大利文")
dt.Rows.Add("fr_pt", "法文至葡萄牙文")
dt.Rows.Add("fr_es", "法文至西班牙文")
dt.Rows.Add("de_en", "德文至英文")
dt.Rows.Add("de_fr", "德文至法文")
dt.Rows.Add("el_en", "希臘文至英文")
dt.Rows.Add("el_fr", "希臘文至法文")
dt.Rows.Add("it_en", "義大利文至英文")
dt.Rows.Add("it_fr", "義大利文至法文")
dt.Rows.Add("ja_en", "日文至英文")
dt.Rows.Add("ko_en", "韓文至英文")
dt.Rows.Add("pt_en", "葡萄牙文至英文")
dt.Rows.Add("pt_fr", "葡萄牙文至法文")
dt.Rows.Add("ru_en", "俄文至英文")
dt.Rows.Add("es_en", "西班牙文至英文")
dt.Rows.Add("es_fr", "西班牙文至法文")
Me.ComboBox1.DataSource = dt
Me.ComboBox1.DisplayMember = "Name"
Me.ComboBox1.ValueMember = "ID"
End Sub
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
TextBox2.Text = Translation(TextBox1.Text, ComboBox1.SelectedValue)
End Sub
Public Function Translation(ByVal texts As String, ByVal languages As String) As String
Dim para As String = "more=1&ei=UTF-8&trtext=" & texts & "&lp=" & languages & ""
Dim req As WebRequest = HttpWebRequest.Create("http://tw.babelfish.yahoo.com/translate_txt")
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim encoding As Encoding = encoding.GetEncoding("UTF-8")
Dim bytes As [Byte]() = encoding.GetBytes(para)
req.ContentLength = bytes.Length
Dim ReadStream As Stream = Nothing
ReadStream = req.GetRequestStream()
ReadStream.Write(bytes, 0, bytes.Length)
ReadStream.Close()
Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
If res.StatusDescription.ToLower() <> "ok" Then
MsgBox("無法連接")
End If
Dim dataStream As Stream = res.GetResponseStream()
Dim reader As New StreamReader(dataStream, encoding.GetEncoding(0))
Dim response As String = Nothing
response = reader.ReadToEnd()
reader.Close()
dataStream.Close()
res.Close()
Dim reg As New Regex("<div style=""padding:0.6em;"">(.*?)</div>")
Dim matches As MatchCollection = reg.Matches(response)
If matches.Count <> 1 OrElse matches(0).Groups.Count <> 2 Then
Return ""
End If
Return matches(0).Groups(1).Value
End Function
Step3:在vs2008編緝器上的功能表按下偵錯\開始偵錯開始測試我們會應用程式
Step4:先選擇繁體中文至英文,在翻譯句子文字方塊裡面輸入你好二個字,接著在按下翻譯按鍵進行轉換。
Step5: 源碼下載