. C # & Net dry goods share - Baidu language translation of the docking structure BaiduLanguageHelper

All return goods packaged API source. . .

Frame.Api namespace
{
    /// <the Summary>
    /// Baidu multi-language support is
    /// </ the Summary>
    public class BaiduLanguageHelper
    {
        / *
Language abbreviated name of the
auto automatic detection
zh Chinese
en English
yue in Cantonese
wyw classical
jp Japanese
kor Korean
fra French
spa Spanish
th Thai
ara Arabic
ru Russian
pt Portuguese
de German
it Italian
el Greek
nl Dutch
pl Polish
bul Bulgarian
est Estonian
dan Danish
fin Finnish
cs Czech
rom Romanian
slo Slovenian
swe Swedish
hu Hungarian
cht Traditional Chinese
vie Vietnamese
         * /

        // API can be translated directly into Baidu's official website to apply
        private static string LAN_APPID_BAI_DU = WebConfigHelper.GetWebConfigString ( "BaiduLanAppId");

        // API can be translated directly into Baidu's official website to apply
        private static string = WebConfigHelper.GetWebConfigString LAN_PWD_BAI_DU ( "BaiduLanPwd");

        /// <Summary>
        /// translate API calls Baidu translation
        /// details can http://api.fanyi.baidu.com/api/trans/product/ apidoc
        /// </ Summary>
        /// <param name = "Tcontent"> character to be translated </ param>
        /// <param name = "fromLan"> source language </ param>
        /// <param name = "toLan"> target language </ param>
        /// <returns> translation </ returns>
        public static string Translation(string tContent, string fromLan, string toLan)
        {
            string jsonResult = String.Empty;
            string languageFrom = fromLan.ToLower();
            string languageTo = toLan.ToLower();
            string randomNum = System.DateTime.Now.Millisecond.ToString();
            string md5Sign = GetMD5WithString(LAN_APPID_BAI_DU + tContent + randomNum + LAN_PWD_BAI_DU);
            String url = String.Format("http://api.fanyi.baidu.com/api/trans/vip/translate?q={0}&from={1}&to={2}&appid={3}&salt={4}&sign={5}", HttpUtility.UrlEncode(tContent, Encoding.UTF8), languageFrom, languageTo, LAN_APPID_BAI_DU, randomNum, md5Sign);
            WebClient wc = new WebClient();
            the try
            {
                JsonResult = wc.DownloadString (URL);
            }
            the catch
            {
                JsonResult = string.Empty;
            }
            the JavaScriptSerializer the JavaScriptSerializer JSS new new = ();
            TranslationResult RET = jss.Deserialize <TranslationResult> (JsonResult);
            IF (String RET = null &&! .IsNullOrEmpty (ret.Error_code))
            {
                return ret.Trans_result [0] .Dst.Trim ();
            }
            // translation fails, return to the original characters
            return Tcontent;
        }


        /// <Summary>
        /// MD5 encrypted
        // / </ summary>
        /// <param name = "input" > be encrypted string </ param>
        /// <Returns> encryption result </ Returns>
        Private GetMD5WithString static String (String INPUT)
        {
            // INPUT must be converted to UTF-8 encoding
            IF (iNPUT == null)
            {
                return null;
            }
            the MD5 md5Hash MD5.Create = ();
            // input string into an array of bytes and calculates a hash data  
            byte [] data = md5Hash.ComputeHash (Encoding.UTF8 . the GetBytes (INPUT));
            // Create a Stringbuilder and create a string of bytes to collect  
            the StringBuilder sBuilder the StringBuilder new new = ();
            // loop through each byte hashed data format and a hexadecimal string  
            for (int I = 0; I <data.length; I ++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            // 返回十六进制字符串  
            return sBuilder.ToString();
        }

    }

    /// <summary>
    /// 翻译API返回对象
    /// </summary>
    public class TranslationResult
    {
        /// <summary>
        ///
        /// </summary>
        public string Error_code { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string Error_msg { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string From { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string To { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string Query { get; set; }

        /// <summary>
        ///
        /// </summary>
        public Translation[] Trans_result { get; set; }
    }

    /// <summary>
    ///
    /// </summary>
    public class Translation
    {
        /// <summary>
        ///
        /// </summary>
        public string Src { get; set; }

        /// <summary>
        ///
        /// </summary>
        public string Dst { get; set; }
    }
}

Guess you like

Origin www.cnblogs.com/hualiuliu/p/11458099.html