private void HandleTranslationError(TranslationError message)
 {
     lock ( _sync )
     {
         if (_transactionHandles.TryGetValue(message.Id, out var result))
         {
             result.SetCompleted(null, message.Reason, message.FailureCode);
             _transactionHandles.Remove(message.Id);
         }
     }
 }
Beispiel #2
0
    private async Task TranslateCurrentDocumentAsync()
    {
        IDocument?doc = Controller.CurrentDocument;

        if (doc is null || !await SaveCurrentDocumentAsync(false))
        {
            return;
        }

        string?fileName = doc.SourceDocumentPath;

        if (GetXmlOutFileName(ref fileName))
        {
            (IList <DataError> Errors, IList <KeyValuePair <long, string> > UnusedTranslations)result;
            var task = Task.Run(() => doc.Translate(fileName));
            _tasks.Add(task);
            try
            {
                result = await task.ConfigureAwait(true);
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message, MessageBoxImage.Error);
                return;
            }

            if (result.Errors.Count != 0)
            {
                TranslationError?.Invoke(this, new DataErrorEventArgs(result.Errors));
            }

            if (result.UnusedTranslations.Count != 0)
            {
                var wnd = new SelectUnusedTranslationsWindow(System.IO.Path.GetFileName(Controller.CurrentDocument !.FileName), result.UnusedTranslations);

                if (true == wnd.ShowDialog(this))
                {
                    foreach (UnusedTranslationUserControl cntr in wnd.Controls)
                    {
                        if (cntr.Remove)
                        {
                            doc.RemoveTranslation(cntr.Kvp.Key);
                        }
                    }
                }
            }
        }
    }
        public async Task RunAsync()
        {
            using (var stdout = Console.OpenStandardOutput())
                using (var writer = new StreamWriter(stdout))
                    using (var stdin = Console.OpenStandardInput())
                        using (var reader = new StreamReader(stdin))
                        {
                            writer.AutoFlush = true;

                            while (true)
                            {
                                var receivedPayload = reader.ReadLine();
                                if (string.IsNullOrEmpty(receivedPayload))
                                {
                                    return;
                                }

                                var message = ExtProtocolConvert.Decode(receivedPayload) as TranslationRequest;
                                if (message == null)
                                {
                                    return;
                                }

                                var context = new TranslationContext(message.UntranslatedTexts, message.SourceLanguage, message.DestinationLanguage);
                                try
                                {
                                    await Endpoint.Translate(context);
                                }
                                catch (Exception e)
                                {
                                    context.FailWithoutThrowing("An error occurred in the pipeline.", e);
                                }


                                ProtocolMessage response;
                                if (!string.IsNullOrEmpty(context.ErrorMessage) || context.Error != null)
                                {
                                    string errorMessage = context.ErrorMessage;
                                    if (context.Error != null)
                                    {
                                        if (!string.IsNullOrEmpty(errorMessage))
                                        {
                                            errorMessage += Environment.NewLine;
                                        }
                                        errorMessage += context.Error.ToString();
                                    }

                                    response = new TranslationError
                                    {
                                        Id     = message.Id,
                                        Reason = errorMessage
                                    };
                                }
                                else
                                {
                                    response = new TranslationResponse
                                    {
                                        Id = message.Id,
                                        TranslatedTexts = context.TranslatedTexts
                                    };
                                }

                                var translatedPayload = ExtProtocolConvert.Encode(response);
                                writer.WriteLine(translatedPayload);
                            }
                        }
        }
 internal TranslationErrorResponse(TranslationError error)
 {
     Error = error;
 }