protected override string Translate(string value, string fromLanguage, string toLanguage, string fromApiLanguage, string toApiLanguage) { if (string.IsNullOrEmpty(tracking)) { tracking = GetTracking(); } TranslateRequest request = new TranslateRequest() { text = WebUtils.FormatJson(value), from = fromApiLanguage, to = toApiLanguage }; string requestJson = JSONSerializer <TranslateRequest> .Serialize(request); using (WebClient webClient = new WebClient()) { string url = "https://www.freetranslation.com/gw-mt-proxy-service-web/mt-translation"; webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; webClient.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8"; webClient.Headers["Tracking"] = tracking; string json = webClient.UploadString(url, requestJson); TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); return(response.translation); } }
private static string TranslateWithToken(string input, string fromLanguage, string toLanguage) { string token = GenerateTk(input);//LazyGenerateToken(input); // There are a lot of url options available, see the following // https://github.com/Stichoza/google-translate-php/blob/master/src/GoogleTranslate.php input = HttpUtility.UrlEncode(input); string url = string.Format("https://translate.google.com/translate_a/single?client=webapp&sl={0}&tl={1}&dt=t&tk={2}&q={3}", fromLanguage, toLanguage, token, input); using (WebClient webClient = new WebClient()) { webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; string result = string.Empty; string json = webClient.DownloadStringAwareOfEncoding(url); TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); object[] items = response[0] as object[]; foreach (object item in items) { object[] values = item as object[]; result += values[0] as string; } return(result); } }
protected override string Translate(string value, string fromLanguage, string toLanguage, string fromApiLanguage, string toApiLanguage) { TimeSpan elapsedTime = DateTime.Now - lastCookiesTime; if (cookies == null || elapsedTime > cookiesExpireTime) { cookies = GetCookies("https://www.bing.com/translator"); } lastCookiesTime = DateTime.Now; try { using (WebClient webClient = new WebClient()) { string url = "https://www.bing.com/ttranslate";//?&category=&IG=7206AF8836B34421880FE9079EAE74A9&IID=translator.5034.6 webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8"; webClient.Headers[HttpRequestHeader.Cookie] = cookies; string json = webClient.UploadString(url, "text=" + HttpUtility.UrlEncode(value) + "&from=" + fromApiLanguage + "&to=" + toApiLanguage); TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); return(response.translationResponse); } } catch (Exception e) { cookies = null; throw e; } }
protected override string Translate(string value, string fromLanguage, string toLanguage, string fromApiLanguage, string toApiLanguage) { TimeSpan elapsedTime = DateTime.Now - lastCookiesTime; if (cookies == null || elapsedTime > cookiesExpireTime) { cookies = GetCookies("https://fanyi.baidu.com"); // The request requires cookies to get a valid token string html; cookies = GetCookies("https://fanyi.baidu.com", cookies, out html); string tokenPrefix = "token: '"; string gtkPrefix = "gtk = '"; int tokenIndex = html.IndexOf(tokenPrefix); int gtkIndex = html.IndexOf(gtkPrefix); if (tokenIndex >= 0 && gtkIndex >= 0) { token = html.Substring(tokenIndex + tokenPrefix.Length); token = token.Substring(0, token.IndexOf('\'')); gtk = html.Substring(gtkIndex + gtkPrefix.Length); gtk = gtk.Substring(0, gtk.IndexOf('\'')); } else { cookies = null; token = null; gtk = null; } } lastCookiesTime = DateTime.Now; using (WebClient webClient = new WebClient()) { string url = "https://fanyi.baidu.com/v2transapi"; string sign = GetSign(value, gtk); webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; webClient.Headers[HttpRequestHeader.Cookie] = cookies; webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8"; string json = webClient.UploadString(url, string.Format("from={0}&to={1}&query={2}&transtype=realtime&token={3}&sign={4}", fromApiLanguage, toApiLanguage, HttpUtility.UrlEncode(value), token, sign)); TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); return(response.trans_result.data[0].dst); } }
protected override string Translate(string value, string fromLanguage, string toLanguage, string fromApiLanguage, string toApiLanguage) { TranslateRequest request = new TranslateRequest() { dirCode = fromApiLanguage + "-" + toApiLanguage, template = "General", text = WebUtils.FormatJson(value), lang = fromApiLanguage, limit = 3000, useAutoDetect = false, key = string.Empty, ts = "MainSite", tid = string.Empty, IsMobile = true }; string requestJson = JSONSerializer <TranslateRequest> .Serialize(request); string url = "http://www.online-translator.com/services/TranslationService.asmx/GetTranslateNew"; using (WebClient webClient = new WebClient()) { webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; webClient.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8"; string json = webClient.UploadString(url, requestJson); // Lazy hack, find the correct way to handle "__type" json = json.Replace("__type", "__nype__"); TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); string result = response.d.result; if (!string.IsNullOrEmpty(result) && result.StartsWith("<style")) { result = HttpUtility.HtmlDecode(result); string resultHeader = "ref_result"; int resultOffset = result.IndexOf(resultHeader) + resultHeader.Length; result = result.Substring(resultOffset); result = result.Substring(result.IndexOf('>') + 1); result = result.Substring(0, result.IndexOf('<')).Trim(); return(result); } else { return(result); } } }
protected override string Translate(string value, string fromLanguage, string toLanguage, string fromApiLanguage, string toApiLanguage) { TimeSpan elapsedTime = DateTime.Now - lastSessionIdTime; if (sessionId == null || elapsedTime > sessionIdExpireTime) { sessionId = GetSessionId(); } lastSessionIdTime = DateTime.Now; try { using (WebClient webClient = new WebClient()) { string formattedValue = HttpUtility.UrlEncode(value); //Alternative: 2-0 string url = string.Format("https://translate.yandex.net/api/v1/tr.json/translate?id={0}-0-0&srv=tr-text&lang={1}-{2}", sessionId, fromApiLanguage, toApiLanguage); if (!usePost) { url += "&text=" + formattedValue; } webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; string json = null; if (usePost) { webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8"; json = webClient.UploadString(url, "text=" + formattedValue); } else { webClient.Headers[HttpRequestHeader.ContentType] = "application/json; charset=UTF-8"; json = webClient.DownloadStringAwareOfEncoding(url); } TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); return(response.text[0]); } } catch (Exception e) { sessionId = null; throw e; } }
protected override string Translate(string value, string fromLanguage, string toLanguage, string fromApiLanguage, string toApiLanguage) { using (WebClient webClient = new WebClient()) { string url = string.Format("https://translation.babylon-software.com/translate/babylon.php?v=1.0&q={0}&langpair={1}|{2}&callback=babylonTranslator.callback&context=babylon.0.3._babylon_api_response", HttpUtility.UrlEncode(value), languageIds[fromApiLanguage], languageIds[toApiLanguage]); webClient.Proxy = null; webClient.Encoding = Encoding.UTF8; webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded; charset=UTF-8"; string json = webClient.DownloadStringAwareOfEncoding(url); json = json.Substring(json.IndexOf('{')); json = json.Substring(0, json.LastIndexOf('}') + 1); TranslateResponse response = JSONSerializer <TranslateResponse> .DeSerialize(json); string result = HttpUtility.UrlDecode(response.translatedText); //string result = HttpUtility.HtmlDecode(response.translatedText); return(result); } }