标题: Google翻译API核心代码示例 [打印本页] 作者: shane007 时间: 2010-7-8 11:50 标题: Google翻译API核心代码示例 这个核心代码代码很简单,只有关键的几句话。 . g$ a" N" [' c3 F' Z" V# |5 Q: r0 L! D演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。 ! J4 m8 H6 ^; ~8 W' j+ P. ?2 ~1 J今后很多翻译工具都会在这个基础上来完成。' c* T( F. E* g y1 v d5 ?
8 X7 n/ Y; }& ?, m* G 8 ?. } S0 F' {" t) V代码# Z: Y* K) E! X9 {
Public Class Main . n; J. l* s: ? Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String 4 q& z5 @4 ~/ f# ^ Dim url As String = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair) 5 [& W9 ~4 Z( A' {+ F8 f Dim webClient As WebClient = New WebClient(); s; ^0 q+ K- P. w" N/ b" v5 k
''webClient.Encoding = System.Text.Encoding.UTF88 ]! j2 D* t) B0 V) g
Dim result As String = webClient.DownloadString(url) 6 P4 l( b* e4 Z result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38) ; X: Q) c5 \0 V9 c" F! z( z: ] result = result.Substring(0, result.IndexOf("""")) 1 E' l- G4 W* ~4 N$ o& T: a Return result( S3 J" h" _: b6 L7 s" C
End Function! {2 ^8 X: c& D7 M6 i1 g
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ; f1 b* d, B5 P: t( S5 T# R* | TextBox2.Text = TranslateText(TextBox1.Text, "en|ja"); c x5 j2 N2 z1 p
End Sub 2 p% b9 T- n1 j$ [6 A- n% ]End Class2 @- x1 u+ }! e- u, c