using System; using System.Collections.Generic; using System.Globalization; using System.Text; using System.Text.RegularExpressions; using UnityEngine; namespace I2.Loc { public class TranslationJob_WEB : TranslationJob_WWW { public TranslationJob_WEB(Dictionary requests, Action, string> OnTranslationReady) { this._requests = requests; this._OnTranslationReady = OnTranslationReady; this.FindAllQueries(); this.ExecuteNextBatch(); } private void FindAllQueries() { this.mQueries = new List>(); foreach (KeyValuePair keyValuePair in this._requests) { foreach (string str in keyValuePair.Value.TargetLanguagesCode) { this.mQueries.Add(new KeyValuePair(keyValuePair.Value.OrigText, keyValuePair.Value.LanguageCode + ":" + str)); } } this.mQueries.Sort((KeyValuePair a, KeyValuePair b) => a.Value.CompareTo(b.Value)); } private void ExecuteNextBatch() { if (this.mQueries.Count == 0) { this.mJobState = TranslationJob.eJobState.Succeeded; return; } this.mCurrentBatch_Text = new List(); string text = null; int num = 200; StringBuilder stringBuilder = new StringBuilder(); int i; for (i = 0; i < this.mQueries.Count; i++) { string key = this.mQueries[i].Key; string value = this.mQueries[i].Value; if (text == null || value == text) { if (i != 0) { stringBuilder.Append("|||"); } stringBuilder.Append(key); this.mCurrentBatch_Text.Add(key); text = value; } if (stringBuilder.Length > num) { break; } } this.mQueries.RemoveRange(0, i); string[] array = text.Split(new char[] { ':' }); this.mCurrentBatch_FromLanguageCode = array[0]; this.mCurrentBatch_ToLanguageCode = array[1]; string text2 = string.Format("http://www.google.com/translate_t?hl=en&vi=c&ie=UTF8&oe=UTF8&submit=Translate&langpair={0}|{1}&text={2}", this.mCurrentBatch_FromLanguageCode, this.mCurrentBatch_ToLanguageCode, Uri.EscapeUriString(stringBuilder.ToString())); Debug.Log(text2); this.www = new WWW(text2); } public override TranslationJob.eJobState GetState() { if (this.www != null && this.www.isDone) { this.ProcessResult(this.www.bytes, this.www.error); this.www.Dispose(); this.www = null; } if (this.www == null) { this.ExecuteNextBatch(); } return this.mJobState; } public void ProcessResult(byte[] bytes, string errorMsg) { if (string.IsNullOrEmpty(errorMsg)) { string @string = Encoding.UTF8.GetString(bytes, 0, bytes.Length); string message = this.ParseTranslationResult(@string, "aab"); Debug.Log(message); if (string.IsNullOrEmpty(errorMsg)) { if (this._OnTranslationReady != null) { this._OnTranslationReady(this._requests, null); } return; } } this.mJobState = TranslationJob.eJobState.Failed; this.mErrorMessage = errorMsg; } private string ParseTranslationResult(string html, string OriginalText) { string result; try { int num = html.IndexOf("TRANSLATED_TEXT='") + "TRANSLATED_TEXT='".Length; int num2 = html.IndexOf("';var", num); string text = html.Substring(num, num2 - num); text = Regex.Replace(text, "\\\\x([a-fA-F0-9]{2})", (Match match) => char.ConvertFromUtf32(int.Parse(match.Groups[1].Value, NumberStyles.HexNumber))); text = Regex.Replace(text, "&#(\\d+);", (Match match) => char.ConvertFromUtf32(int.Parse(match.Groups[1].Value))); text = text.Replace("
", "\n"); if (OriginalText.ToUpper() == OriginalText) { text = text.ToUpper(); } else if (GoogleTranslation.UppercaseFirst(OriginalText) == OriginalText) { text = GoogleTranslation.UppercaseFirst(text); } else if (GoogleTranslation.TitleCase(OriginalText) == OriginalText) { text = GoogleTranslation.TitleCase(text); } result = text; } catch (Exception ex) { Debug.LogError(ex.Message); result = string.Empty; } return result; } private Dictionary _requests; private Action, string> _OnTranslationReady; public string mErrorMessage; private string mCurrentBatch_ToLanguageCode; private string mCurrentBatch_FromLanguageCode; private List mCurrentBatch_Text; private List> mQueries; } }