public BingTrans Trans(string word, string fromLanguage, string toLanguage)
        {
            var    tk              = JavaScriptHandle.Instance.tk(word, "414398.1781904367");
            string TransUrl        = $"https://translate.google.com/translate_a/single?client=t&sl=auto&tl={toLanguage}&hl=en&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&otf=1&pc=1&ssel=0&tsel=0&kc=2&tk={tk}&q={word}";
            var    TransResultJson = string.Empty;

            try
            {
                var googleTransUrl = new StringBuilder();
                TransResultJson = Sever.HttpGet(TransUrl, "");

                string Aem  = "";
                string BrE  = "";
                var    defs = new List <defs>();
                try
                {
                    var jarray = JArray.Parse(TransResultJson);
                    if (jarray[1].Count() > 0)
                    {
                        foreach (var Item in jarray[1])
                        {
                            defs.Add(new RecitingWord.defs()
                            {
                                def = Item[1].GetEnumeratorString(),
                                pos = Item[0].ToString()
                            });
                        }
                    }
                    else
                    {
                        defs.Add(new RecitingWord.defs()
                        {
                            def = jarray[0][0][0].ToString(),
                            pos = string.Empty
                        });
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("json解析错误->{0},message = {1}", TransResultJson, ex.Message);
                    BingTransApi.ErrorRecords("BulidBingTransError", string.Format("json = {0},message = {1}", TransResultJson, ex.Message));
                }

                return(new BingTrans(word, Aem, BrE, defs));
            }
            catch (Exception ex)
            {
                Console.WriteLine("服务器错误 -> {0}", ex.Message);
            }
            return(new BingTrans("", "", "", new List <defs>()));
        }
Example #2
0
        public List <string> GetTransResult(String query, String from, String to)
        {
            List <string> resule = new List <string>();

            for (int i = 0; i < 10; i++)
            {
                string jsonResult = string.Empty;
                try
                {
                    jsonResult = Sever.HttpGet(TRANS_API_HOST, buildParams(query, from, to));
                    foreach (var item in JObject.Parse(jsonResult)["trans_result"])
                    {
                        resule.Add(item["dst"].Value <string>());
                    }
                    break;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine("{0}:{1}", i, jsonResult);
                }
            }
            return(resule);
        }