首先使用Visual Studio 2008新建一个“ASP.NET Web服务应用程序”
由于Google AJAX Language API返回的是JSON格式的数据。所以我们先将下载后的“Newtonsoft.Json.dll”引用到项目,并添加新项“Web服务”将文件命名为“WebService.asmx”。
我们需要获得从Google返回的JSON数据,实现方法如下:
/// 使用WebRequest获取Google翻译后的内容
/// </summary>
/// <param name="strTranslateString">需要翻译的内容</param>
/// <param name="strRequestLanguage">原文语种</param>
/// <param name="strResultLanguage">译文语种</param>
/// <returns></returns>
private string GetGoogleTranslateJSONString(string strTranslateString,
string strRequestLanguage, string strResultLanguage)
{
WebRequest request = HttpWebRequest.Create(
"http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + strTranslateString + "&langpair=" + strRequestLanguage + "%7C" + strResultLanguage);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
实现原理:利用WebRequest把需要翻译的内容提交到Google服务器“http://ajax.googleapis.com/ajax/services/language/translate”,v=1.0是Google AJAX Language API的版本,q参数传递的是需要进行翻译的内容,langpair传递的是需要翻译的内容的语种,“%7C”后面的是译文语种。再使用HttpWebResponse获取Google返回的数据。
Google返回的JSON数据格式为:
"translatedText":"Ciao mondo"
},
"responseDetails": null,
"responseStatus": 200
};
按照Google返回的JSON数据格式,声明反系列化类。
/// 译文的反系列化类
/// </summary>
public class TranslateString
{
private TranslatedText responsedata;
public TranslatedText responseData
{
get { return responsedata; }
set { responsedata = value; }
}
private string responsedetails;
public string responseDetails
{
get { return responsedetails; }
set { responsedetails = value; }
}
private int responsestatus;
public int responseStatus
{
get { return responsestatus; }
set { responsestatus = value; }
}
/**//// <summary>
/// 译文内容
/// </summary>
public class TranslatedText
{
private string translatedtext;
public string translatedText
{
get { return translatedtext; }
set { translatedtext = value; }
}
}
}
对Google返回的数据进行反系列化后,就可以把反系列化后获得的译文返回显现给用户了。
/// 翻译
/// </summary>
/// <param name="strTranslateString">需要翻译的内容</param>
/// <param name="strRequestLanguage">原文语种</param>
/// <param name="strResultLanguage">译文语种</param>
/// <returns></returns>
[WebMethod(Description="Multi Language Translate.")]
public string MultiLanguageTranslate(string strTranslateString, string
strRequestLanguage, string strResultLanguage)
{
try
{
if (!string.IsNullOrEmpty(strTranslateString))
{
TranslateString transtring =
(TranslateString)
Newtonsoft.Json.JavaScriptConvert.DeserializeObject(
GetGoogleTranslateJSONString(strTranslateString,
strRequestLanguage, strResultLanguage),
typeof(TranslateString));
if (transtring.responseStatus == 200)
return transtring.responseData.translatedText;
else
return "There was an error.";
}
else
{
return strTranslateString;
}
}
catch (Exception e)
{
return e.Message;
}
}
这句“(TranslateString)Newtonsoft.Json.JavaScriptConvert.DeserializeObject(GetGoogleTranslateJSONString(strTranslateString, strRequestLanguage, strResultLanguage), typeof(TranslateString));”的作用是将GetGoogleTranslateJSONString(…)方法返回的字符串调用Json.NET的静态方法Newtonsoft.Json.JavaScriptConvert.DeserializeObject实现反系列化,并将其转化为TranslateString类型。
到这里这个Web Service基本上已经完成了。为方便使用,我们再内置两个中英文互译的方法。
/// 翻译
/// 默认地将英文翻译成简体中文
/// </summary>
/// <param name="TranslateString">需要翻译的内容</param>
/// <returns></returns>
[WebMethod(Description = "Translate English to Chinese.")]
public string TranslateEnglishToChinese(string strTranslateString)
{
return MultiLanguageTranslate(strTranslateString, "en", "zh-CN");
}
/// <summary>
/// 翻译
/// 默认地将简体中文翻译成英文
/// </summary>
/// <param name="TranslateString">需要翻译的内容</param>
/// <returns></returns>
[WebMethod(Description = "Translate Chinese to English.")]
public string TranslateChineseToEnglish(string strTranslateString)
{
return MultiLanguageTranslate(strTranslateString, "zh-CN", "en");
}