Exemplo n.º 1
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Accep", "application/json, text/javascript, */*; q=0.01");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Origin", "http://fanyi.youdao.com/");
                    webClient.Headers.Add("Referer", "http://fanyi.youdao.com/");

                    var urldecode      = HttpUtility.UrlDecode("%C3%A6%C2%83%C2%B3%C3%A8%C2%A6%C2%81%C3%A6%C2%88%C2%90%C3%A4%C2%B8%C2%BA");
                    var uri            = new Uri("http://fanyi.youdao.com/translate");
                    var requestDetails = string.Format("type={1}2{2}&i={0}&doctype=json&xmlVersion=1.8&keyfrom=fanyi.web&ue=UTF-8&action=FY_BY_CLICKBUTTON&typoResult=true", (text), from, to);
                    //requestDetails = "type=zh2en&i=%C3%A6%C2%83%C2%B3%C3%A8%C2%A6%C2%81%C3%A6%C2%88%C2%90%C3%A4%C2%B8%C2%BA&doctype=json&xmlVersion=1.8&keyfrom=fanyi.web&ue=UTF-8&action=FY_BY_CLICKBUTTON&typoResult=true";
                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.IndexOf("translateResult", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        var youdao = JsonHelper.Deserialize <Youdao>(resultJson);
                        return(string.Join(". ", youdao.translateResult.Select(s => string.Join(". ", s.Select(s1 => s1.tgt)))));
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 2
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Origin", "https://translate.google.com/");
                    webClient.Headers.Add("Referer", "https://translate.google.com/");


                    var url = string.Format("https://translate.google.com/translate_a/single?client=t&sl={1}&tl={2}&hl=en&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&ie=UTF-8&oe=UTF-8&source=btn&srcrom=0&ssel=3&tsel=3&kc=0&tk=521427|965199&q={0}", HttpUtility.UrlEncode(text), from, to);

                    var bytes      = webClient.DownloadData(url);
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.Length > text.Length)
                    {
                        resultJson = resultJson.Substring(resultJson.IndexOf(@"[[[""", StringComparison.Ordinal) + 4, resultJson.IndexOf(@""",""" + text.First(), StringComparison.Ordinal) - 4);
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 3
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                // translate to special language code for BabelFish
                var language = new Dictionary <string, string>
                {
                    { "en", "en" },
                    { "ja", "ja" },
                    { "ko", "ko" },
                    { "zh", "zh-CHS" },
                };

                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Encoding", "gzip, deflate");
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    webClient.Headers.Add("Origin", "http://www.babelfish.com/");
                    webClient.Headers.Add("Referer", "http://www.babelfish.com/");

                    webClient.Headers.Add("Cookie", string.Format("skip_contest=1; lang_to=English; lang_from=Japanese; lang_to_code=en; lang_from_code=ja; PHPSESSID={0}", text.GetMd5Hash()));
                    var uri            = new Uri("http://www.babelfish.com/tools/translate_files/ajax/session.php");
                    var requestDetails = string.Format("act=save_session&lang_s={1}&lang_d={2}&phrase={0}", HttpUtility.UrlEncode(text), language[from] ?? from, language[to] ?? to);



                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    // have saved session, get translation from it
                    if (resultJson.Contains("phrase_t"))
                    {
                        var babelFish = JsonHelper.Deserialize <BabelFish>(resultJson);
                        return(babelFish.session.phrase_t);
                    }

                    // do a second request to get the translate text

                    var uri2   = new Uri("http://www.babelfish.com/tools/translate_files/ajax/page5_success_page.php?userID=");
                    var bytes2 = webClient.DownloadData(uri2);

                    var resultJson2 = Encoding.UTF8.GetString(bytes2);

                    var startIndex = resultJson2.LastIndexOf(@"<li class=""s_after"">", StringComparison.OrdinalIgnoreCase);
                    var endIndex   = resultJson2.IndexOf("</li>", startIndex, StringComparison.OrdinalIgnoreCase);
                    if (startIndex > 0)
                    {
                        resultJson2 = resultJson2.Substring(startIndex, endIndex - startIndex);
                        return(resultJson2.Replace(@"<li class=""s_after"">", ""));
                    }

                    return("");
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 4
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Origin", "http://honyaku.yahoo.co.jp/");
                    webClient.Headers.Add("Referer", "http://honyaku.yahoo.co.jp/");

                    var uri            = new Uri("http://honyaku.yahoo.co.jp/TranslationText");
                    var requestDetails = string.Format("p={0}&ieid={1}&oeid={2}&results=1000&formality=0&_crumb={3}&output=json", HttpUtility.UrlEncode(text), from, to, GetCrumb());

                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.IndexOf("ResultSet", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        var honyaku = JsonHelper.Deserialize <Honyaku>(resultJson);
                        return(string.Join(". ", honyaku.ResultSet.ResultText.Results.Select(s => s.TranslatedText)));
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 5
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Encoding", "gzip, deflate");
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    webClient.Headers.Add("Origin", "http://translate.baidu.com");
                    webClient.Headers.Add("Referer", "http://translate.baidu.com");

                    var uri            = new Uri("http://translate.baidu.com/transcontent");
                    var requestDetails = string.Format("ie=utf-8&source=txt&query={0}&from={1}&to={2}&token=4b8ef49c152664896591cb3c27610ce6", HttpUtility.UrlEncode(text), from, to);
                    var bytes          = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson     = Encoding.UTF8.GetString(bytes);

                    var result = JsonHelper.Deserialize <Baidu>(resultJson);
                    return(string.Join(" ", result.data.Select(s => s.dst)));
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 6
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Origin", "http://www.lec.com/translate-demos.asp");
                    webClient.Headers.Add("Referer", "http://www.lec.com/translate-demos.asp");

                    var uri            = new Uri("http://www.lec.com/translate-demos.asp");
                    var requestDetails = string.Format("selectSourceLang={1}&selectTargetLang={2}&DoTransText=go&SourceText={0}", HttpUtility.UrlEncode(text), from, to);

                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.IndexOf("<textarea readonly", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        resultJson = resultJson.Substring(resultJson.IndexOf("<textarea readonly", StringComparison.OrdinalIgnoreCase));
                        resultJson = resultJson.Substring(resultJson.IndexOf(">", StringComparison.OrdinalIgnoreCase) + 1);
                        resultJson = resultJson.Substring(0, resultJson.IndexOf("</textarea>", StringComparison.OrdinalIgnoreCase)).Trim();
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 7
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Origin", "http://www.systranet.com");
                    webClient.Headers.Add("Referer", "http://www.systranet.com/translate");

                    var url            = string.Format("http://www.systranet.com/sai?lp={0}_{1}&service=systranettranslate", from, to);
                    var requestDetails = string.Format("[{0}]", text);

                    var bytes      = webClient.UploadData(url, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.Length > text.Length)
                    {
                        resultJson = resultJson.Substring(resultJson.IndexOf("];[", StringComparison.Ordinal) + 3);
                        resultJson = resultJson.Substring(0, resultJson.Length - 2);
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 8
0
        private string GetCrumb()
        {
            using (var webClient = WebClientFactory.ChromeClient())
            {
                var html = webClient.DownloadString("http://honyaku.yahoo.co.jp/transtext");
                html = html.Substring(html.IndexOf(@"name=""TTcrumb"" value=""", StringComparison.Ordinal));
                html = html.Substring(0, html.IndexOf(@"""/>", StringComparison.Ordinal));

                return(html.Replace(@"name=""TTcrumb"" value=""", ""));
            }
        }
Exemplo n.º 9
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            var language = new Dictionary <string, string>
            {
                { "en", "0" },
                { "ja", "8" },
                { "ko", "12" },
                { "zh", "10" },
                { "fr", "1" },
                { "it", "2" },
                { "es", "3" },
                { "de", "6" },
                { "pt", "5" }
            };

            if (language.ContainsKey(from) == false || language.ContainsKey(to) == false)
            {
                return(string.Format("This translator does not support translating from {0} to {1}", from, to));
            }

            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Origin", "http://translation.babylon.com/");
                    webClient.Headers.Add("Referer", "http://translation.babylon.com/");


                    var url = string.Format("http://translation.babylon.com/translate/babylon.php?v=1.0&langpair={1}%7C{2}&callback=ret&q={0}", HttpUtility.UrlEncode(text), language[from], language[to]);

                    var bytes      = webClient.DownloadData(url);
                    var resultJson = Encoding.UTF8.GetString(bytes); // ret('', {"translatedText":"Feed."}, 200, null, null);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.Contains(@"{""translatedText"":"))
                    {
                        resultJson =
                            resultJson.Substring(resultJson.IndexOf(@"{""translatedText"":", StringComparison.Ordinal));
                        resultJson = resultJson.Substring(0, resultJson.IndexOf(@"},", StringComparison.Ordinal) + 1);

                        var babylon = JsonHelper.Deserialize <Babylon>(resultJson);

                        return(babylon.translatedText);
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 10
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            var language = new Dictionary <string, string>
            {
                { "en", "E" },
                { "ja", "J" },
                { "ko", "K" },
                { "zh", "C" },
                { "fr", "F" },
                { "it", "I" },
                { "es", "S" },
                { "de", "G" },
                { "pt", "P" }
            };

            if (language.ContainsKey(from) == false || language.ContainsKey(to) == false)
            {
                return(string.Format("This translator does not support translating from {0} to {1}", from, to));
            }

            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
                    webClient.Headers.Add("Cross-Licence", "infoseek/main e3f33620ae053e48cdba30a16b1084b5d69a3a6c");
                    webClient.Headers.Add("Origin", "http://translation.infoseek.ne.jp/");
                    webClient.Headers.Add("Referer", "http://translation.infoseek.ne.jp/");

                    var uri            = new Uri("http://translation.infoseek.ne.jp/clsoap/translate");
                    var requestDetails = string.Format("e={1}{2}&t={0}", HttpUtility.UrlEncode(text), language[from] ?? "J", language[to] ?? "E");

                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    var infoSeek = JsonHelper.Deserialize <InfoSeek>(resultJson);

                    if (infoSeek.t != null)
                    {
                        return(infoSeek.t[0].text);
                    }

                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 11
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            var language = new Dictionary <string, string>
            {
                { "en", "eng" },
                { "ja", "jpn" },
                { "ko", "kor" },
                { "zh", "chi" },
                { "fr", "fra" },
                { "it", "ita" },
                { "es", "spa" },
                { "de", "ger" },
                { "pt", "por" },
                { "vi", "vie" }
            };

            if (language.ContainsKey(from) == false || language.ContainsKey(to) == false)
            {
                return(string.Format("This translator does not support translating from {0} to {1}", from, to));
            }

            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/json");
                    webClient.Headers.Add("Tracking", "applicationKey=dlWbNAC2iLJWujbcIHiNMQ%3D%3D applicationInstance=freetranslation");
                    webClient.Headers.Add("Origin", "http://www.freetranslation.com/");
                    webClient.Headers.Add("Referer", "http://www.freetranslation.com/");

                    var uri            = new Uri("http://www.freetranslation.com/gw-mt-proxy-service-web/mt-translation");
                    var requestDetails = string.Format(@"{{""text"":""{0}"",""from"":""{1}"",""to"":""{2}""}}", HttpUtility.UrlEncode(text), language[from], language[to]);

                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (!string.IsNullOrWhiteSpace(resultJson) && resultJson.IndexOf("translation", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        var freeTranslation = JsonHelper.Deserialize <FreeTranslation>(resultJson);
                        return(freeTranslation.translation);
                    }
                    return(resultJson);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 12
0
        public string Translate(string text, string from = "auto", string to = "auto")
        {
            try
            {
                var language = new Dictionary <string, string>
                {
                    { "en", "EN" },
                    { "ja", "JA" },
                };

                if (language.ContainsKey(from) == false || language.ContainsKey(to) == false)
                {
                    return(string.Format("This translator does not support translating from {0} to {1}", from, to));
                }

                using (var webClient = WebClientFactory.ChromeClient())
                {
                    //webClient.Headers.Add("Accept-Encoding", "gzip, deflate");
                    webClient.Headers.Add("Accept-Language", "en-US,en;q=0.8,vi;q=0.6");
                    webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    webClient.Headers.Add("Origin", "http://www.excite.co.jp/world/english_japanese/");
                    webClient.Headers.Add("Referer", "http://www.excite.co.jp/world/english_japanese/");
                    webClient.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                    //webClient.Headers.Add("Cookie", "xtl_s=723cf7ad55a5713f47403; registered=no; UID=6EA8403F55A57119");

                    var uri            = new Uri("http://www.excite.co.jp/world/english_japanese/");
                    var requestDetails = string.Format("wb_lp={1}{2}&before={0}", HttpUtility.UrlEncode(text), from, to);



                    var bytes      = webClient.UploadData(uri, Encoding.UTF8.GetBytes(requestDetails));
                    var resultJson = Encoding.UTF8.GetString(bytes);

                    if (resultJson.Contains(@"name=""after"">"))
                    {
                        var startIndex = resultJson.IndexOf(@"name=""after"">", StringComparison.OrdinalIgnoreCase);
                        var endIndex   = resultJson.IndexOf(@"</textarea>", startIndex, StringComparison.OrdinalIgnoreCase);
                        resultJson = resultJson.Substring(startIndex, endIndex - startIndex);
                        return(resultJson.Replace(@"name=""after"">", ""));
                    }

                    return("");
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
Exemplo n.º 13
0
        AccessToken GetAccessToken()
        {
            var clientID     = HttpUtility.UrlEncode("paranovels");
            var clientSecret = HttpUtility.UrlEncode("Qyyv9xNOQmBfYijLaHzMwkhvdnYSX6ppJ385txXoWV0=");

            const string translatorAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
            var          requestDetails      = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", clientID, clientSecret);


            using (var webClient = WebClientFactory.ChromeClient())
            {
                webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                string resultJson = webClient.UploadString(translatorAccessUri, requestDetails);

                return(JsonHelper.Deserialize <AccessToken>(resultJson));
            }
        }
Exemplo n.º 14
0
        public string Translate(string text, string from, string to)
        {
            try
            {
                using (var webClient = WebClientFactory.ChromeClient())
                {
                    var token = GetAccessToken();

                    string uri = string.Format("http://api.microsofttranslator.com/v2/Http.svc/Translate?text={0}&from={1}&to={2}", HttpUtility.UrlEncode(text), from, to);
                    webClient.Headers.Add("Authorization", string.Format("Bearer {0}", token.access_token));
                    var bytes = webClient.DownloadData(uri);

                    using (Stream stream = new MemoryStream(bytes))
                    {
                        var dcs = new DataContractSerializer(typeof(string));
                        return((string)dcs.ReadObject(stream));
                    }
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }