public DeepLMtTranslationProviderLanguageDirection(DeepLMtTranslationProvider deepLMtTranslationProvider, LanguagePair languageDirection)
 {
     _deepLMtTranslationProvider = deepLMtTranslationProvider;
     _languageDirection          = languageDirection;
     _options = deepLMtTranslationProvider.Options;
     _normalizeSourceTextHelper = new NormalizeSourceTextHelper();
 }
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            _formality = Helpers.IsLanguageCompatible(languageDirection.TargetCulture) ? _formality : Formality.Default;

            var targetLanguage  = GetLanguage(languageDirection.TargetCulture, SupportedTargetLanguages);
            var sourceLanguage  = GetLanguage(languageDirection.SourceCulture, SupportedSourceLanguages);
            var translatedText  = string.Empty;
            var normalizeHelper = new NormalizeSourceTextHelper();

            try
            {
                sourceText = normalizeHelper.NormalizeText(sourceText);

                using (var httpClient = new HttpClient())
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                    httpClient.Timeout = TimeSpan.FromMinutes(5);
                    var content = new StringContent($"text={sourceText}" +
                                                    $"&source_lang={sourceLanguage}" +
                                                    $"&target_lang={targetLanguage}" +
                                                    $"&formality={_formality.ToString().ToLower()}" +
                                                    "&preserve_formatting=1" +
                                                    "&tag_handling=xml" +
                                                    $"&auth_key={ApiKey}",
                                                    Encoding.UTF8, "application/x-www-form-urlencoded");

                    httpClient.DefaultRequestHeaders.Add("Trace-ID", $"SDL Trados Studio 2021 /plugin {_pluginVersion}");

                    var response = httpClient.PostAsync("https://api.deepl.com/v2/translate", content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var translationResponse = response.Content?.ReadAsStringAsync().Result;
                        var translatedObject    = JsonConvert.DeserializeObject <TranslationResponse>(translationResponse);

                        if (translatedObject != null && translatedObject.Translations.Any())
                        {
                            translatedText = translatedObject.Translations[0].Text;
                            translatedText = DecodeWhenNeeded(translatedText);
                        }
                    }
                    else
                    {
                        var message =
                            $"HTTP Request to DeepL Translate REST API endpoint failed with status code '{response.StatusCode}'. " +
                            $"Response content: {response.Content?.ReadAsStringAsync().Result}.";
                        _logger.Error(message);
                        throw new HttpRequestException(message);
                    }
                }
            }
            catch (Exception e)
            {
                _logger.Error($"{e.Message}\n {e.StackTrace}");
                throw;
            }

            return(translatedText);
        }
Exemplo n.º 3
0
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            var targetLanguage  = languageDirection.TargetCulture.TwoLetterISOLanguageName;
            var sourceLanguage  = languageDirection.SourceCulture.TwoLetterISOLanguageName;
            var translatedText  = string.Empty;
            var normalizeHelper = new NormalizeSourceTextHelper();

            try
            {
                sourceText = normalizeHelper.NormalizeText(sourceText);

                using (var httpClient = new HttpClient())
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

                    httpClient.Timeout = TimeSpan.FromMinutes(5);
                    var content = new StringContent($"text={sourceText}" +
                                                    $"&source_lang={sourceLanguage}" +
                                                    $"&target_lang={targetLanguage}" +
                                                    "&preserve_formatting=1" +
                                                    $"&tag_handling=xml&auth_key={ApiKey}", Encoding.UTF8, "application/x-www-form-urlencoded");

                    httpClient.DefaultRequestHeaders.Add("Trace-ID", $"SDL Trados Studio 2019 /plugin {_pluginVersion}");

                    var response = httpClient.PostAsync("https://api.deepl.com/v1/translate", content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var translationResponse = response.Content?.ReadAsStringAsync().Result;
                        var translatedObject    = JsonConvert.DeserializeObject <TranslationResponse>(translationResponse);

                        if (translatedObject != null && translatedObject.Translations.Any())
                        {
                            translatedText = translatedObject.Translations[0].Text;
                            translatedText = DecodeWhenNeeded(translatedText);
                        }
                    }
                    else
                    {
                        Log.Logger.Error($"HTTP Request to DeepL Translate REST API endpoint failed with status code '{response.StatusCode}'. " +
                                         $"Response content: {response.Content?.ReadAsStringAsync().Result}.");
                        MessageBox.Show(response.ReasonPhrase, string.Empty, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{e.Message}\n {e.StackTrace}");
            }

            return(translatedText);
        }
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            _formality = Helpers.IsLanguageCompatible(languageDirection.TargetCulture) ? _formality : Formality.Default;

            var targetLanguage  = GetLanguage(languageDirection.TargetCulture, SupportedTargetLanguages);
            var sourceLanguage  = GetLanguage(languageDirection.SourceCulture, SupportedSourceLanguages);
            var translatedText  = string.Empty;
            var normalizeHelper = new NormalizeSourceTextHelper();

            try
            {
                sourceText = normalizeHelper.NormalizeText(sourceText);

                var content = new StringContent($"text={sourceText}" +
                                                $"&source_lang={sourceLanguage}" +
                                                $"&target_lang={targetLanguage}" +
                                                $"&formality={_formality.ToString().ToLower()}" +
                                                "&preserve_formatting=1" +
                                                "&tag_handling=xml" +
                                                $"&auth_key={ApiKey}",
                                                Encoding.UTF8, "application/x-www-form-urlencoded");

                var response = DeeplApplicationInitializer.Clinet.PostAsync("https://api.deepl.com/v1/translate", content).Result;
                response.EnsureSuccessStatusCode();

                var translationResponse = response.Content?.ReadAsStringAsync().Result;
                var translatedObject    = JsonConvert.DeserializeObject <TranslationResponse>(translationResponse);

                if (translatedObject != null && translatedObject.Translations.Any())
                {
                    translatedText = translatedObject.Translations[0].Text;
                    translatedText = DecodeWhenNeeded(translatedText);
                }
            }
            catch (AggregateException aEx)
            {
                foreach (var innerEx in aEx.InnerExceptions)
                {
                    _logger.Error(innerEx);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex);
                throw;
            }

            return(translatedText);
        }
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            var targetLanguage  = languageDirection.TargetCulture.TwoLetterISOLanguageName;
            var sourceLanguage  = languageDirection.SourceCulture.TwoLetterISOLanguageName;
            var translatedText  = string.Empty;
            var normalizeHelper = new NormalizeSourceTextHelper();

            try
            {
                sourceText = normalizeHelper.NormalizeText(sourceText);

                using (var httpClient = new HttpClient())
                {
                    var content = new StringContent($"text={sourceText}" +
                                                    $"&source_lang={sourceLanguage}" +
                                                    $"&target_lang={targetLanguage}" +
                                                    "&preserve_formatting=1" +
                                                    $"&tag_handling=xml&auth_key={ApiKey}", Encoding.UTF8, "application/x-www-form-urlencoded");

                    var studioVersion = new Toolkit.Core.Studio().GetStudioVersion().ExecutableVersion;
                    httpClient.DefaultRequestHeaders.Add("Trace-ID", $"SDL Trados Studio 2019 {studioVersion}/plugin {_pluginVersion}");

                    var response = httpClient.PostAsync("https://api.deepl.com/v1/translate", content).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        var translationResponse = response.Content?.ReadAsStringAsync().Result;
                        var translatedObject    = JsonConvert.DeserializeObject <TranslationResponse>(translationResponse);

                        if (translatedObject != null && translatedObject.Translations.Any())
                        {
                            translatedText = translatedObject.Translations[0].Text;
                            translatedText = DecodeWhenNeeded(translatedText);
                        }
                    }
                    else
                    {
                        MessageBox.Show(response.ReasonPhrase, string.Empty, MessageBoxButton.OK, MessageBoxImage.Exclamation);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{e.Message}\n {e.StackTrace}");
            }

            return(translatedText);
        }
Exemplo n.º 6
0
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            const string tagOption       = @"xml";
            var          targetLanguage  = languageDirection.TargetCulture.TwoLetterISOLanguageName;
            var          sourceLanguage  = languageDirection.SourceCulture.TwoLetterISOLanguageName;
            var          translatedText  = string.Empty;
            var          normalizeHelper = new NormalizeSourceTextHelper();

            try
            {
                sourceText = normalizeHelper.NormalizeText(sourceText);

                using (var httpClient = new HttpClient())
                {
                    var values = new Dictionary <string, string>
                    {
                        { "text", sourceText },
                        { "source_lang", sourceLanguage },
                        { "target_lang", targetLanguage },
                        { "preserve_formatting", "1" },
                        { "tag_handling", "xml" },
                        { "auth_key", ApiKey },
                    };
                    httpClient.DefaultRequestHeaders.Add("Connection", "Keep-Alive");
                    var content          = new FormUrlEncodedContent(values);
                    var response         = httpClient.PostAsync("https://api.deepl.com/v1/translate", content).Result.Content.ReadAsStringAsync().Result;
                    var translatedObject = JsonConvert.DeserializeObject <TranslationResponse>(response);

                    if (translatedObject != null)
                    {
                        translatedText = translatedObject.Translations[0].Text;
                        translatedText = DecodeWhenNeeded(translatedText);
                        translatedText = HttpUtility.HtmlDecode(translatedText);
                    }
                }
            }
            catch (WebException e)
            {
                var eReason = Helpers.ProcessWebException(e);
                throw new Exception(eReason);
            }

            return(translatedText);
        }
        public string Translate(LanguagePair languageDirection, string sourceText)
        {
            var targetLanguage  = languageDirection.TargetCulture.TwoLetterISOLanguageName;
            var sourceLanguage  = languageDirection.SourceCulture.TwoLetterISOLanguageName;
            var translatedText  = string.Empty;
            var normalizeHelper = new NormalizeSourceTextHelper();

            try
            {
                sourceText = normalizeHelper.NormalizeText(sourceText);

                using (var httpClient = new HttpClient())
                {
                    var content = new StringContent($"text={sourceText}" +
                                                    $"&source_lang={sourceLanguage}" +
                                                    $"&target_lang={targetLanguage}" +
                                                    "&preserve_formatting=1" +
                                                    $"&tag_handling=xml&auth_key={ApiKey}", Encoding.UTF8, "application/x-www-form-urlencoded");

                    var studioVersion = new Toolkit.Core.Studio().GetStudioVersion().ExecutableVersion;
                    httpClient.DefaultRequestHeaders.Add("Trace-ID", $"SDL Trados Studio 2019 {studioVersion}/plugin 4.8.7");
                    var response         = httpClient.PostAsync("https://api.deepl.com/v1/translate", content).Result.Content.ReadAsStringAsync().Result;
                    var translatedObject = JsonConvert.DeserializeObject <TranslationResponse>(response);

                    if (translatedObject != null)
                    {
                        translatedText = translatedObject.Translations[0].Text;
                        translatedText = DecodeWhenNeeded(translatedText);
                    }
                }
            }
            catch (WebException e)
            {
                var eReason = Helpers.ProcessWebException(e);
                throw new Exception(eReason);
            }

            return(translatedText);
        }