コード例 #1
0
        /// <summary>
        /// </summary>
        /// <param name="line"></param>
        /// <param name="isJP"></param>
        /// <returns></returns>
        private static GoogleTranslateResult ResolveGoogleTranslateResult(string line, bool isJP)
        {
            GoogleTranslateResult result = null;
            var outLang = GoogleTranslate.Offsets[Settings.Default.TranslateTo].ToString();

            if (Settings.Default.TranslateJPOnly)
            {
                if (isJP)
                {
                    result = GoogleTranslate.Translate(line, "ja", outLang, true);
                }
            }
            else
            {
                result = GoogleTranslate.Translate(
                    line,
                    isJP
                        ? "ja"
                        : "en",
                    outLang,
                    true);
            }

            return(result);
        }
コード例 #2
0
        /// <summary>
        /// </summary>
        /// <param name="line"></param>
        /// <param name="isJP"></param>
        /// <param name="resultOnly"></param>
        public static GoogleTranslateResult GetAutomaticResult(string line, bool isJP, bool resultOnly = false)
        {
            Logging.Log(Logger, "Begin Translation");
            var timeStampColor = Settings.Default.TimeStampColor.ToString();
            var player         = line.Substring(0, line.IndexOf(":", StringComparison.Ordinal)) + ": ";
            var message        = line.Substring(line.IndexOf(":", StringComparison.Ordinal) + 1);

            Logging.Log(Logger, $"Player [{player}] said [{message}]");

            GoogleTranslateResult result = ResolveGoogleTranslateResult(message, isJP);

            Logging.Log(Logger, $"Translation Result: {result?.Translated}");
            Logging.Log(Logger, $"Translation Result: {result?.Romanization}");

            if (result != null)
            {
                if (result.Translated.Length <= 0 || string.Equals(line, result.Translated, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(new GoogleTranslateResult {
                        Original = line
                    });
                }
            }

            if (!resultOnly && result != null)
            {
                Constants.FD.AppendFlow(
                    player,
                    string.Empty,
                    result.Translated,
                    new[] {
                    timeStampColor,
                    "#EAFF00"
                },
                    MainView.View.TranslatedFD._FDR);
                if (TranslationWidget.View.IsVisible)
                {
                    Constants.FD.AppendFlow(
                        player,
                        string.Empty,
                        result.Translated,
                        new[] {
                        timeStampColor,
                        "#EAFF00"
                    },
                        TranslationWidget.View.TranslatedFD._FDR);
                }
            }

            return(result);
        }
コード例 #3
0
        /// <summary>
        /// </summary>
        /// <param name="textToTranslate"> </param>
        /// <param name="inLang"> </param>
        /// <param name="outLang"> </param>
        /// <param name="jpOnly"> </param>
        /// <returns> </returns>
        public static GoogleTranslateResult Translate(string textToTranslate, string inLang, string outLang, bool jpOnly)
        {
            Logging.Log(Logger, "Calling Google");

            var result = new GoogleTranslateResult {
                Original = textToTranslate
            };

            try {
                var url = string.Format(
                    _baseUrl,
                    textToTranslate,
                    jpOnly
                        ? inLang
                        : "auto",
                    outLang);

                Logging.Log(Logger, $"Resolved URL: {url}");

                HTTPClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4");

                using (Task <Stream> response = HTTPClient.GetStreamAsync(new Uri(url))) {
                    Stream stream = response.Result;

                    var doc = new HtmlDocument();
                    doc.Load(stream, Encoding.UTF8);

                    HtmlNode translated = doc.DocumentNode.SelectSingleNode("//span[@id='result_box']");
                    if (translated != null)
                    {
                        Logging.Log(Logger, $"Translated: {translated.InnerText}");
                        result.Translated = HttpUtility.HtmlDecode(translated.InnerText);
                    }

                    HtmlNode romanization = doc.DocumentNode.SelectSingleNode("//div[@id='res-translit']");
                    if (romanization != null)
                    {
                        Logging.Log(Logger, $"Romanized: {romanization.InnerText}");
                        result.Romanization = HttpUtility.HtmlDecode(romanization.InnerText);
                    }
                }
            }
            catch (Exception ex) {
                Logging.Log(Logger, new LogItem(ex, true));
            }

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// </summary>
        /// <param name="value"> </param>
        private static void ManualTranslate(string value)
        {
            value = value.Trim();
            var outLang = GoogleTranslate.Offsets[Settings.Default.ManualTranslate].ToString();

            if (value.Length <= 0)
            {
                return;
            }

            GoogleTranslateResult tmpTranString = Translate.GetManualResult(value, outLang, false);

            MainView.View.Chatter.Text = Settings.Default.UseRomanization
                                             ? tmpTranString.Romanization
                                             : tmpTranString.Translated;
        }
コード例 #5
0
        /// <summary>
        /// </summary>
        /// <param name="textToTranslate"> </param>
        /// <param name="inLang"> </param>
        /// <param name="outLang"> </param>
        /// <param name="jpOnly"> </param>
        /// <returns> </returns>
        public static GoogleTranslateResult Translate(string textToTranslate, string inLang, string outLang, bool jpOnly)
        {
            var result = new GoogleTranslateResult
            {
                Original = textToTranslate
            };

            try
            {
                if (jpOnly)
                {
                    var url = String.Format("{0}{1}&sl={2}&tl={3}#", _baseUrl, textToTranslate, inLang, outLang);
                    _httpWReq = (HttpWebRequest)WebRequest.Create(url);
                }
                else
                {
                    var url = String.Format("{0}{1}&sl=auto&tl={2}#auto|{2}|{1}", _baseUrl, textToTranslate, outLang);
                    _httpWReq = (HttpWebRequest)WebRequest.Create(url);
                }
                _httpWReq.UserAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.70 Safari/533.4";
                _httpWResp          = (HttpWebResponse)_httpWReq.GetResponse();
                var stream = _httpWResp.GetResponseStream();
                if (_httpWResp.StatusCode != HttpStatusCode.OK || stream == null)
                {
                }
                else
                {
                    var doc = new HtmlDocument();
                    doc.Load(stream, true);
                    var translated = doc.DocumentNode.SelectSingleNode("//span[@id='result_box']");
                    if (translated != null)
                    {
                        result.Translated = HttpUtility.HtmlDecode(translated.InnerText);
                    }
                    var romanization = doc.DocumentNode.SelectSingleNode("//div[@id='res-translit']");
                    if (romanization != null)
                    {
                        result.Romanization = HttpUtility.HtmlDecode(romanization.InnerText);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return(result);
        }