示例#1
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,
                        text        = sentence
                    };

                    var reqv = _PapagoEncoder.EncodePapagoTranslationRequest(JsonConvert.SerializeObject(papagoRequest));

                    var tmpResponse = PapagoReader.GetWebData(url, WebApi.WebReader.WebMethods.POST, reqv);

                    PapagoResponse papagoResponse = JsonConvert.DeserializeObject <PapagoResponse>(tmpResponse);

                    result = papagoResponse.translatedText;
                }
                catch (Exception e)
                {
                    _Logger?.WriteLog(e.ToString());
                }
            }

            return(result);
        }
示例#2
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);
        }