这个核心代码代码很简单,只有关键的几句话。2 Q0 s \2 f% w
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。2 H7 m4 Z V9 X# i% ~
今后很多翻译工具都会在这个基础上来完成。# R! o( P& W% \) f* |# I
" Z) U6 T2 s( b. K/ s$ o9 G0 S
8 ^# b/ Y% d* e, O" l I" m代码8 {( d+ N7 `( }$ h; Z
Public Class Main
* k+ |) M8 V% U. d. } Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String/ s/ E% Y( g4 X- C
Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)! k3 \' |7 ~' |0 T6 \: ~% d3 ^
Dim webClient As WebClient = New WebClient()4 R3 X8 `1 @3 p' C, }5 @- C
''webClient.Encoding = System.Text.Encoding.UTF8
) W% [+ T, ]) u! {/ s, X Dim result As String = webClient.DownloadString(url), v a9 ]- l+ |1 t1 W8 j; m# R# P
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
' K: S" x) q7 `) L% r! w- Q result = result.Substring(0, result.IndexOf(""""))
) a0 ?; H0 Q6 w' s# }( v Return result* e2 u' A7 q$ A& b* L) E
End Function
: i1 h/ O8 o9 _; ~ Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
+ `( X. r( n l7 Q5 Y TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
+ L: r4 j {! Y5 A& z3 G; M End Sub- I" B- F2 @$ E# {+ ^' s m! J
End Class& \- q) D* c( a% k) N3 \
+ V% }9 X/ l/ ~8 Z: k& Z
抓图
% n- |2 o3 X" I' u0 \# x |