这个核心代码代码很简单,只有关键的几句话。/ N! N# e2 T: G9 P$ N
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
8 M& f3 r7 Z m- H1 R5 p今后很多翻译工具都会在这个基础上来完成。
2 e9 J( W! z! R& I0 \" r7 Q7 M8 e2 [8 ^- k1 _! A
A: d. i$ u4 S2 P
代码3 i6 f9 u) i( t# B4 V0 i
Public Class Main
) \6 o* J8 r& A2 t! M0 v" \, \ Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
% _% ^! D( m/ N+ s# { Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)
. ? x6 O9 }2 D4 s: @( p/ U Dim webClient As WebClient = New WebClient()! y: e; S- G) R% d W" f2 d$ H+ E
''webClient.Encoding = System.Text.Encoding.UTF8
" o0 `7 \4 d* q Dim result As String = webClient.DownloadString(url)3 M1 O# i8 f% }' T
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
" s0 k! [8 |& W) h result = result.Substring(0, result.IndexOf(""""))% \/ W9 x- B* ^
Return result
/ [% Q- O+ ^7 A, W' s End Function! o( @7 T' P3 q: \
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click) a: h$ b z6 e3 N" l; ?% C# M
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
- {' [! r# ]) v# n& b End Sub
0 d* j; U7 p0 x& @End Class( u8 n* L' b% e7 r0 o# _
: x* |* I* |* I* a" p. q( g) M2 C
抓图6 J: r# p) D! r* j7 z: [
|