这个核心代码代码很简单,只有关键的几句话。
2 S: D ~+ Z( V8 N- O) N9 @演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。; z- w! o: N) J+ y0 V# [- f$ J
今后很多翻译工具都会在这个基础上来完成。
4 y2 S& ?4 v% L0 @. P
1 B, J/ g( D' Z2 k/ S) g# U4 f$ u+ Q* f1 m# ]
代码1 l- k! C! N: F2 j
Public Class Main3 \ E5 L% z% X5 }/ r
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
# c6 D/ p+ j( d, p( A, N Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)
- v& z0 u0 B. `( }) e* N. s Dim webClient As WebClient = New WebClient()
$ t6 L4 ~; J2 l ''webClient.Encoding = System.Text.Encoding.UTF8! ~& s2 t: l" `: `: o3 k. P& d0 r
Dim result As String = webClient.DownloadString(url)7 S) G9 }) _$ T7 v! o7 \
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
# g! @% ^+ K2 N7 X: j4 [1 { result = result.Substring(0, result.IndexOf(""""))4 Q7 O- f. b: w8 H, O
Return result
( [! c3 k6 J! A0 A1 D: f- m& n% ` End Function
7 y, A5 B i) y. y% f Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click. @) }8 \3 M9 v
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
; x }2 G; ?) X3 u End Sub, X1 L* D- s) t; v# f9 p
End Class
2 f& i- e/ \% F# v0 W. q4 R& f! B. I5 Z+ I; Y8 ?7 s& Z0 A" v% C ?
抓图) d! N$ e1 A2 {! b& q
|