设为首页收藏本站官方微博

汉化资料 Translate your text using Google Api's

[复制链接]
查看: 1482|回复: 0
打印 上一主题 下一主题

[汉化资料] Translate your text using Google Api's

跳转到指定楼层
楼主
发表于 2010-5-21 22:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

Translate your text using Google Api's

原文* 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- w
5 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 c
7 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
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 很美好很美好 很差劲很差劲
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

冒险解谜游戏中文网 ChinaAVG

官方微博官方微信号小黑屋 微信玩家群  

(C) ChinaAVG 2004 - 2019 All Right Reserved. Powered by Discuz! X3.2
辽ICP备11008827号 | 桂公网安备 45010702000051号

冒险,与你同在。 冒险解谜游戏中文网ChinaAVG诞生于2004年9月9日,是全球华人共同的冒险解谜类游戏家园。我们致力于提供各类冒险游戏资讯供大家学习交流。本站所有资源均不用于商业用途。

快速回复 返回顶部 返回列表