public IEnumerator Translate(UntranslatedTextInfo[] untranslatedTextInfos, string from, string to, Action <string[]> success, Action <string, Exception> failure)
        {
            var startTime = Time.realtimeSinceStartup;
            var context   = new TranslationContext(untranslatedTextInfos, from, to, success, failure);

            _ongoingTranslations++;

            //if( untranslatedTextInfos.Length > 0 && untranslatedTextInfos.Length <= 2 )
            //{
            //   var maxLength = untranslatedTextInfos.Max( x => x.UntranslatedText.Length );
            //   var untranslatedTextInfo = untranslatedTextInfos.First( x => x.UntranslatedText.Length == maxLength );
            //   if( untranslatedTextInfo.ContextBefore.Count == 0 )
            //   {
            //      foreach( var priorTranslation in _priorTranslations )
            //      {
            //         if( startTime - priorTranslation.Time < MaxPriorTranslationAge )
            //         {
            //            untranslatedTextInfo.ContextBefore.Add( priorTranslation.UntranslatedText );
            //         }
            //      }
            //      var parts = NewlineSplitter.Split( untranslatedTextInfo.UntranslatedText );
            //      foreach( var part in parts )
            //      {
            //         var isGarbage = NewlineSplitter.IsMatch( part );
            //         if( !isGarbage )
            //         {
            //            _priorTranslations.Add( new PriorTranslation { Time = startTime, UntranslatedText = part } ); ;
            //         }
            //      }
            //      while( _priorTranslations.Count > MaxPriorTranslations )
            //      {
            //         _priorTranslations.RemoveAt( 0 );
            //      }
            //   }
            //}

            try
            {
                if (Settings.SimulateDelayedError)
                {
                    yield return(CoroutineHelper.CreateWaitForSeconds(1f));

                    context.FailWithoutThrowing("Simulating delayed error. Press CTRL+ALT+NP8 to disable!", null);
                }
                else if (Settings.SimulateError)
                {
                    context.FailWithoutThrowing("Simulating error. Press CTRL+ALT+NP9 to disable!", null);
                }
                else
                {
                    bool ok       = false;
                    var  iterator = Endpoint.Translate(context);
                    if (iterator != null)
                    {
                        TryMe : try
                        {
                            ok = iterator.MoveNext();

                            // check for timeout
                            var now = Time.realtimeSinceStartup;
                            if (now - startTime > Settings.Timeout)
                            {
                                ok = false;
                                context.FailWithoutThrowing($"Timeout occurred during translation (took more than {Settings.Timeout} seconds)", null);
                            }
                        }
                        catch (TranslationContextException)
                        {
                            ok = false;
                        }
                        catch (Exception e)
                        {
                            ok = false;
                            context.FailWithoutThrowing("Error occurred during translation.", e);
                        }

                        if (ok)
                        {
                            yield return(iterator.Current);

                            goto TryMe;
                        }
                    }
                }
            }
            finally
            {
                _ongoingTranslations--;

                context.FailIfNotCompleted();
            }
        }
Exemple #2
0
        public IEnumerator Translate(string[] untranslatedTexts, string from, string to, Action <string[]> success, Action <string, Exception> failure)
        {
            var startTime = Time.realtimeSinceStartup;
            var context   = new TranslationContext(untranslatedTexts, from, to, success, failure);

            _ongoingTranslations++;

            try
            {
                if (Settings.SimulateDelayedError)
                {
                    yield return(new WaitForSeconds(1));

                    context.FailWithoutThrowing("Simulating delayed error. Press CTRL+ALT+NP8 to disable!", null);
                }
                else if (Settings.SimulateError)
                {
                    context.FailWithoutThrowing("Simulating error. Press CTRL+ALT+NP9 to disable!", null);
                }
                else
                {
                    bool ok       = false;
                    var  iterator = Endpoint.Translate(context);
                    if (iterator != null)
                    {
                        TryMe : try
                        {
                            ok = iterator.MoveNext();

                            // check for timeout
                            var now = Time.realtimeSinceStartup;
                            if (now - startTime > Settings.Timeout)
                            {
                                ok = false;
                                context.FailWithoutThrowing($"Timeout occurred during translation (took more than {Settings.Timeout} seconds)", null);
                            }
                        }
                        catch (TranslationContextException)
                        {
                            ok = false;
                        }
                        catch (Exception e)
                        {
                            ok = false;
                            context.FailWithoutThrowing("Error occurred during translation.", e);
                        }

                        if (ok)
                        {
                            yield return(iterator.Current);

                            goto TryMe;
                        }
                    }
                }
            }
            finally
            {
                _ongoingTranslations--;

                context.FailIfNotCompleted();
            }
        }