示例#1
0
        public static string Translate(string text, string from, string to, out string speakUrl)
        {
            speakUrl = null;
            string url     = "https://openapi.youdao.com/api";
            string salt    = DateTime.Now.Millisecond.ToString();
            string curTime = WebExt.GetTimeSpan();
            string sign    = ComputeHash(YoudaoKey.AppKey + Truncate(text) + salt + curTime + YoudaoKey.AppSecret);
            string str     = string.Format($"q={HttpUtility.UrlEncode(text)}" +
                                           $"&from={GetTranLang(from)}&to={GetTranLang(to)}&appKey={YoudaoKey.AppKey}" +
                                           $"&salt={salt}&sign={sign}&signType=v3&curtime={curTime}");
            string result = WebExt.Request(url, null, str);
            // 解析json数据
            JavaScriptSerializer js   = new JavaScriptSerializer();
            YoudaoTranslate      list = js.Deserialize <YoudaoTranslate>(result);

            if (list.errorCode != "0")
            {
                ErrorTip(list.errorCode,
                         "https://ai.youdao.com/DOCSIRMA/html/%E8%87%AA%E7%84%B6%E8%AF%AD%E8%A8%80%E7%BF%BB%E8%AF%91/API%E6%96%87%E6%A1%A3/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1/%E6%96%87%E6%9C%AC%E7%BF%BB%E8%AF%91%E6%9C%8D%E5%8A%A1-API%E6%96%87%E6%A1%A3.html");
                return("");
            }
            speakUrl = list.tSpeakUrl;
            // 接收序列化后的数据
            return(string.Join("", list.translation));
        }
示例#2
0
        /// <summary>
        /// 直接调用有道图片翻译
        /// </summary>
        /// <param name="path">图片路径</param>
        /// <param name="from">源语言</param>
        /// <returns></returns>
        public static void YoudaoTran(string path, string from, string to, out string src_text, out string dst_text)
        {
            Dictionary <String, String> dic = new Dictionary <String, String>();

            string url  = "https://openapi.youdao.com/ocrtransapi";
            string q    = LoadAsBase64(path);
            string salt = DateTime.Now.Millisecond.ToString();
            string type = "1";

            //string to = "zh-CHS";

            dic.Add("from", from);
            dic.Add("to", to);
            dic.Add("type", type);
            dic.Add("q", HttpUtility.UrlEncode(q, Encoding.UTF8));
            string signStr = appKey + q + salt + appSecret;;
            string sign    = ComputeHash(signStr, new MD5CryptoServiceProvider());

            dic.Add("appKey", appKey);
            dic.Add("salt", salt);
            dic.Add("sign", sign);

            string result = Post(url, dic);

            JavaScriptSerializer js   = new JavaScriptSerializer();               // 实例化一个能够序列化数据的类
            YoudaoTranslate      list = js.Deserialize <YoudaoTranslate>(result); // 将json数据转化为对象类型并赋值给list

            if (list.errorCode != "0")
            {
                throw new Exception("错误代码:" + list.errorCode);
            }

            // 接收序列化后的数据
            StringBuilder dst = new StringBuilder();
            StringBuilder src = new StringBuilder();

            foreach (var item in list.resRegions)
            {
                src.Append(item.context + "\r\n");
                dst.Append(item.tranContent + "\r\n");
            }

            int sLen = src.ToString().LastIndexOf('\r');
            int dLen = dst.ToString().LastIndexOf('\r');

            src_text = src.ToString().Remove(sLen);
            dst_text = dst.ToString().Remove(dLen);
        }