冒险解谜游戏中文网 ChinaAVG
标题:
Google翻译API核心代码示例
[打印本页]
作者:
shane007
时间:
2010-7-8 11:50
标题:
Google翻译API核心代码示例
这个核心代码代码很简单,只有关键的几句话。
k2 Y. a; _. s' D4 M {# q; P
演示了把英文字幕翻译为日文字幕的过程,翻译为中文也是一样的。
1 h/ J* |" k6 _
今后很多翻译工具都会在这个基础上来完成。
. q; Z" H9 H0 V+ Y0 E
2 g% f. s3 c: ^* H4 G2 Q- ?$ ]
- c- z2 F7 Q5 o5 J" o8 ~, b
代码
7 N$ I: A% N4 a$ u0 N' M% O2 m7 k' k
Public Class Main
. r* o" J+ M: U! X" E
Public Function TranslateText(ByVal input As String, ByVal languagePair As String) As String
) ~3 y" {) j( R" s+ ?' N
Dim url As String = String.Format("
http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1
}", input, languagePair)
$ j: Q( f* J: D7 b6 g8 F
Dim webClient As WebClient = New WebClient()
9 i, E; r c _0 O N$ L
''webClient.Encoding = System.Text.Encoding.UTF8
/ U& n# c1 J# g7 k1 @
Dim result As String = webClient.DownloadString(url)
8 Z9 F, z' o$ Z0 y. a2 k% B
result = result.Substring(result.IndexOf("<input type=hidden name=gtrans value=") + 38)
% P5 v- z/ H$ D( S5 m% E; Q; ~
result = result.Substring(0, result.IndexOf(""""))
, H) B7 x- A; A9 O2 m) S6 y3 [
Return result
. Q/ N- `$ q+ m
End Function
6 V2 ]% x8 s- N$ ]$ S2 g5 C
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
3 p+ Q) z7 ]2 Q5 J$ ~0 C
TextBox2.Text = TranslateText(TextBox1.Text, "en|ja")
: e/ _! {. V$ W5 F4 T
End Sub
" v4 r3 f5 ?; e* N5 d( C
End Class
1 z$ h3 E: z8 J- R
% F5 o3 e8 s7 X( I" u+ G! Y" r _ x/ Y
抓图
2 ]7 U* Q- n! \ k# q
[attach]16627[/attach]
欢迎光临 冒险解谜游戏中文网 ChinaAVG (https://chinaavg.com/)
Powered by Discuz! X3.2