Esempio n. 1
0
        private RestCall BuildRestCall(TextTranslationRequest request)
        {
            var apiKey  = _settingsProvider.ProvideSettings().DeeplApiKey;
            var builder = _restCallBuilderFactory
                          .StartBuilding(new Uri("https://api.deepl.com/v2/translate"), RestCallMethodType.Post);

            var queryParamBuilder = builder.WithQueryParameters()
                                    .WithQueryParameter("auth_key", apiKey)
                                    .WithQueryParameter("target_lang", request.TargetLanguage.Code);

            request.SourceLanguage.Evaluate(language =>
            {
                queryParamBuilder.WithQueryParameter("source_lang", language.Code);
            });

            request.ApplyIgnoreMarkUp(queryParamBuilder);
            request.TextParts.ForEach(tp =>
            {
                queryParamBuilder.WithQueryParameter("text", tp.Text);
            });

            builder = queryParamBuilder.BuildQueryParameters();

            return(builder.Build());
        }
        public TextTranslationResult Adapt(TextTranslationRequest request, TextTranslationResultDto resultDto)
        {
            var translations = new List <TranslatedText>();

            for (var i = 0; i < resultDto.Translations.Count; i++)
            {
                var translationDto         = resultDto.Translations.ElementAt(i);
                var detectedSourceLanguage = TranslationLanguage.CreateByCode(translationDto.Detected_source_language);
                var textKey = request.TextParts.ElementAt(i).Key;
                var text    = translationDto.Text;

                request.IgnoreMarkup.Evaluate(markup =>
                {
                    text = text.Replace(IgnoreForTranslationMarkup.IgnoreBeginTag, markup.BeginTag, StringComparison.Ordinal);
                    text = text.Replace(IgnoreForTranslationMarkup.IgnoreEndTag, markup.EndTag, StringComparison.Ordinal);
                });

                var identifiableText = new IdentifiableText(textKey, text);
                translations.Add(new TranslatedText(detectedSourceLanguage, identifiableText));
            }

            var result = new TextTranslationResult(translations);

            return(result);
        }
Esempio n. 3
0
        public async Task <TextTranslationResult> TranslateAsync(TextTranslationRequest request)
        {
            var restCall = BuildRestCall(request);

            var response = await _restProxy.PerformCallAsync <TextTranslationResultDto>(restCall);

            var result = _resultAdapter.Adapt(request, response);

            return(result);
        }