/* 软件名称:GoogleTranslate
* 开发日期:2008-6-5
* 作者备注:闲转时写的,仅限技术交流,不可用于商业用途。为减少Google服务器压力,请不要大批量使用。
* 源码中以包含 全部核心源码,相信大家以能自行修改,如遇问题或创新欢迎交流。
*
*
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;//注意要手动引用
namespace GoogleTranslate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
GoogleTranslate gt = new GoogleTranslate();
gt.translateText = "china";
MessageBox.Show(gt.translate());
}
}
public class GoogleTranslate
{
public string translateText;
private string engineUrl = "http://translate.google.com/translate_t";
public string lang = "en|zh-CN";
public string translate()
{
string rstr = GetText(GetHTML());
return rstr;
}
public string GetText(string DocText)
{
string text = DocText;
string keyStart = "<div id=result_box dir=/"ltr/">";
string keyEnd = "</div>";
int tsint = text.IndexOf(keyStart);
int start = tsint + keyStart.Length;
string tempText = text.Substring(start, text.Length - start - 1);
int teint = tempText.IndexOf(keyEnd);
int len = teint;
string rText = tempText.Substring(0, len);
return rText;
}
public string GetHTML()
{
string address = (this.engineUrl + "?text=" + this.encodeText(translateText) + "&langpair=" + this.lang) + "&ie=UTF8&hl=en";
byte[] bytes = new WebClient().DownloadData(address);
return Encoding.Default.GetString(bytes);
}
private string encodeText(string text)
{
text = text.Replace("’", "'");
if (this.lang == "en|zh-CN")
{
return HttpUtility.UrlEncode(text.Trim());
}
return HttpUtility.UrlEncode(text.Trim(), Encoding.UTF8);
}
}
}
2588

被折叠的 条评论
为什么被折叠?



