这个核心代码代码很简单,只有关键的几句话。
; \) Y$ j! L# {; v" G演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
+ g4 S- v. T" z: ^: Q7 K今后很多翻译工具都会在这个基础上来完成。/ Y8 E7 Y) A- r A3 ?
, m R+ a, x V- O) Z! A: M
2 d3 N* _9 ` _ b9 B
代码
! W! k0 b5 ?1 r* g! z9 \$ LPublic Class Main
. k1 v5 K: @8 [" |# } Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
: k2 c0 c# @' e6 l2 ]1 o Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair): T% x/ n* B9 {* e6 g6 s# ^
Dim webClient As WebClient = New WebClient(), H6 L8 C3 d2 t: r `
''webClient.Encoding = System.Text.Encoding.UTF81 y1 `- z% |3 i* e! f7 x
Dim result As String = webClient.DownloadString(url)- F! L) t. l4 {1 e2 R! T& @
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
' m) I5 }& H: W0 v5 V result = result.Substring(0, result.IndexOf(""""))
: A3 v- y0 i1 l$ }$ Y8 V. Z Return result9 X3 c3 @8 M$ K: W; x
End Function" `2 t) `. b. }9 i& C' A- g$ x
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click6 F ?7 }1 c, U) D0 b/ P
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")( A9 v# \2 y: w7 A
End Sub
4 J, \* S& A, A( V3 G- e* b8 hEnd Class6 R* j1 j) y9 Q$ p, x: N
5 r% ?, ]0 t0 E3 w/ I1 Q: N
抓图* {2 [( p# i3 o2 y8 D- l1 B
|