这个核心代码代码很简单,只有关键的几句话。" M8 ?7 U* ]; `: [8 W
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
/ A0 G3 H- S4 ~/ H% ~$ y今后很多翻译工具都会在这个基础上来完成。
5 `! k0 r1 }* v0 Z5 h1 u; T2 D; S5 E" \8 K' a8 u y1 P, {
8 ?# Z2 q8 P5 u8 B- |代码$ `% s) U7 p9 b' [% _2 x; \, p/ Q
Public Class Main( L8 v. Q2 ^6 G6 }) q; K; o
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String/ \1 k1 g! I. ~+ D. U$ m+ R! z
Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)' B. c& I+ U8 S, g( {4 Z
Dim webClient As WebClient = New WebClient()* p6 U0 G' v% l& f8 N
''webClient.Encoding = System.Text.Encoding.UTF8
0 _- d" T4 t9 ]% T8 s6 V Dim result As String = webClient.DownloadString(url)
+ K9 C: }% _* H5 H result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)" W" y" _5 y0 d8 O4 m( v/ p
result = result.Substring(0, result.IndexOf(""""))$ T5 c3 A) I) v* H& V$ m9 n: F
Return result
L8 J+ W' J$ c0 ]+ W End Function
. Y- m7 r8 Q& i7 ?& | Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- p% m- q4 W( I p TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")5 M; S/ \1 Q% N7 M' P" P
End Sub; ]6 b- y& x5 n8 k, r$ F5 n
End Class" D% l( R! K3 a" t- ~% V" ^7 Q
- q* z$ c/ s( v$ A4 A O
抓图2 g) H2 W! @; F+ U
|