private void SetTranslatedText(object ui, string translatedText, TranslationKeys key, TranslationInfo info)
        {
            var untemplatedTranslatedText = key.Untemplate(translatedText);

            info?.SetTranslatedText(untemplatedTranslatedText);

            if (_isInTranslatedMode)
            {
                SetText(ui, untemplatedTranslatedText, true, info);
            }
        }
        /// <summary>
        /// Translates the string of a UI  text or queues it up to be translated
        /// by the HTTP translation service.
        /// </summary>
        private string TranslateOrQueueWebJobImmediate(object ui, string text, TranslationInfo info, bool supportsStabilization)
        {
            // Get the trimmed text
            text = (text ?? ui.GetText()).Trim();

            // Ensure that we actually want to translate this text and its owning UI element.
            if (!string.IsNullOrEmpty(text) && IsTranslatable(text) && ShouldTranslate(ui) && !IsCurrentlySetting(info))
            {
                info?.Reset(text);

                var textKey = new TranslationKeys(text, !supportsStabilization);

                // if we already have translation loaded in our _translatios dictionary, simply load it and set text
                string translation;
                if (TryGetTranslation(textKey, out translation))
                {
                    QueueNewUntranslatedForClipboard(textKey);

                    if (!string.IsNullOrEmpty(translation))
                    {
                        SetTranslatedText(ui, translation, textKey, info);
                        return(translation);
                    }
                }
                else
                {
                    if (supportsStabilization)
                    {
                        // if we dont know what text to translate it to, we need to figure it out.
                        // this might take a while, so add the UI text component to the ongoing operations
                        // list, so we dont start multiple operations for it, as its text might be constantly
                        // changing.
                        _ongoingOperations.Add(ui);

                        // start a coroutine, that will execute once the string of the UI text has stopped
                        // changing. For all texts except 'story' texts, this will add a delay for exactly
                        // 0.5s to the translation. This is barely noticable.
                        //
                        // on the other hand, for 'story' texts, this will take the time that it takes
                        // for the text to stop 'scrolling' in.
                        try
                        {
                            StartCoroutine(
                                WaitForTextStablization(
                                    ui: ui,
                                    delay: 1.0f,  // 1 second to prevent '1 second tickers' from getting translated
                                    maxTries: 60, // 50 tries, about 1 minute
                                    currentTries: 0,
                                    onMaxTriesExceeded: () =>
                            {
                                _ongoingOperations.Remove(ui);
                            },
                                    onTextStabilized: stabilizedText =>
                            {
                                _ongoingOperations.Remove(ui);

                                if (!string.IsNullOrEmpty(stabilizedText) && IsTranslatable(stabilizedText))
                                {
                                    var stabilizedTextKey = new TranslationKeys(stabilizedText, false);

                                    QueueNewUntranslatedForClipboard(stabilizedTextKey);

                                    info?.Reset(stabilizedText);

                                    // once the text has stabilized, attempt to look it up
                                    if (TryGetTranslation(stabilizedTextKey, out translation))
                                    {
                                        if (!string.IsNullOrEmpty(translation))
                                        {
                                            SetTranslatedText(ui, translation, stabilizedTextKey, info);
                                        }
                                    }
                                    else
                                    {
                                        // Lets try not to spam a service that might not be there...
                                        if (_endpoint != null)
                                        {
                                            if (_consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown)
                                            {
                                                var job = GetOrCreateTranslationJobFor(stabilizedTextKey);
                                                job.Components.Add(ui);
                                            }
                                        }
                                        else
                                        {
                                            QueueNewUntranslatedForDisk(stabilizedTextKey);
                                        }
                                    }
                                }
                            }));
                        }
                        catch (Exception)
                        {
                            _ongoingOperations.Remove(ui);
                        }
                    }
                    else
                    {
                        if (!_startedOperationsForNonStabilizableComponents.Contains(textKey.GetDictionaryLookupKey()))
                        {
                            _startedOperationsForNonStabilizableComponents.Add(textKey.GetDictionaryLookupKey());

                            QueueNewUntranslatedForClipboard(textKey);

                            // Lets try not to spam a service that might not be there...
                            if (_endpoint != null)
                            {
                                if (_consecutiveErrors < Settings.MaxErrors && !Settings.IsShutdown)
                                {
                                    GetOrCreateTranslationJobFor(textKey);
                                }
                            }
                            else
                            {
                                QueueNewUntranslatedForDisk(textKey);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Translates the string of a UI  text or queues it up to be translated
        /// by the HTTP translation service.
        /// </summary>
        private string TranslateOrQueueWebJobImmediate(object ui, string text, TranslationInfo info)
        {
            // Get the trimmed text
            text = (text ?? GetText(ui)).Trim();
            info?.Reset(text);

            // Ensure that we actually want to translate this text and its owning UI element.
            if (!string.IsNullOrEmpty(text) && IsTranslatable(text) && ShouldTranslate(ui) && !IsAlreadyTranslating(info))
            {
                // if we already have translation loaded in our _translatios dictionary, simply load it and set text
                string translation;
                if (TryGetTranslation(text, out translation))
                {
                    if (!string.IsNullOrEmpty(translation))
                    {
                        SetTranslatedText(ui, translation, info);
                        return(translation);
                    }
                }
                else
                {
                    if (SupportsStabilization(ui))
                    {
                        // if we dont know what text to translate it to, we need to figure it out.
                        // this might take a while, so add the UI text component to the ongoing operations
                        // list, so we dont start multiple operations for it, as its text might be constantly
                        // changing.
                        _ongoingOperations.Add(ui);

                        // start a coroutine, that will execute once the string of the UI text has stopped
                        // changing. For all texts except 'story' texts, this will add a delay for exactly
                        // 0.5s to the translation. This is barely noticable.
                        //
                        // on the other hand, for 'story' texts, this will take the time that it takes
                        // for the text to stop 'scrolling' in.
                        try
                        {
                            StartCoroutine(
                                WaitForTextStablization(
                                    ui: ui,
                                    delay: 0.5f,
                                    maxTries: 100, // 100 tries == 50 seconds
                                    currentTries: 0,
                                    onMaxTriesExceeded: () =>
                            {
                                _ongoingOperations.Remove(ui);
                            },
                                    onTextStabilized: stabilizedText =>
                            {
                                _ongoingOperations.Remove(ui);

                                if (!string.IsNullOrEmpty(stabilizedText))
                                {
                                    info?.Reset(stabilizedText);

                                    // once the text has stabilized, attempt to look it up
                                    if (TryGetTranslation(stabilizedText, out translation))
                                    {
                                        if (!string.IsNullOrEmpty(translation))
                                        {
                                            SetTranslatedText(ui, translation, info);
                                        }
                                    }

                                    if (translation == null)
                                    {
                                        // Lets try not to spam a service that might not be there...
                                        if (AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors)
                                        {
                                            var job = new TranslationJob {
                                                UI = ui, UntranslatedText = stabilizedText
                                            };
                                            _unstartedJobs.Add(job);
                                        }
                                        else
                                        {
                                            _newUntranslated.Add(stabilizedText);
                                        }
                                    }
                                }
                            }));
                        }
                        catch (Exception)
                        {
                            _ongoingOperations.Remove(ui);
                        }
                    }
                    else
                    {
                        if (!_startedOperationsForNonStabilizableComponents.Contains(text))
                        {
                            _startedOperationsForNonStabilizableComponents.Add(text);

                            // Lets try not to spam a service that might not be there...
                            if (AutoTranslateClient.IsConfigured && _consecutiveErrors < Settings.MaxErrors)
                            {
                                var job = new TranslationJob {
                                    UntranslatedText = text
                                };
                                _unstartedJobs.Add(job);
                            }
                            else
                            {
                                _newUntranslated.Add(text);
                            }
                        }
                    }
                }
            }

            return(null);
        }