private static bool GoogleTranslate(string input, out string result) { result = ""; string urlEncoded = HttpUtility.UrlEncode(input); if (urlEncoded.Length > GOOGLE_LIMIT) { return(false); } string source = PDDLanguage.Russian.GoogleCode; string target = PDDLanguage.Current.GoogleCode; // Non-Json URL: "http://translate.googleapis.com/translate_a/t?client=gtx&sl={0}&tl={1}&q={2}" // Unfortunately this stops working after a few requests string format = "http://translate.googleapis.com/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl={0}&tl={1}&q={2}"; string url = String.Format(format, source, target, input); WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; string output = null; for (int tries = 1; tries <= TRIES_NUM; tries++) { try { output = webClient.DownloadString(url); if (gtFailure.Match(output).Success) { ProgressForm.Bug("Random Google failure, retrying..."); continue; } } catch (WebException) { ProgressForm.Bug("Google request failed."); Thread.Sleep(TRIES_WAIT); } } if (output == null) { return(false); } foreach (Match match in gtJsonExtract.Matches(output)) { result += match.Groups[1].Value; } result = result.Replace("\\\"", "\"") .Replace(@"\/", "/") .Replace(@"\r", "\r") .Replace(@"\n", "\n") .Replace(@"\\", @"\"); if (result == "") { ProgressForm.Bug("Failed to capture result. Input:\r\n" + input + "\r\nOutput:\r\n" + output); return(false); } else { return(true); } }
public static string TranslateMatch(Match match, string extension) { if (!cyrillicRx.Match(match.Value).Success || stringIDs.Contains(match.Value)) { return(null); } Group textGroup = match.Groups["text"]; string plainText = textGroup.Value; string left = match.Value.Substring(0, textGroup.Index - match.Index); string right = match.Value.Substring(textGroup.Index - match.Index + textGroup.Length); // Pre-processing if (extension == ".ltx") { if (Regex.Match(plainText, @"\{[+-][^\}]+\}").Success) { ProgressForm.Bug("Aborting for condlist: " + plainText); return(null); } if (plainText[0] == '"' && plainText[plainText.Length - 1] == '"') { plainText = plainText.Substring(1, plainText.Length - 2); } } else if (extension == ".xml") { plainText = HttpUtility.HtmlDecode(plainText); } else if (extension == ".script") { plainText = plainText.Replace("\\\"", "\""); } string result = TranslateString(plainText, extension); // Post-processing if (extension == ".ltx") { result = result.Replace("\"", "'"); if (Regex.Match(result, @"\s").Success) { result = "\"" + result + "\""; } } else if (extension == ".xml") { result = SecurityElement.Escape(result); // Xray crashes with "dest string less than needed" if lines are too long bool success; do { success = true; foreach (Match line in Regex.Matches(result, @"[^\r\n]+")) { if (line.Length > 4000) { string leftHalf, rightHalf; HalveText(line.Value, out leftHalf, out rightHalf); result = (line.Index > 0 ? result.Substring(0, line.Index) : "") + leftHalf + "\r\n" + rightHalf + (line.Index + line.Length < result.Length ? result.Substring(line.Index + line.Length) : ""); success = false; break; } } } while (!success); } else if (extension == ".script") { left = "\""; right = "\""; result = rawQuoteRx.Replace(result, @"\"""); } return(left + result + right); }
public ProgressForm() { current = this; InitializeComponent(); }