标题: Translate your text using Google Api's [打印本页] 作者: shane007 时间: 2010-5-21 22:10 标题: Translate your text using Google Api's 原文 2 `' r; D: w8 q! W8 w- @ O8 v$ \http://blogs.msdn.com/shahpiyush/archive/2007/06/09/3188246.aspx 5 Z; J }2 l+ K- j3 O4 x" U* ~: a/ X. t5 v1 W E$ i
Here is how you can translate a Text using Google's "Unofficial" API's." m# G& f$ b0 a
The URL for Google Translate is - http://www.google.com/translate_ ... =UTF8&text={0}&langpair={1}, g9 \9 M5 g2 I' Y; Q" ]# x3 |
[li]"text" is your input string which needs to be translated. [/li][li]langpair is the language pairs involved in the tranlsation. E.g. "ar|en" means translate from Arabic to English.[/li]
The result when you browse to the URL is a HTML page. You will have to do screen scraping to get your translated text. ; g8 I( T9 U& B: @Below is a C# function which translates, scrapes and gives you the result. I am using String.Substring function but you can use Regex too. 2 l+ R h! I* w! B/// <summary>" V8 a3 F# c+ ]
3 F6 b' t- Y, D/ [: G/// Translate Text using Google Translate API's z+ o7 @) _- ~* i4 f
, e* V( @3 t# |+ K& u9 B; e
/// Google URL - http://www.google.com/translate_ ... =UTF8&text={0}&langpair={1} i* ~$ @( c; i
1 b9 U g5 q" d/ ^& |! Z; B" l* A2 X/// </summary> ) c' F+ y$ A" z9 X" k% e4 D. V& Y) l/ z' |
/// <param name="input">Input string</param> * T2 r5 T! i, |7 }+ C " y3 w: e' A9 }$ U- m7 L/// <param name="languagePair">2 letter Language Pair, delimited by "|". ; L/ O+ Z+ }- [* U6 o" i; v
/// E.g. "ar|en" language pair means to translate from Arabic to English</param> 4 a4 k( ^/ ^/ O {/ }0 b6 t! g; m$ r6 h
/// <returns>Translated to String</returns> ( s! D: ^0 M- `* x9 @1 p& a# \4 d% v
public string TranslateText(: Y- }3 n: N. ]) d# M- n" M1 f
4 ~! U1 p; K+ g" M% Z string input,, k8 x, t& H; K
! I" F7 j( r* J! S2 S+ j* F8 R$ { string languagePair) ; _; G# a3 B1 W1 k . R' u0 P7 O2 Y{& d' s% u( q6 e' i2 [" z
8 ]* z6 j& c; e" X 7 Q( L- ~* }3 @, E' v( ^ % p9 ^1 o' s V9 c string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair); + }6 u9 \3 c, L% F' A* l7 {5 G) {4 y! U- ^, u
WebClient webClient = new WebClient();& v/ b" x$ k0 \" i! d+ g0 R2 |
& [+ h( [9 X' }" g4 j1 a: @1 j4 b4 ? webClient.Encoding = System.Text.Encoding.UTF8; 9 I/ N9 S/ l" l# ^8 I + u! V; P, K8 V5 f 8 U: s$ c( u% `3 ^ 9 ~8 R8 F- e* q6 n* M0 A string result = webClient.DownloadString(url);( m6 ?1 S% z, M; {8 f+ L8 r' E
6 l6 M* ~2 {- B% w% h% E) i; n4 w
result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500);1 B8 c! V. a4 o1 X/ Y7 u
1 Z R8 R* X* \' Z2 v& t- |
result = result.Substring(0, result.IndexOf("</div")); 5 M1 ~: m0 G, g) S# O; z1 A ( `# D2 {8 n2 T& g) m5 I: m# z8 X; [3 I: p# B3 ]# o* E1 ]) X
3 J) L8 `5 ?) S, }9 r$ o return result; $ s2 Y. d% o' e' I) r9 E% z- d* |' P* h: S# i, \2 z, q" X) S- W
}, g5 ?: D9 O& g5 h" W7 O {9 J
More details about this Unofficial Google Translation API can be found Here