这个核心代码代码很简单,只有关键的几句话。
5 A4 A) [$ T, Q) Y* J8 v$ K3 r- f演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。, E: `2 _ z2 v8 D
今后很多翻译工具都会在这个基础上来完成。
0 H! Q2 U5 P" s; ]4 p* l) @# n/ {+ p4 n8 u4 L2 L" y4 s+ m2 p
/ `9 r( R5 H, _- \5 i代码
% e# P) o7 ?" nPublic Class Main$ e% K8 _" r6 h) U
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
8 Q5 O9 \/ }4 `0 q Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)" ?) W3 Q& a7 P2 Z/ c
Dim webClient As WebClient = New WebClient()$ L9 t) }) _7 @5 {/ R: T+ y
''webClient.Encoding = System.Text.Encoding.UTF8
4 r% S" v, v+ `2 P Dim result As String = webClient.DownloadString(url)2 t5 N& q6 ^2 ~* [# |6 N0 F) z
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
+ w8 I2 h( Z9 A6 o2 c; I8 _ result = result.Substring(0, result.IndexOf(""""))
' M* n* a0 w# K. A% M8 d) }2 s4 Z; ? Return result
# T8 X8 `% n$ |2 l2 }# M End Function
, I3 E8 g* m6 E( |: x2 Q1 z Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click& }8 z8 a v2 Z: N& ?
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")/ x. u) a2 n' O6 @5 ]8 {
End Sub: C5 O3 D$ x! w' N( S ]
End Class0 k. J% b$ W& [: e
% ~. d% Q" [( H' [, H
抓图
+ H" E: Z( ^0 Z( N3 S! r. o; p |