这个核心代码代码很简单,只有关键的几句话。* P3 M2 I c3 |/ p1 F2 m1 P8 J
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。7 U0 R! g4 |+ [+ i: }" D
今后很多翻译工具都会在这个基础上来完成。8 o2 S+ M+ B. }1 k* u
& X% D5 f; ^. `# z9 e" R) e0 S1 { @3 U& B, `' _ L/ F
代码
: o* ^2 a1 a) QPublic Class Main+ P8 B' w& Q4 X R* r' c8 ~0 B7 O( ^* S" I
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String6 n* p/ E: ?+ T! s
Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)
6 f/ S. l X5 ?# N; U2 j Dim webClient As WebClient = New WebClient()2 o' R% t$ U0 p1 I% s
''webClient.Encoding = System.Text.Encoding.UTF8% s2 {) U/ m$ \ U' y
Dim result As String = webClient.DownloadString(url)
' a- e7 S8 E9 \ result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
) R1 `, p2 |8 W7 ^& c result = result.Substring(0, result.IndexOf(""""))) z3 {! W7 F/ C. O
Return result8 x/ y) ~+ M& U' O$ m0 Z8 z. i
End Function
6 ?* j7 o, W! {. p Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click& g- h! L) j, Z! ]9 A9 w+ x
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")0 d( C$ ^: v& A1 u% [/ h5 w( z% ~! N
End Sub7 ^8 c0 I x- }+ m( _, _
End Class
1 o2 b, Y6 s* O [
: l1 d! H4 Q' @* H( C! M+ m抓图1 L% z4 b6 k# v. g8 q1 x, B
|