这个核心代码代码很简单,只有关键的几句话。1 l+ A7 v/ {* N
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
. S! V" E0 o7 x+ M) f8 o* i今后很多翻译工具都会在这个基础上来完成。
( R* w2 W" c3 W3 s; V9 P, _4 r
' B. o' m. l" C% \. F& c/ v0 Z% s% |7 }7 n" j- a" J
代码# g. l1 `; j* i
Public Class Main
. @7 X. b' Q* T: d* A4 t5 y) V: ^ Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String' p3 W9 |" C( J5 k6 M
Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair) n# R. Q, l, a# ^
Dim webClient As WebClient = New WebClient()
1 J$ v8 u, S9 }: I ''webClient.Encoding = System.Text.Encoding.UTF8
8 h: p& W7 Q% C$ A4 O$ l: O Dim result As String = webClient.DownloadString(url)5 l9 | p) W, [
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
4 G3 |) q7 j6 ~3 _+ } result = result.Substring(0, result.IndexOf("""")); _8 C+ l0 j3 H: Q. J
Return result5 q0 R( ~; C; f2 I/ j9 @
End Function
4 E4 ]% `! d4 ? Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click3 k0 ~: g" i+ q3 X
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
) g3 @, K2 F- T( t End Sub# ]$ {% x4 z$ C: a+ t2 d# I9 Y9 i- s
End Class
5 j! X* \' B. u- U4 k* ~
" Z) a/ T2 d* u* l抓图
; b" p B0 x) c. s+ [ |