Example #1
0
        public string TranslateText(string text, string sourceLanguage, string targetLanguage)
        {
            try
            {
                var request = new RestRequest("/mt/translations/async", Method.POST)
                {
                    RequestFormat = DataFormat.Json
                };
                var traceId = GetTraceId(request);

                string[] texts = { text };
                request.AddBody(new
                {
                    input            = texts,
                    sourceLanguageId = sourceLanguage,
                    targetLanguageId = targetLanguage,
                    model            = _flavor,
                    inputFormat      = "xliff"
                });
                var response = _client.Execute(request);

                if (response.StatusCode == HttpStatusCode.Unauthorized && !string.IsNullOrEmpty(_authenticationMethod) &&
                    _authenticationMethod.Equals(Enums.GetDisplayName(Enums.LoginOptions.StudioAuthentication)))
                {
                    // Get refresh token
                    var token = _studioCredentials.EnsureValidConnection();
                    if (!string.IsNullOrEmpty(token))
                    {
                        UpdateRequestHeadersForRefreshToken(request, token);

                        var translationAsyncResponse = _client.Execute(request);

                        if (translationAsyncResponse.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            _messageBoxService.ShowMessage(Constants.UnauthorizedCredentials, Constants.PluginName);
                            Log.Logger.Error($"{Constants.UnauthorizedToken} {traceId}");
                        }
                        else if (!translationAsyncResponse.IsSuccessful && translationAsyncResponse.StatusCode != HttpStatusCode.Unauthorized)
                        {
                            ShowErrors(translationAsyncResponse);
                        }
                        return(ReturnTranslation(translationAsyncResponse));
                    }
                }
                else if (!response.IsSuccessful && response.StatusCode != HttpStatusCode.Unauthorized)
                {
                    ShowErrors(response);
                }
                return(ReturnTranslation(response));
            }
            catch (Exception e)
            {
                Log.Logger.Error($"{Constants.TranslateTextMethod} {e.Message}\n {e.StackTrace}");
            }
            return(string.Empty);
        }
        public string TranslateText(string text, string sourceLanguage, string targetLanguage)
        {
            try
            {
                var request = new RestRequest("/mt/translations/async", Method.POST)
                {
                    RequestFormat = DataFormat.Json
                };
                var traceId = GetTraceId(request);

                string[] texts = { text };
                request.AddBody(new
                {
                    input            = texts,
                    sourceLanguageId = sourceLanguage,
                    targetLanguageId = targetLanguage,
                    model            = _flavor,
                    inputFormat      = "xliff"
                });
                var response = _client.Execute(request);

                if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    // Get refresh token
                    var token = _studioCredentials.EnsureValidConnection();
                    if (!string.IsNullOrEmpty(token))
                    {
                        UpdateRequestHeadersForRefreshToken(request, token);

                        var translationAsyncResponse = _client.Execute(request);

                        if (translationAsyncResponse.StatusCode == HttpStatusCode.Unauthorized)
                        {
                            MessageBox.Show("Unauthorized: Please check your credentials", PluginName, MessageBoxButton.OK);

                            Log.Logger.Error($"Unauthorized: Translate text using refresh token \nTrace-Id: {traceId}");
                        }
                        else if (!translationAsyncResponse.IsSuccessful && translationAsyncResponse.StatusCode != HttpStatusCode.Unauthorized)
                        {
                            ShowErrors(translationAsyncResponse);
                        }
                        return(ReturnTranslation(translationAsyncResponse));
                    }
                }
                else if (!response.IsSuccessful && response.StatusCode != HttpStatusCode.Unauthorized)
                {
                    ShowErrors(response);
                }
                return(ReturnTranslation(response));
            }
            catch (Exception e)
            {
                Log.Logger.Error($"Translate text method: {e.Message}\n {e.StackTrace}");
            }
            return(string.Empty);
        }