public bool CanTranslate(TranslateDir dir) { String s; if (((s = Translate("Hello", new TranslateDir("en", dir.from))) != "") && (Translate(s, new TranslateDir(dir.from, dir.to)) != "")) return true; else return false; }
public string Translate(string text, TranslateDir dir) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TranslateUrl); // Encode the text to be translated string postSourceData = GetPostSourceData(text, dir); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = postSourceData.Length; request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"; HttpWebResponse response; try { using (Stream writeStream = request.GetRequestStream()) { byte[] bytes = Encoding.UTF8.GetBytes(postSourceData); writeStream.Write(bytes, 0, bytes.Length); writeStream.Close(); } response = (HttpWebResponse)request.GetResponse(); } catch (Exception) { throw new Exception("Couldn't connect to the translation web service"); } StreamReader readStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string page = readStream.ReadToEnd(); response.Close(); Regex reg = new Regex(RegexpResult, RegexOptions.IgnoreCase); Match m = reg.Match(page); string s; if (m.Success) { s = GetString(page, m); } else throw new Exception("Couldn't parse a web service response. Please, update software"); return s; }
public void SetDirection(TranslateDir dir) { CurrentDirection = dir; }
protected override string GetPostSourceData(string text, TranslateDir dir) { return string.Format("f=mt&s={0}&t={1}&text={2}&cid={3}&p={4}", dir.from, dir.to, HttpUtility.UrlEncode(text), @"*****@*****.**", "NnZgBxUzeh"); }
protected override string GetPostSourceData(string text, TranslateDir dir) { return string.Format("hl={0}&ie=UTF8&text={1}&sl={2}&tl={3}", dir.from, HttpUtility.UrlEncode(text), dir.from, dir.to); }
protected abstract string GetPostSourceData(string text, TranslateDir dir);