Пример #1
0
 private void saveTranslateResult(TrasnlateResult result)
 {
     if (result.IsSuccess)
     {
         successResultWriter.WriteLine("<item>");
         successResultWriter.WriteLine("<hw>" + result.Text + "<hw>");
         successResultWriter.WriteLine("<tr>");
         successResultWriter.WriteLine(result.Result);
         successResultWriter.WriteLine("</tr></item>");
     }
     else
     {
         failResultWriter.WriteLine(result.Text);
     }
 }
Пример #2
0
        /// <summary>
        /// 执行翻译
        /// </summary>
        /// <param name="text"></param>
        /// <param name="fromLan"></param>
        /// <param name="toLan"></param>
        /// <returns></returns>
        public TrasnlateResult Translate(string text, string fromLan = "auto", string toLan = "zh-CN")
        {
            if (string.IsNullOrWhiteSpace(fromLan))
            {
                fromLan = "en";
            }
            if (string.IsNullOrWhiteSpace(toLan))
            {
                fromLan = "zh-CN";
            }
            var result = new TrasnlateResult()
            {
                Text         = text,
                FromLan      = fromLan,
                ToLan        = toLan,
                IsSuccess    = false,
                ErrorMessage = "",
                Result       = "",
            };
            var url = string.Format("{2}/translate_a/single?client=t&sl=auto&tl={0}&hl={1}&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&source=btn&ssel=0&tsel=0&kc=0", toLan, fromLan, this.GoogleTranslateWeb.ToString());


            try
            {
                var tk = GetToken(text);
                url += tk + "&q=" + Uri.EscapeDataString(text);

                result.Result    = Get(url, ref cookie);
                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }