asp.net利用google的api做翻譯

來源:互聯網
上載者:User

說明: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;
}
}
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.