コード例 #1
0
        public string Translate(string sentence, string inLang, string outLang)
        {
            string result = String.Empty;

            try
            {
                string _outLang = outLang;
                string _inLang  = inLang;

                string _baseUrl = "https://translate.google.com/m?hl=ru&sl={0}&tl={1}&ie=UTF-8&prev=_m&q={2}";
                string url      = string.Format(_baseUrl, _inLang, _outLang, System.Web.HttpUtility.UrlEncode(sentence));

                var tmpResult = _GoogleWebReader.RequestWebData(url, HttpUtilities.HttpMethods.GET);

                var rxMatch = GoogleRx.Match(tmpResult.Body);

                if (rxMatch.Success)
                {
                    result = System.Net.WebUtility.HtmlDecode(rxMatch.Value);
                }
            }
            catch (Exception e)
            {
                _Logger?.WriteLog(Convert.ToString(e));
            }

            return(result);
        }
コード例 #2
0
        private void InitTranslator()
        {
            _IsInitialised        = false;
            _InitializationFailed = false;

            Task.Run(() =>
            {
                try
                {
                    _BaiduEncoder = new BaiduReqEncoder(GlobalTranslationSettings.BaiduEncoder, _Logger);

                    string url = "https://fanyi.baidu.com/";

                    var tmpResult = _BaiduWebRead.RequestWebData(url, HttpUtilities.HttpMethods.GET);

                    tmpResult = _BaiduWebRead.RequestWebData(url, HttpUtilities.HttpMethods.GET, true);

                    Regex tokenRegex = new Regex("token: '(.*)'");
                    Regex gtkRegex   = new Regex("gtk = '(.*)'");

                    Match tokenMatch = tokenRegex.Match(tmpResult.Body);
                    Match gtkMatch   = gtkRegex.Match(tmpResult.Body);

                    if (tokenMatch.Success && gtkMatch.Success)
                    {
                        _Token = tokenMatch.Value;
                        _Gtk   = gtkMatch.Value;
                        _Token = _Token.Substring(8, _Token.Length - 8).TrimEnd(new char[] { (char)39 });
                        _Gtk   = _Gtk.Substring(7, _Gtk.Length - 7).TrimEnd(new char[] { (char)39 });

                        _IsInitialised = true;
                    }
                    else
                    {
                        _IsInitialised        = false;
                        _InitializationFailed = true;
                    }
                }
                catch (Exception e)
                {
                    _InitializationFailed = true;
                    _IsInitialised        = false;
                    _Logger?.WriteLog(e.ToString());
                }
            });
        }
コード例 #3
0
        public string Translate(string sentence, string inLang, string outLang)
        {
            sentence = sentence.Replace(":", " : ");
            string result = string.Empty;
            string url    = @"https://papago.naver.com/apis/n2mt/translate";

            if (_PapagoEncoder == null)
            {
                _PapagoEncoder = new PapagoEncoder(GlobalTranslationSettings.PapagoEncoderPath, _Logger);
            }

            /*
             * if (inLang == "auto")
             *  inLang = DetectLanguage(sentence);//*/
            if (inLang.Length == 0)
            {
                return(result);
            }

            if (_PapagoEncoder.IsAvaliable)
            {
                try
                {
                    PapagoTranslationRequest papagoRequest = new PapagoTranslationRequest()
                    {
                        deviceId    = "",
                        dict        = false,
                        dictDisplay = 0,
                        honorific   = false,
                        instant     = false,
                        paging      = false,
                        source      = inLang,
                        target      = outLang,
                        locale      = "ko-KR",
                        text        = Uri.EscapeDataString(sentence)
                    };

                    var reqvObj = _PapagoEncoder.EncodePapagoTranslationRequest(papagoRequest);

                    if (reqvObj != null)
                    {
                        _PapagoReader.OptionalHeaders.Clear();

                        _PapagoReader.OptionalHeaders.Add("device-type", "pc");
                        _PapagoReader.OptionalHeaders.Add("x-apigw-partnerid", "papago");
                        _PapagoReader.OptionalHeaders.Add("Origin", @"https://papago.naver.com");
                        _PapagoReader.OptionalHeaders.Add("Sec-Fetch-Site", "same-origin");
                        _PapagoReader.OptionalHeaders.Add("Sec-Fetch-Mode", "cors");
                        _PapagoReader.OptionalHeaders.Add("Sec-Fetch-Dest", "empty");

                        _PapagoReader.OptionalHeaders.Add("Authorization", reqvObj.AuthorizationHeader);
                        _PapagoReader.OptionalHeaders.Add("Timestamp", reqvObj.Timestamp);

                        var webResponse = _PapagoReader.RequestWebData(url, HttpUtilities.HttpMethods.POST, reqvObj.StringRequest, true);

                        PapagoResponse papagoResponse = JsonConvert.DeserializeObject <PapagoResponse>(webResponse.Body);

                        result = papagoResponse.translatedText;
                    }
                    else
                    {
                        _Logger?.WriteLog("reqvObj != null");
                    }
                }
                catch (Exception e)
                {
                    _Logger?.WriteLog(e.ToString());
                }
            }

            return(result);
        }
コード例 #4
0
        public string Translate(string sentence, string inLang, string outLang)
        {
            string result = string.Empty;

            if (!_InitializationFailed)
            {
                if (!_IsInitialised)
                {
                    SpinWait.SpinUntil(() => _IsInitialised || _InitializationFailed);
                }

                if (!_InitializationFailed)
                {
                    if (!_RequestsEncoder.IsAvaliable)
                    {
                        return(string.Empty);
                    }

                    try
                    {
                        var request = _RequestsEncoder.Encode(sentence, inLang, outLang);

                        if (request != null)
                        {
                            var reqvUrl      = string.Format("https://translate.yandex.net/api/v1/tr.json/translate?{0}", request.UrlParams);
                            var requestBodey = string.Format("text={0}&options=4", Uri.EscapeDataString(sentence));

                            //var response = _YandexWebReader.RequestWebData(reqvUrl, HttpUtilities.HttpMethods.POST, request.BodyRequest);
                            var response = _YandexWebReader.RequestWebData(reqvUrl, HttpUtilities.HttpMethods.POST, requestBodey);

                            if (response.IsSuccessful)
                            {
                                try
                                {
                                    var translationResponse = JsonConvert.DeserializeObject <TranslationResponse>(response.Body);

                                    if (translationResponse?.Text != null && translationResponse.Text.Count > 0)
                                    {
                                        foreach (var str in translationResponse.Text)
                                        {
                                            result += str + " ";
                                        }
                                    }
                                }
                                catch (Exception e)
                                {
                                    request.YandexSession.IsBad = true;
                                }
                            }
                            else
                            {
                                request.YandexSession.IsBad = true;
                            }

                            if (result.Length < 1 && request?.YandexSession != null)
                            {
                                request.YandexSession.IsBad = true;
                            }
                            if (result.Length > 1)
                            {
                                result = result.Replace(":", ": ");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _Logger?.WriteLog(ex?.ToString() ?? "Exception is null");
                    }
                }
            }

            return(result);
        }