public async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            if (!_bingConfiguration.CanSupport() || !_bingConfiguration.IsActive())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            var requestObject = new
            {
                from = _applicationConfiguration.FromLanguage.Name.ToLower(),
                to   = _applicationConfiguration.ToLanguage.Name.ToLower(),
                text = translateRequest.CurrentText
            };

            IRestResponse response = await _restClient
                                     .Manipulate(client => client.BaseUrl = _bingConfiguration.Url.ToUri())
                                     .ExecutePostTaskAsync(new RestRequest(Method.POST)
                                                           .AddHeader(ContentTypeName, ContentType)
                                                           .AddParameter(ContentType, requestObject.ToJsonString(true), ParameterType.RequestBody)
                                                           );

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                IMeanOrganizer meanOrganizer = _meanOrganizerFactory.GetMeanOrganizers().First(x => x.TranslatorType == TranslatorType);
                mean = await meanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        public async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            if (!_googleConfiguration.CanSupport() || !_googleConfiguration.IsActive())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            string uri = string.Format(
                _googleConfiguration.Url,
                _applicationConfiguration.ToLanguage.Extension,
                _applicationConfiguration.ToLanguage.Extension,
                HttpUtility.UrlEncode(translateRequest.CurrentText, Encoding.UTF8));

            IRestResponse response = await _restClient.Manipulate(client =>
            {
                client.BaseUrl  = uri.ToUri();
                client.Encoding = Encoding.UTF8;
            })
                                     .ExecuteGetTaskAsync(
                new RestRequest(Method.GET)
                .AddHeader(Headers.AcceptLanguage, AcceptLanguage)
                .AddHeader(Headers.AcceptEncoding, AcceptEncoding)
                .AddHeader(Headers.UserAgent, UserAgent)
                .AddHeader(Headers.Accept, Accept));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                IMeanOrganizer organizer = _meanOrganizerFactory.GetMeanOrganizers().First(x => x.TranslatorType == TranslatorType);
                mean = await organizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        public async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            if (!_sesliSozlukConfiguration.CanSupport() || !_sesliSozlukConfiguration.IsActive())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            string parameter = $"sl=auto&text={Uri.EscapeUriString(translateRequest.CurrentText)}&tl={_applicationConfiguration.ToLanguage.Extension}";

            IRestResponse response = await _restClient.Manipulate(client =>
            {
                client.BaseUrl     = _sesliSozlukConfiguration.Url.ToUri();
                client.Encoding    = Encoding.UTF8;
                client.CachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromHours(1));
            }).ExecutePostTaskAsync(
                new RestRequest(Method.POST)
                .AddHeader(Headers.AcceptLanguage, AcceptLanguage)
                .AddHeader(Headers.AcceptEncoding, AcceptEncoding)
                .AddHeader(Headers.ContentType, ContentType)
                .AddHeader(Headers.UserAgent, UserAgent)
                .AddHeader(Headers.Accept, Accept)
                .AddParameter(ContentType, parameter, ParameterType.RequestBody));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                IMeanOrganizer meanOrganizer = _meanOrganizerFactory.GetMeanOrganizers().First(x => x.TranslatorType == TranslatorType);
                mean = await meanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
        public async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            if (!_turengConfiguration.CanSupport())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            if (!_turengConfiguration.IsActive())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            var uri = new Uri(_turengConfiguration.Url + translateRequest.CurrentText);

            IRestResponse response = await _restClient
                                     .Manipulate(client =>
            {
                client.BaseUrl     = uri;
                client.Encoding    = Encoding.UTF8;
                client.CachePolicy = new HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromHours(1));
            }).ExecuteGetTaskAsync(
                new RestRequest(Method.GET)
                .AddHeader(Headers.UserAgent, UserAgent)
                .AddHeader(Headers.AcceptLanguage, AcceptLanguage));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                IMeanOrganizer organizer = _meanOrganizerFactory.GetMeanOrganizers().First(x => x.TranslatorType == TranslatorType);
                mean = await organizer.OrganizeMean(response.Content, translateRequest.FromLanguageExtension);
            }

            return(new TranslateResult(true, mean));
        }
        public async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            if (!_yandexConfiguration.CanSupport() || !_yandexConfiguration.IsActive())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            Uri           address;
            IRestResponse response;

            if (_yandexConfiguration.ShouldBeAnonymous)
            {
                address = new Uri(string.Format(_yandexConfiguration.Url +
                                                new StringBuilder()
                                                .Append($"id={_yandexConfiguration.SId}")
                                                .Append(Headers.Ampersand)
                                                .Append("srv=tr-text")
                                                .Append(Headers.Ampersand)
                                                .Append($"lang={translateRequest.FromLanguageExtension}-{_applicationConfiguration.ToLanguage.Extension}")
                                                .Append(Headers.Ampersand)
                                                .Append($"text={Uri.EscapeUriString(translateRequest.CurrentText)}")));

                response = await _restClient.Manipulate(client => { client.BaseUrl = address; })
                           .ExecutePostTaskAsync(new RestRequest(Method.POST)
                                                 .AddParameter(Headers.ContentTypeDefinition, $"text={translateRequest.CurrentText}"));
            }
            else
            {
                address = new Uri(string.Format(_yandexConfiguration.Url +
                                                new StringBuilder()
                                                .Append($"key={_yandexConfiguration.ApiKey}")
                                                .Append(Headers.Ampersand)
                                                .Append($"lang={translateRequest.FromLanguageExtension}-{_applicationConfiguration.ToLanguage.Extension}")
                                                .Append(Headers.Ampersand)
                                                .Append($"text={Uri.EscapeUriString(translateRequest.CurrentText)}")));

                response = await _restClient.Manipulate(client => { client.BaseUrl = address; }).ExecutePostTaskAsync(new RestRequest(Method.POST));
            }

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                IMeanOrganizer organizer = _meanOrganizerFactory.GetMeanOrganizers().First(x => x.TranslatorType == TranslatorType);
                mean = await organizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }
Ejemplo n.º 6
0
        public async Task <TranslateResult> Find(TranslateRequest translateRequest)
        {
            if (!_promptConfiguration.CanSupport() || !_promptConfiguration.IsActive())
            {
                return(new TranslateResult(false, new Maybe <string>()));
            }

            var requestObject = new
            {
                dirCode       = $"{translateRequest.FromLanguageExtension}-{_applicationConfiguration.ToLanguage.Extension}",
                template      = _promptConfiguration.Template,
                text          = translateRequest.CurrentText,
                lang          = translateRequest.FromLanguageExtension,
                limit         = _promptConfiguration.Limit,
                useAutoDetect = true,
                key           = string.Empty,
                ts            = _promptConfiguration.Ts,
                tid           = string.Empty,
                IsMobile      = false
            };

            IRestResponse response = await _restClient
                                     .Manipulate(client =>
            {
                client.BaseUrl = _promptConfiguration.Url.ToUri();
            }).ExecutePostTaskAsync(new RestRequest(Method.POST)
                                    .AddHeader(ContentTypeName, ContentType)
                                    .AddParameter(ContentType, requestObject.ToJsonString(false), ParameterType.RequestBody));

            var mean = new Maybe <string>();

            if (response.Ok())
            {
                IMeanOrganizer meanOrganizer = _meanOrganizerFactory.GetMeanOrganizers().First(x => x.TranslatorType == TranslatorType);
                mean = await meanOrganizer.OrganizeMean(response.Content);
            }

            return(new TranslateResult(true, mean));
        }