private void QueueNewTranslationForDisk(TranslationKeys key, string value)
 {
     lock ( _writeToFileSync )
     {
         _newTranslations[key.GetDictionaryLookupKey()] = value;
     }
 }
Esempio n. 2
0
 private void QueueNewTranslationForDisk(TranslationKeys key, string value)
 {
     lock ( _writeToFileSync )
     {
         _newTranslations[key.RelevantKey] = value;
     }
 }
Esempio n. 3
0
        private void AddTranslation(TranslationKeys key, string value)
        {
            _translations[key.OriginalKey] = value;
            _translatedTexts.Add(value);

            if (Settings.IgnoreWhitespaceInDialogue && key.IsDialogue)
            {
                _translations[key.DialogueKey] = value;
            }
        }
        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);
            }
        }
        private void QueueNewUntranslatedForClipboard(TranslationKeys key)
        {
            if (Settings.CopyToClipboard)
            {
                if (!_textsToCopyToClipboard.Contains(key.RelevantKey))
                {
                    _textsToCopyToClipboard.Add(key.RelevantKey);
                    _textsToCopyToClipboardOrdered.Add(key.RelevantKey);

                    _clipboardUpdated = Time.realtimeSinceStartup;
                }
            }
        }
        private void ReloadTranslations()
        {
            LoadTranslations();

            foreach (var kvp in ObjectExtensions.GetAllRegisteredObjects())
            {
                var info = kvp.Value as TranslationInfo;
                if (info != null && !string.IsNullOrEmpty(info.OriginalText))
                {
                    var key = new TranslationKeys(info.OriginalText, false);
                    if (TryGetTranslation(key, out string translatedText) && !string.IsNullOrEmpty(translatedText))
                    {
                        SetTranslatedText(kvp.Key, translatedText, key, info);
                    }
                }
            }
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        /// <summary>
        /// Loads the translations found in Translation.{lang}.txt
        /// </summary>
        private void LoadTranslations()
        {
            try
            {
                lock ( _writeToFileSync )
                {
                    Directory.CreateDirectory(Path.Combine(Config.Current.DataPath, Settings.TranslationDirectory));
                    Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(Config.Current.DataPath, Settings.OutputFile)));

                    foreach (var fullFileName in GetTranslationFiles())
                    {
                        if (File.Exists(fullFileName))
                        {
                            string[] translations = File.ReadAllLines(fullFileName, Encoding.UTF8);
                            foreach (string translation in translations)
                            {
                                string[] kvp = translation.Split(new char[] { '=', '\t' }, StringSplitOptions.None);
                                if (kvp.Length >= 2)
                                {
                                    string key   = TextHelper.Decode(kvp[0].Trim());
                                    string value = TextHelper.Decode(kvp[1].Trim());

                                    if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                                    {
                                        var translationKey = new TranslationKeys(key);
                                        AddTranslation(translationKey, value);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("[XUnity.AutoTranslator][ERROR]: An error occurred while loading translations. " + Environment.NewLine + e);
            }
        }
        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);
        }
        /// <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);
        }
 private bool TryGetTranslation(TranslationKeys key, out string value)
 {
     return(_translations.TryGetValue(key.GetDictionaryLookupKey(), out value));
 }
 private void QueueNewUntranslatedForDisk(TranslationKeys key)
 {
     _newUntranslated.Add(key.GetDictionaryLookupKey());
 }
 private void AddTranslation(TranslationKeys key, string value)
 {
     _translations[key.GetDictionaryLookupKey()] = value;
     _translatedTexts.Add(value);
 }
Esempio n. 14
0
        public TranslationJob(TranslationKeys key)
        {
            Keys = key;

            Components = new List <object>();
        }
Esempio n. 15
0
 private bool TryGetTranslation(TranslationKeys key, out string value)
 {
     return(_translations.TryGetValue(key.OriginalKey, out value) || (Settings.IgnoreWhitespaceInDialogue && _translations.TryGetValue(key.DialogueKey, out value)));
 }
Esempio n. 16
0
 private void QueueNewUntranslatedForDisk(TranslationKeys key)
 {
     _newUntranslated.Add(key.RelevantKey);
 }