public static string[] Translate(string[] src, string from, string to) { StringBuilder sb = new StringBuilder(); int size = src.Length; List <string> ls = new List <string>(); int i; for (i = 0; i < size; i += 10) { sb.Remove(0, sb.Length); //sb.Clear(); for (int j = 0; j < 10; j++) { if (i + j < size) { sb.AppendLine(src[i + j]); } } string text = getRequest(sb.ToString(), from, to); BaiduResult r = jsonGet(text); if (r != null && r.trans_result != null) { foreach (BaiduResult.Trans_result tr in r.trans_result) { ls.Add(tr.dst); } } } return(ls.ToArray()); }
public static string Translate(string src, string from, string to) { string text = getRequest(src, from, to); BaiduResult r = jsonGet(text); if (r != null && r.trans_result != null && r.trans_result.Length > 0) { return(r.trans_result[0].dst); } else { return(""); } }
public static void Translate(Dictionary <string, string> src, string from = TRANSLATE_LANGUAGE_ENGLISH, string to = TRANSLATE_LANGUAGE_CHINESE) { int size = src.Count; StringBuilder sb = new StringBuilder(); int count = 0; string[] keys = new string[src.Count]; src.Keys.CopyTo(keys, 0); foreach (string key in keys) { sb.AppendLine(key); count++; if (count >= 10) { string text = getRequest(sb.ToString(), from, to); BaiduResult r = jsonGet(text); if (r != null && r.trans_result != null) { foreach (BaiduResult.Trans_result tr in r.trans_result) { src[tr.src] = tr.dst; } } sb.Remove(0, sb.Length); //sb.Clear(); } } if (sb.Length > 0) { string text = getRequest(sb.ToString(), from, to); BaiduResult r = jsonGet(text); if (r != null && r.trans_result != null) { foreach (BaiduResult.Trans_result tr in r.trans_result) { src[tr.src] = tr.dst; } } } }