Beispiel #1
0
        /// <summary>
        /// 百度翻译
        /// </summary>
        /// <param name="str">待翻译文字</param>
        /// <param name="form"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public BaiduTransAPI BaiduTranslate(string str, string form, string to)
        {
            string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?";

            // 拼接翻译类型参数
            url += "from=" + form;    // 翻译源语言
            url += "&to=" + to;       //  译文语言
            url += "&appid=" + appid; // 接口ID
            int    salt = new Random((int)DateTime.Now.Ticks).Next();
            string u    = url;

            u += "&salt=" + salt;                                                                           // 随机数
            u += "&q=" + HttpUtility.UrlEncode(str);                                                        // 翻译内容,需转码
            u += "&sign=" + HttpUtility.UrlEncode(EncryptionUtils.EncryptMD5(appid + str + salt + appkey)); // 签名
            BaiduTransAPI tran = GetReqObj <BaiduTransAPI>(u);

            return(tran);
        }
Beispiel #2
0
        /// <summary>
        /// 百度翻译
        /// </summary>
        /// <param name="dic">字典数据</param>
        /// <param name="Lan">翻译类型</param>
        /// <param name="path">保存路径</param>
        public Dictionary <string, string> BaiduTranslate(Dictionary <string, string> dic, string Lan, string path)
        {
            Dictionary <string, string> res = new Dictionary <string, string>();

            foreach (var key in dic.Keys)
            {
                string[]      lanArr = Lan.Split(';');
                BaiduTransAPI tran   = BaiduTranslate(dic[key], lanArr[0], lanArr[1]);

                if (tran.trans_result.Count > 0)
                {
                    res.Add(tran.trans_result[0].src, tran.trans_result[0].dst);
                }
                else
                {
                    res.Add(dic[key], ">>>>>>>>>>翻译失败  " + tran.error_code + "  " + tran.error_msg);
                }
            }

            return(res);
        }