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

            if (inLang == outLang)
            {
                return(sentence);
            }

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

                if (_DeepLId > 83931525)
                {
                    _DeepLId       = Rnd.Next(33931525, 53931525);
                    DeeplWebReader = new WebApi.DeepLWebReader(_Logger);

                    _IsFirstTime = true;
                }

                if (_IsFirstTime)
                {
                    _IsFirstTime = false;

                    DeepLRequest.DeepLHandshakeRequest deepLHandshakeRequest = new DeepLRequest.DeepLHandshakeRequest(_DeepLId);

                    string handShakeUrl             = @"https://www.deepl.com/PHP/backend/clientState.php?request_type=jsonrpc&il=RU";
                    string strDeepLHandshakeRequest = JsonConvert.SerializeObject(deepLHandshakeRequest);

                    var strDeepLHandshakeResp = DeeplWebReader.GetWebData(handShakeUrl, WebApi.DeepLWebReader.WebMethods.POST, strDeepLHandshakeRequest);

                    var DeepLHandshakeResp = JsonConvert.DeserializeObject <DeepLResponse.DeepLHandshakeResponse>(strDeepLHandshakeResp);

                    _Logger?.WriteLog(strDeepLHandshakeResp);

                    _DeepLId++;

                    _IsFirstTime = false;
                }

                string url = @"https://www2.deepl.com/jsonrpc";

                if (_ServerSplit || inLang == "auto")
                {
                    DeepLRequest.DeepLCookieRequest deepLSentenceRequest = new DeepLRequest.DeepLCookieRequest(_DeepLId, sentence);
                    string strDeepLsentenceRequest = deepLSentenceRequest.ToJsonString();

                    var strDeepLSentencetResp = DeeplWebReader.GetWebData(url, WebApi.DeepLWebReader.WebMethods.POST, strDeepLsentenceRequest);
                    var deepLSentenceResp     = JsonConvert.DeserializeObject <DeepLResponse.DeepLSentencePreprocessResponse>(strDeepLSentencetResp);

                    _DeepLId++;

                    inLang = deepLSentenceResp.result.lang;
                    if (inLang.Length == 0)
                    {
                        return(result);
                    }
                }

                DeepLRequest.DeepLTranslatorRequest deepLTranslationRequest = new DeepLRequest.DeepLTranslatorRequest(_DeepLId, sentence, inLang, outLang);
                string strDeepLTranslationRequest = deepLTranslationRequest.ToJsonString();

                var strDeepLTranslationResponse = DeeplWebReader.GetWebData(url, WebApi.DeepLWebReader.WebMethods.POST, strDeepLTranslationRequest);
                _DeepLId++;

                var DeepLTranslationResponse = JsonConvert.DeserializeObject <DeepLResponse.DeepLTranslationResponse>(strDeepLTranslationResponse);


                StringBuilder temporaryResult = new StringBuilder();
                if (DeepLTranslationResponse?.ResponseResult?.Translations != null)
                {
                    List <DeepLResponse.DeepLTranslationResponse.Translation> translations = DeepLTranslationResponse.ResponseResult.Translations;
                    foreach (DeepLResponse.DeepLTranslationResponse.Translation transaltion in translations)
                    {
                        var beam = transaltion.Beams.FirstOrDefault();
                        if (beam?.PostprocessedSentence != null)
                        {
                            temporaryResult.Append(beam.PostprocessedSentence);
                            temporaryResult.Append(" ");
                        }
                    }
                }//*/

                result = temporaryResult.ToString();
            }
            catch (Exception e)
            {
                _Logger?.WriteLog(Convert.ToString(e));
            }

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

            if (inLang == outLang)
            {
                return(sentence);
            }

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

                if (_DeepLId > 83931525)
                {
                    _DeepLId       = Rnd.Next(33931525, 53931525);
                    DeeplWebReader = new WebApi.DeepLWebReader(_Logger);

                    _IsFirstTime = true;
                }

                if (_IsFirstTime)
                {
                    _IsFirstTime = false;

                    DeepLRequest.DeepLHandshakeRequest deepLHandshakeRequest = new DeepLRequest.DeepLHandshakeRequest(_DeepLId);

                    string handShakeUrl             = @"https://www.deepl.com/PHP/backend/clientState.php?request_type=jsonrpc&il=RU";
                    string strDeepLHandshakeRequest = JsonConvert.SerializeObject(deepLHandshakeRequest);

                    var strDeepLHandshakeResp = DeeplWebReader.GetWebData(handShakeUrl, WebApi.WebReader.WebMethods.POST, strDeepLHandshakeRequest);
                    var DeepLHandshakeResp    = JsonConvert.DeserializeObject <DeepLResponse.DeepLHandshakeResponse>(strDeepLHandshakeResp);

                    _Logger?.WriteLog(strDeepLHandshakeResp);

                    _DeepLId++;

                    _IsFirstTime = false;
                }

                string url = @"https://www2.deepl.com/jsonrpc";

                if (_ServerSplit || inLang == "auto")
                {
                    DeepLRequest.DeepLCookieRequest deepLSentenceRequest = new DeepLRequest.DeepLCookieRequest(_DeepLId, sentence);
                    string strDeepLsentenceRequest = deepLSentenceRequest.ToJsonString();

                    var strDeepLSentencetResp = DeeplWebReader.GetWebData(url, WebApi.WebReader.WebMethods.POST, strDeepLsentenceRequest);
                    var deepLSentenceResp     = JsonConvert.DeserializeObject <DeepLResponse.DeepLSentencePreprocessResponse>(strDeepLSentencetResp);

                    _DeepLId++;

                    inLang = deepLSentenceResp.result.lang;
                    if (inLang.Length == 0)
                    {
                        return(result);
                    }
                }

                DeepLRequest.DeepLTranslatorRequest deepLTranslationRequest = new DeepLRequest.DeepLTranslatorRequest(_DeepLId, sentence, inLang, outLang);
                string strDeepLTranslationRequest = deepLTranslationRequest.ToJsonString();

                var strDeepLTranslationResponse = DeeplWebReader.GetWebData(url, WebApi.WebReader.WebMethods.POST, strDeepLTranslationRequest);
                _DeepLId++;

                var DeepLTranslationResponse = JsonConvert.DeserializeObject <DeepLResponse.DeepLTranslationResponse>(strDeepLTranslationResponse);

                string temporaryResult = String.Empty;
                if (DeepLTranslationResponse != null)
                {
                    var translations = DeepLTranslationResponse.result.translations;
                    for (int i = 0; i < translations.Count; i++)
                    {
                        if (translations[i].beams.Count > 0)
                        {
                            temporaryResult += " " + translations[i].beams[0].postprocessed_sentence;
                        }
                    }
                }

                result = temporaryResult;
            }
            catch (Exception e)
            {
                _Logger?.WriteLog(Convert.ToString(e));
            }

            return(result);
        }