Ejemplo n.º 1
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();
            }
        }