这个核心代码代码很简单,只有关键的几句话。
1 J4 {; V+ ?- n- u9 T* t4 o演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。% R8 n' N+ p4 O, h9 M; o
今后很多翻译工具都会在这个基础上来完成。
7 [ b6 X, `; ?$ {' ]5 _6 [
( L2 r) [/ g3 y1 c: p9 g" k% Z, W; j* |' h2 y2 E9 ]$ P8 j+ U% M
代码: P: `; I3 ?# s, H3 i& M) t. s
Public Class Main. o1 F- [. t( ?, t) I" Y
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String% a0 [6 N( `. U+ a/ ?# q
Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)# X' N9 ~# V* |5 U: u
Dim webClient As WebClient = New WebClient()4 C; {3 S1 `+ y# s
''webClient.Encoding = System.Text.Encoding.UTF8
* z4 N! d% }1 \/ I Dim result As String = webClient.DownloadString(url)
, O: I) V+ y: X; |/ ]2 O L( a result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
5 U% f# Q+ W, R9 ^5 R w result = result.Substring(0, result.IndexOf(""""))
) G8 X$ }" |2 y! n Return result
, Q/ F; k( @; D/ p* P% V1 `1 \ End Function
( Y0 A( ^6 n& {& k3 Z Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click) c' v3 ~, x5 M6 K! l/ M5 T* T/ [
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja") C7 n- l1 Q% m% w% a
End Sub5 C' ~% P' Y U- z6 _# r+ q( {' V" v: o
End Class7 j, ^) p, _- V2 o
& Y$ i# t2 `! f8 @4 }/ r0 c
抓图5 K0 k" ]6 _( q
|