123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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<string, TranslationQuery> requests, Action<Dictionary<string, TranslationQuery>, string> OnTranslationReady)
- {
- this._requests = requests;
- this._OnTranslationReady = OnTranslationReady;
- this.FindAllQueries();
- this.ExecuteNextBatch();
- }
- private void FindAllQueries()
- {
- this.mQueries = new List<KeyValuePair<string, string>>();
- foreach (KeyValuePair<string, TranslationQuery> keyValuePair in this._requests)
- {
- foreach (string str in keyValuePair.Value.TargetLanguagesCode)
- {
- this.mQueries.Add(new KeyValuePair<string, string>(keyValuePair.Value.OrigText, keyValuePair.Value.LanguageCode + ":" + str));
- }
- }
- this.mQueries.Sort((KeyValuePair<string, string> a, KeyValuePair<string, string> 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>();
- 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("<br>", "\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<string, TranslationQuery> _requests;
- private Action<Dictionary<string, TranslationQuery>, string> _OnTranslationReady;
- public string mErrorMessage;
- private string mCurrentBatch_ToLanguageCode;
- private string mCurrentBatch_FromLanguageCode;
- private List<string> mCurrentBatch_Text;
- private List<KeyValuePair<string, string>> mQueries;
- }
- }
|