这个核心代码代码很简单,只有关键的几句话。' ?, M. m& y! n, {( F. R
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
) z2 V4 F* X" I今后很多翻译工具都会在这个基础上来完成。
) s- f- y/ V3 v0 c" N9 B% v# _- S% X3 w
$ O9 M! G0 i" A4 M
代码: |6 R0 O9 c5 e. [. c; [
Public Class Main7 k9 T. j6 z* ?; H: p a
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String0 Z& n/ w% T/ c3 B6 @7 L
Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair)
# Q% l0 `6 g4 z- y0 O Dim webClient As WebClient = New WebClient()7 W& V# }9 i+ C8 v# i, N! w' ?9 X% c
''webClient.Encoding = System.Text.Encoding.UTF8( B5 }. ~& g0 t1 ]9 [
Dim result As String = webClient.DownloadString(url)
* B3 A1 t5 o, D# Y+ r result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)1 u+ P$ V B% d, D7 a
result = result.Substring(0, result.IndexOf(""""))/ n: `6 ?0 A* s- Z
Return result
/ C$ Y/ H4 B8 M End Function
0 w( }9 ?2 M- v& _& Y Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
8 T. `+ R- O7 l. C9 U TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
1 y# p3 F* k. \8 |: T) s* h7 ]9 H End Sub% ^' F; h4 e$ f* S& U
End Class9 `2 Y, ?# y2 a& z8 M( P0 g
! Z; \6 [; u$ Q; Q; i) w
抓图" ?4 n! D% ~% S, Y
|