private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add("en", "英文");
dt.Rows.Add("zh-TW", "中文(繁體)");
dt.Rows.Add("zh-CN", "中文(簡體)");
dt.Rows.Add("tr", "土耳其文");
dt.Rows.Add("da", "丹麥文");
dt.Rows.Add("ja", "日文");
dt.Rows.Add("lt", "立陶宛文");
dt.Rows.Add("hu", "匈牙利文");
dt.Rows.Add("id", "印尼文");
dt.Rows.Add("hi", "印度文");
dt.Rows.Add("es", "西班牙文");
dt.Rows.Add("ca", "西班牙加泰羅尼亞文");
dt.Rows.Add("hr", "克羅地亞文");
dt.Rows.Add("iw", "希伯來文");
dt.Rows.Add("el", "希臘文");
dt.Rows.Add("lv", "拉脫維亞文");
dt.Rows.Add("pl", "波蘭文");
dt.Rows.Add("fr", "法文");
dt.Rows.Add("fi", "芬蘭文");
dt.Rows.Add("ar", "阿拉伯文");
dt.Rows.Add("sq", "阿爾巴尼亞文");
dt.Rows.Add("bg", "保加利亞文");
dt.Rows.Add("ru", "俄文");
dt.Rows.Add("no", "挪威文");
dt.Rows.Add("th", "泰文");
dt.Rows.Add("uk", "烏克蘭文");
dt.Rows.Add("mt", "馬耳他文");
dt.Rows.Add("cs", "捷克文");
dt.Rows.Add("nl", "荷蘭文");
dt.Rows.Add("sl", "斯拉維尼亞文");
dt.Rows.Add("sk", "斯洛伐克文");
dt.Rows.Add("tl", "菲律賓文");
dt.Rows.Add("vi", "越南文");
dt.Rows.Add("sr", "塞爾維亞文");
dt.Rows.Add("et", "愛沙尼亞文");
dt.Rows.Add("gl", "愛爾蘭的蓋爾文");
dt.Rows.Add("sv", "瑞典文");
dt.Rows.Add("it", "義大利文");
dt.Rows.Add("pt", "葡萄牙文");
dt.Rows.Add("de", "德文");
dt.Rows.Add("ko", "韓文");
dt.Rows.Add("ro", "羅馬尼亞文");
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "ID";
this.comboBox1.SelectedIndex = 1;
this.comboBox2.DataSource = dt.Copy();
this.comboBox2.DisplayMember = "Name";
this.comboBox2.ValueMember = "ID";
this.comboBox2.SelectedIndex = 0;
}
*******************************************************************************************
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Rows.Add("en", "英文");
dt.Rows.Add("zh-TW", "中文(繁體)");
dt.Rows.Add("zh-CN", "中文(簡體)");
dt.Rows.Add("tr", "土耳其文");
dt.Rows.Add("da", "丹麥文");
dt.Rows.Add("ja", "日文");
dt.Rows.Add("lt", "立陶宛文");
dt.Rows.Add("hu", "匈牙利文");
dt.Rows.Add("id", "印尼文");
dt.Rows.Add("hi", "印度文");
dt.Rows.Add("es", "西班牙文");
dt.Rows.Add("ca", "西班牙加泰羅尼亞文");
dt.Rows.Add("hr", "克羅地亞文");
dt.Rows.Add("iw", "希伯來文");
dt.Rows.Add("el", "希臘文");
dt.Rows.Add("lv", "拉脫維亞文");
dt.Rows.Add("pl", "波蘭文");
dt.Rows.Add("fr", "法文");
dt.Rows.Add("fi", "芬蘭文");
dt.Rows.Add("ar", "阿拉伯文");
dt.Rows.Add("sq", "阿爾巴尼亞文");
dt.Rows.Add("bg", "保加利亞文");
dt.Rows.Add("ru", "俄文");
dt.Rows.Add("no", "挪威文");
dt.Rows.Add("th", "泰文");
dt.Rows.Add("uk", "烏克蘭文");
dt.Rows.Add("mt", "馬耳他文");
dt.Rows.Add("cs", "捷克文");
dt.Rows.Add("nl", "荷蘭文");
dt.Rows.Add("sl", "斯拉維尼亞文");
dt.Rows.Add("sk", "斯洛伐克文");
dt.Rows.Add("tl", "菲律賓文");
dt.Rows.Add("vi", "越南文");
dt.Rows.Add("sr", "塞爾維亞文");
dt.Rows.Add("et", "愛沙尼亞文");
dt.Rows.Add("gl", "愛爾蘭的蓋爾文");
dt.Rows.Add("sv", "瑞典文");
dt.Rows.Add("it", "義大利文");
dt.Rows.Add("pt", "葡萄牙文");
dt.Rows.Add("de", "德文");
dt.Rows.Add("ko", "韓文");
dt.Rows.Add("ro", "羅馬尼亞文");
this.comboBox1.DataSource = dt;
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "ID";
this.comboBox1.SelectedIndex = 1;
this.comboBox2.DataSource = dt.Copy();
this.comboBox2.DisplayMember = "Name";
this.comboBox2.ValueMember = "ID";
this.comboBox2.SelectedIndex = 0;
}
*******************************************************************************************
// WebRequest方式 Demo
private string TranslateByPost(string sourceWord, string fromLanguage, string toLanguage)
{
//POST方式實現,無長度限制
string serverUrl = "http://translate.google.com/";
string postData = string.Format("langpair={0}|{1}&text={2}", fromLanguage, toLanguage, HttpUtility.UrlEncode(sourceWord));
private string TranslateByPost(string sourceWord, string fromLanguage, string toLanguage)
{
//POST方式實現,無長度限制
string serverUrl = "http://translate.google.com/";
string postData = string.Format("langpair={0}|{1}&text={2}", fromLanguage, toLanguage, HttpUtility.UrlEncode(sourceWord));
WebRequest request = WebRequest.Create(serverUrl);
request.Method = "POST";
byte[] bytes = Encoding.Default.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(bytes, 0, postData.Length);
postStream.Close();
request.Method = "POST";
byte[] bytes = Encoding.Default.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postData.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(bytes, 0, postData.Length);
postStream.Close();
WebResponse response = request.GetResponse();
Encoding encoding = Encoding.GetEncoding(response.ContentType.Replace("text/html; charset=", "")); //取得網頁編碼
string strResult = new StreamReader(response.GetResponseStream(), encoding).ReadToEnd(); //回傳的結果
response.Close();
Encoding encoding = Encoding.GetEncoding(response.ContentType.Replace("text/html; charset=", "")); //取得網頁編碼
string strResult = new StreamReader(response.GetResponseStream(), encoding).ReadToEnd(); //回傳的結果
response.Close();
//使用正則表達式去除多餘的文字內容
string strReg = @"\s+id=""?result_box""?\s+[^>]*>(.+)</span>\s*</div>\s*</div>\s*<div id=spell-place-holder\s+";
Match match = Regex.Match(strResult, strReg, RegexOptions.IgnoreCase | RegexOptions.Singleline);
string strReg = @"\s+id=""?result_box""?\s+[^>]*>(.+)</span>\s*</div>\s*</div>\s*<div id=spell-place-holder\s+";
Match match = Regex.Match(strResult, strReg, RegexOptions.IgnoreCase | RegexOptions.Singleline);
string strReturn = null;
if (match.Success)
{
strReturn = match.Groups[1].Value;
strReturn = Regex.Replace(strReturn, @"<br\s*/?>", "\n", RegexOptions.Singleline | RegexOptions.IgnoreCase);
strReturn = Regex.Replace(strReturn, @"<[^>]*>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
strReturn = HttpUtility.HtmlDecode(strReturn);
}
if (match.Success)
{
strReturn = match.Groups[1].Value;
strReturn = Regex.Replace(strReturn, @"<br\s*/?>", "\n", RegexOptions.Singleline | RegexOptions.IgnoreCase);
strReturn = Regex.Replace(strReturn, @"<[^>]*>", "", RegexOptions.Singleline | RegexOptions.IgnoreCase);
strReturn = HttpUtility.HtmlDecode(strReturn);
}
return strReturn;
}
*******************************************************************************************
//開始翻譯
textBox2.Text = TranslateByPost(textBox1.Text, comboBox1.SelectedValue.ToString(), comboBox2.SelectedValue.ToString());
}
*******************************************************************************************
//開始翻譯
textBox2.Text = TranslateByPost(textBox1.Text, comboBox1.SelectedValue.ToString(), comboBox2.SelectedValue.ToString());
*******************************************************************************************
下載:https://docs.google.com/file/d/0B-GGlGmvBDC0S3dCZ3E5NXBSRW8/edit
下載:https://docs.google.com/file/d/0B-GGlGmvBDC0S3dCZ3E5NXBSRW8/edit
沒有留言:
張貼留言