原文* d0 v+ Q0 _6 U( G3 y
http://blogs.msdn.com/shahpiyush/archive/2007/06/09/3188246.aspx
$ {( Z9 f8 o) j f
: O' Z$ k/ s2 R- u" lHere is how you can translate a Text using Google's "Unofficial" API's.
/ g$ i/ p p0 J) m0 _. TThe URL for Google Translate is - http://www.google.com/translate_ ... =UTF8&text={0}&langpair={1}
* G" } ^# X" j# [[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.+ U+ _' \, U; w: |: U" c
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.1 z' \ S* N* R* F2 }4 A+ ]
/// <summary>2 i6 w& r3 ^. M/ n V
" C1 D7 ~. i# u+ c0 K+ g. _
/// Translate Text using Google Translate API's( h$ m1 R9 u/ P
3 X5 j. _' [3 u! _/// Google URL - http://www.google.com/translate_ ... =UTF8&text={0}&langpair={1}
- c) k( m1 v' V9 v
6 y3 N5 R2 y& g0 U; g/// </summary>
( l8 x! t( l- w5 V% {( {; W2 _0 E) t
/// <param name="input">Input string</param>
; P7 Z+ m( {+ y
# `0 c6 a4 z1 T0 r/// <param name="languagePair">2 letter Language Pair, delimited by "|".
6 K9 |+ F! g$ k! Z: g$ E1 C/// E.g. "ar|en" language pair means to translate from Arabic to English</param>, t; S, l+ U5 `& Q+ n% E- c0 P) ?
0 T( U0 u4 R# d
/// <returns>Translated to String</returns> l, m. @' x2 k3 Z4 Z0 k7 ~
& i* g8 D0 S& g) z2 z t \3 Cpublic string TranslateText(* ?. u) U" _# M5 u. ]' s$ F' j: T
; ]2 p; } z2 I7 l9 s% W! ` string input,
( {$ Z. G E5 [7 s- \4 H+ F* ]* s8 y2 O' ]& R
string languagePair)+ O% G- K, G: s: i* j2 R
3 g! j" O; _0 ~8 f+ a6 u
{
1 u$ D1 n' T7 k9 M
7 K# G% V4 ]. V# E! M# ]6 C; c3 L
" U @" [; U# b" y' k, i
- p' A6 Z6 C4 A" z4 p) m9 I p) o string url = String.Format("http://www.google.com/translate_t?hl=en&ie=UTF8&text={0}&langpair={1}", input, languagePair);) X# u$ h7 s; I$ z* M
1 A' b1 ]+ I* t2 H* ]/ c( i, b
WebClient webClient = new WebClient();
9 Z. Q: G# O& s! N; J) R2 H- _3 `; h
webClient.Encoding = System.Text.Encoding.UTF8;
: G: B+ v0 q, x5 o) `" C! N6 c7 b$ `1 I6 X8 |) D' @
. S9 g+ E! n4 k8 S4 m$ j1 ?6 N; X9 X1 X* {9 {9 E4 d
string result = webClient.DownloadString(url);- p/ ^" b6 x" J& W( J
$ U) y* O" W1 } result = result.Substring(result.IndexOf("id=result_box") + 22, result.IndexOf("id=result_box") + 500);
. w* o. R9 k6 _- B8 ]
: X$ e$ @$ V0 z( }( g result = result.Substring(0, result.IndexOf("</div"));7 G& G1 r9 S' U' l/ L( p
% \3 Y) z! x7 C: D. g$ J& h. x: O- R
2 A5 m1 S: C8 ` return result;
$ ]+ ~% v( U- y. C- @
3 u- h/ j$ J7 E0 e$ j}7 ^; Q* M0 b6 |: \. x
More details about this Unofficial Google Translation API can be found Here |