这个核心代码代码很简单,只有关键的几句话。0 }3 X* N3 K1 M. o
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
, _- r2 i E$ {. D* y/ C今后很多翻译工具都会在这个基础上来完成。
! Z9 F* l+ z% |! n; x ]3 R+ U9 C6 i* Y
- @" L8 m% \* C% h1 P' ^1 i代码
?3 c& O! U( q9 |- q! CPublic Class Main5 n( Y j4 }/ K' K& A- A
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
8 D/ \4 k+ i0 `; n Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair), q# {) L* A3 X+ c
Dim webClient As WebClient = New WebClient()
3 }, d8 a3 J2 G" Z ''webClient.Encoding = System.Text.Encoding.UTF8* ~ V# `9 N3 }+ {: u' S
Dim result As String = webClient.DownloadString(url)
5 J2 y8 K3 w, z/ c( J result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38) n9 C+ C0 G5 T j- d5 U7 d1 n
result = result.Substring(0, result.IndexOf(""""))
% l! ]! m0 F: t* [1 M Return result
% e6 H5 k" a4 G9 X( }2 M ~2 S End Function
: \" ~5 t- ~' c9 I4 l2 Z Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
6 O3 [, J% g( t R TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
; t$ b1 a' _2 I3 o' y6 c5 @8 e! K End Sub+ R" I: m' q5 P* b9 U: Q9 ~
End Class
5 I1 |1 R) D; q+ y& c9 G7 R
. A7 t& l$ J8 M2 S: Q: @' b2 h6 a抓图0 M: y: [& `. K( e
|