这个核心代码代码很简单,只有关键的几句话。; f x5 P3 y6 a* O. a: m+ u. ~# ?
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。2 m d) L* D& s" d; A5 G
今后很多翻译工具都会在这个基础上来完成。
& g0 x' h& Y7 J+ O9 h V; g! r0 F1 l" ` ^6 I3 P( w. G5 ~
2 i! L, [. D# S k m; L- U
代码& ?: [& ~# Q2 C
Public Class Main, ~- T' J# T G8 E+ T
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
. F; y! L" s5 F" ]4 P4 L! ^9 u Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)
' G+ r1 J; J" p5 ^/ o Dim webClient As WebClient = New WebClient()
& u! U1 K. D3 a; R ''webClient.Encoding = System.Text.Encoding.UTF8$ x7 P1 N5 h0 ]1 h
Dim result As String = webClient.DownloadString(url)4 G! M# I/ n$ S( |
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
& T" K' s& T$ y ^! F2 ^ result = result.Substring(0, result.IndexOf("""")), W& U* F* r6 t2 G p
Return result; P' q" F/ v: ]9 E( ^8 ^( ^
End Function
" | e2 T6 u1 a. Y2 @4 o6 b$ l/ e2 U Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click* V; B4 T/ V* R- J6 U3 ]
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
# w8 r, L# m% F- p, ]3 t End Sub( l* T) V) a; D$ `* u: X
End Class s2 ~8 I( L! l0 U6 y
' E' \ [. k* c# B6 B抓图
) {! k) f+ }- M |