Пример #1
0
        private TranslationJob GetOrCreateTranslationJobFor(TranslationKeys key)
        {
            if (_unstartedJobs.TryGetValue(key.ForcedRelevantKey, out TranslationJob job))
            {
                return(job);
            }

            foreach (var completedJob in _completedJobs)
            {
                if (completedJob.Keys.ForcedRelevantKey == key.ForcedRelevantKey)
                {
                    return(completedJob);
                }
            }

            job = new TranslationJob(key);
            _unstartedJobs.Add(key.ForcedRelevantKey, job);

            CheckThresholds();

            return(job);
        }
        private TranslationJob GetOrCreateTranslationJobFor(TranslationKeys key)
        {
            if (_unstartedJobs.TryGetValue(key.GetDictionaryLookupKey(), out TranslationJob job))
            {
                return(job);
            }

            foreach (var completedJob in _completedJobs)
            {
                if (completedJob.Keys.GetDictionaryLookupKey() == key.GetDictionaryLookupKey())
                {
                    return(completedJob);
                }
            }

            Logger.Current.Debug("Queued translation for: " + key.GetDictionaryLookupKey());

            job = new TranslationJob(key);
            _unstartedJobs.Add(key.GetDictionaryLookupKey(), job);

            CheckThresholds();

            return(job);
        }
 public void InvokeJobFailed(TranslationJob job)
 {
     JobFailed?.Invoke(job);
 }
 public void InvokeJobCompleted(TranslationJob job)
 {
     JobCompleted?.Invoke(job);
 }
Пример #5
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);
        }