http://www.crifan.com/teach_you_how_to_find_free_google_translate_api/
(还没试过有时间试下.....)
今日,在想要在C#中,利用google翻译,实现把中文翻译为英文。
然后就去找,对应翻译的url和参数是如何使用的。
参考之前自己的的BlogsToWordpress中的Python版本的crifanLib:crifanLib.py中的translateString,然后去写代码,结果竟然发现,无法成功获得对应的返回的html,因此也就无法找到翻译后的结果了。
所以,就打算手动自己去找当前可用的google翻译。
http://translate.google.cn/
然后使用之前在:
【总结】浏览器中的开发人员工具(IE9的F12和Chrome的Ctrl+Shift+I)-网页分析的利器
中所介绍的IE9的F12,去调试。
然后设置好
源语言:中文(简体)
目标语言:英文
然后再随便输入一串汉字,比如:
他们是这样说的
然后可以看到结果为:
其中,此处是自动实时翻译,所以没有点击翻译按钮,就可以看到翻译的结果:
They say
了。
2. 然后接下来去找,到底此翻译的请求,是哪个地址。
而对于IE9的F12中,抓到很多url,内容繁多,也不容易找到到底哪个是我们想要的。
但是我们现在已经知道了翻译结果为:
They say
所以,就可以通过F12自带的搜索功能,去找到对应的url了:
如上,再点击Back to summary view,就可以看到是这个地址了:
对应的地址,在Detail view中,拷贝出来就是:
http://translate.google.cn/translate_a/t?client=t&text=%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84&hl=zh-CN&sl=zh-CN&tl=en&ie=UTF-8&oe=UTF-8&multires=1&otf=1&pc=1&it=srcd_gms.1378&ssel=4&tsel=6&sc=1
然后将其分解为:
http://translate.google.cn/translate_a/t?
client=t&
text=%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84&
hl=zh-CN&
sl=zh-CN&
tl=en&
ie=UTF-8&
oe=UTF-8&
multires=1&
otf=1&
pc=1&
it=srcd_gms.1378&
ssel=4&
tsel=6&
sc=1
3. 由此信息,便可以去写代码了。
经过一番代码调试,最后搞定:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//-----------------------------------------------------------------------------
//translate strToTranslate from fromLanguage to toLanguage
//return the translated string
//return empty string if error
//some frequently used language abbrv:
//Chinese Simplified: zh-CN
//Chinese Traditional: zh-TW
//English: en
//German: de
//Japanese: ja
//Korean: ko
//French: fr
//more can be found at:
//http://code.google.com/intl/ru/apis/language/translate/v2/using_rest.html#language-params
public
string
translateString(
string
strToTranslate,
string
fromLanguage,
string
toLanguage)
{
string
translatedStr =
""
;
string
transRetHtml =
""
;
////following refer:http://python.u85.us/viewnews-335.html
//string googleTranslateUrl = "http://translate.google.cn/translate_t";
//Dictionary
//postDict.Add("hl", "zh-CN");
//postDict.Add("ie", "UTF-8");
//postDict.Add("text", strToTranslate);
//postDict.Add("langpair", fromLanguage + "|" + toLanguage);
//const string googleTransHtmlCharset = "UTF-8";
//string transRetHtml = getUrlRespHtml(googleTranslateUrl, null,googleTransHtmlCharset, postDict);
////http://translate.google.cn/#zh-CN/en/%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84
//string googleTransBaseUrl = "http://translate.google.cn/#";
//strToTranslate = "他们是这样说的";
//string encodedStr = HttpUtility.UrlEncode(strToTranslate);
//string googleTransUrl = googleTransBaseUrl + fromLanguage + "/" + toLanguage + "/" + encodedStr;
//string transRetHtml = getUrlRespHtml(googleTransUrl);
//http://translate.google.cn/translate_a/t?client=t&text=%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84&hl=zh-CN&sl=zh-CN&tl=en&ie=UTF-8&oe=UTF-8&multires=1&ssel=0&tsel=0&sc=1
//strToTranslate = "他们是这样说的";
string
encodedStr = HttpUtility.UrlEncode(strToTranslate);
string
googleTransBaseUrl =
"http://translate.google.cn/translate_a/t?"
;
string
googleTransUrl = googleTransBaseUrl;
googleTransUrl +=
"&client="
+
"t"
;
googleTransUrl +=
"&text="
+ encodedStr;
googleTransUrl +=
"&hl="
+
"zh-CN"
;
googleTransUrl +=
"&sl="
+ fromLanguage;
// source language
googleTransUrl +=
"&tl="
+ toLanguage;
// to language
googleTransUrl +=
"&ie="
+
"UTF-8"
;
// input encode
googleTransUrl +=
"&oe="
+
"UTF-8"
;
// output encode
try
{
transRetHtml = getUrlRespHtml(googleTransUrl);
//[[["They say","他们是这样说的","","Tāmen shì zhèyàng shuō de"]],,"zh-CN",,[["They",[5],0,0,1000,0,1,0],["say",[6],1,0,1000,1,2,0]],[["他们 是",5,[["They",1000,0,0],["they are",0,0,0],["they were",0,0,0],["that they are",0,0,0],["they are the",0,0,0]],[[0,3]],"他们是这样说的"],["这样 说",6,[["say",1000,1,0],["said",0,1,0],["say so",0,1,0],["says",0,1,0],["say this",0,1,0]],[[3,6]],""]],,,[["zh-CN"]],1]
if
(extractSingleStr(
@"\[\[\[""(.+?)"","".+?"","
, transRetHtml,
out
translatedStr))
{
//extrac out:They say
}
}
catch
{
// if pass some special string, such as "彭德怀", then will occur 500 error
// here tmp not process the error, just omit it here
}
return
translatedStr;
}
public
string
transZhcnToEn(
string
strToTranslate)
{
return
translateString(strToTranslate,
"zh-CN"
,
"en"
);
}
|
【总结】
此处通过自己调试,找到的google翻译的api为:
基地址:
http://translate.google.cn/translate_a/t?
一些固定参数:
client=t
hl=zh-CN:表示本地语言是zh-CN
需要自己设置的参数:
text=%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84: 需要翻译的文字,被encode后的值,%E4%BB%96%E4%BB%AC%E6%98%AF%E8%BF%99%E6%A0%B7%E8%AF%B4%E7%9A%84对应的原文字是:他们是这样说的。
sl=zh-CN:source language为zh-CN,即需要翻译的文字是中文简体
tl=en:to language,目标语言为en,即要翻译为英语
ie=UTF-8:input encoding,输入的文字的编码为UTF-8
oe=UTF-8:output encoding,输出,翻译后,的文字的编码为UTF-8
其他可有可无的参数:
multires=1
otf=1
pc=1
it=srcd_gms.1378
ssel=4
tsel=6
sc=1
其中,最最核心的很明显是sl和tl,相对次要的是ie和oe。
自己根据自己的需要,设置text,sl,tl,(和ie和oe),提交对应的请求,即可获得如下的返回内容:
[[["They say","他们是这样说的","","Tāmen shì zhèyàng shuō de"]],,"zh-CN",,[["They",[5],0,0,1000,0,1,0],["say",[6],1,0,1000,1,2,0]],[["他们 是",5,[["They",1000,0,0],["they are",0,0,0],["they were",0,0,0],["that they are",0,0,0],["they are the",0,0,0]],[[0,3]],"他们是这样说的"],["这样 说",6,[["say",1000,1,0],["said",0,1,0],["say so",0,1,0],["says",0,1,0],["say this",0,1,0]],[[3,6]],""]],,,[["zh-CN"]],1]
提取其中的最开始的They say,即可实现翻译的目的了。