这个核心代码代码很简单,只有关键的几句话。2 W. ^4 T* } R, w
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
% u: V+ t' ?3 Y5 Z7 f1 \ k7 G今后很多翻译工具都会在这个基础上来完成。
9 y5 k0 K+ r0 v4 \& l" s
( @5 }4 T8 d) R& I6 G7 ?# {. z+ W
( P6 z( ?3 A# W; ?( M( R代码* l8 d( X- b) }
Public Class Main
" o% f6 k' L" W* k& F Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
) r6 z3 n+ W, m8 e8 l( _* z Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair) A- N0 V4 M" l
Dim webClient As WebClient = New WebClient()
7 {8 b( `4 {7 z) Y. f: W ''webClient.Encoding = System.Text.Encoding.UTF8
. _9 X1 h3 U9 ]/ _! j Dim result As String = webClient.DownloadString(url)
: b% s& c+ y" i, e* F result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
% @6 E1 w) Y: J result = result.Substring(0, result.IndexOf(""""))" L+ }& x# w1 Y( ?5 G, F
Return result
* I/ R% ?( O9 E+ I' b) s" G, \$ y End Function
: ^: l ~4 ~& t$ O d2 p4 F Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click0 `# g3 u5 X& J. J! x
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
& F" `2 m( g" x End Sub, ]) \( W9 h" J' k& \# n
End Class
3 Q) V: b$ Q4 G1 A) ?4 X1 @
$ z6 K, @1 n3 U+ N" |; g0 C抓图
( `- ]) |8 v4 f$ R8 V: |' ]* r |