說明:google提供了一組API可以給我們很方便的實現語言翻譯功能,對於我們(中國人)而言,常用的是中文與英文的互譯。大家英文好的可以看看GOOGLE的文檔:http://code.google.com/apis/ajaxlanguage/documentation/reference.html#LangNameArray
這篇文章原創在:http://blog.moozi.net/archives/2008/10/16/the-realization-of-web-service-of-full-text-translations-based-on-google-ajax-language-api.aspx
需要使用到JSON.NET在http://www.codeplex.com/Json下載。
什麼是序列化?可以看看我前面的文章。http://www.sosuo8.com/article/show.asp?id=2470
其實google已經提供了在JS下很方便的翻譯功能,但有些情況下我們卻需要在.net下操作。什麼情況?采集中文文章並翻譯成英文入庫,或者是寫一個C/S的軟件。下面我將主要貼出代碼,具體的可以看木子寫的文章。
其需要用:
encodeURIComponent
傳值。
主要代碼:
復制代碼 代碼如下:
/// <summary>
/// 替換翻譯後的特殊字符,用於換行等
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private string TransToHtml(string str)
{
string temp;
temp = str.Replace("003c", "<");
temp = temp.Replace("003e",">");
return temp;
}
/// <summary>
/// 使用WebRequest獲取Google翻譯後的內容
/// </summary>
/// <param name="str"></param>
/// <param name="strRequestLan"></param>
/// <param name="strResultLan"></param>
/// <returns></returns>
private string GetGoogleTransJSONString(string str, string strRequestLan, string strResultLan)
{
try
{
WebRequest request = HttpWebRequest.Create("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + str + "&langpair=" + strRequestLan + "%7C" + strResultLan);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
stream.Close();
response.Close();
return responseFromServer;
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 翻譯
/// </summary>
/// <param name="str">需要翻譯的內容</param>
/// <param name="strReuest">原文</param>
/// <param name="strResult">譯文</param>
/// <returns></returns>
public string Translate(string str, string strReuestLan, string strResultLan)
{
try
{
if (!string.IsNullOrEmpty(str))
{
TranslateString transtr = (TranslateString)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(GetGoogleTransJSONString(str, strReuestLan, strResultLan),typeof(TranslateString));
if (transtr.responseStatus == 200)
{
return transtr.responseData.translatedText;
}
else
{
return "有錯誤發生";
}
}
else
{
return str;
}
}
catch (Exception ex)
{
return ex.Message;
}
}
反序列化: 復制代碼 代碼如下:
using System;
/// <summary>
///反序列化
/// </summary>
/// {"responseData": {"translatedText":"ahuinan"}, "responseDetails": null, "responseStatus": 200}
[Serializable]
public class TranslateString
{
private TranslatedText _responseData;
private string _responseDetails;
private int _responseStauts;
public TranslatedText responseData
{
get
{
return _responseData;
}
set
{
_responseData = value;
}
}
public string responseDetails
{
get
{
return _responseDetails;
}
set
{
_responseDetails = value;
}
}
public int responseStatus
{
get
{
return _responseStauts;
}
set
{
_responseStauts = value;
}
}
/// <summary>
/// 譯文內容
/// </summary>
public class TranslatedText
{
private string _translatedText;
public string translatedText
{
get
{
return _translatedText;
}
set
{
_translatedText = value;
}
}
}
}