Ejemplo n.º 1
0
        private static IEnumerator TranslateFullNameCoroutine(string origName, ChaControl chaControl, NameScope scope, Text textComponent,
                                                              TranslationResultHandler handler)
        {
            // wait a frame to avoid slowing down XUA TranslatingCallback handling
            yield return(null);

            if (!TranslationHelper.NameNeedsTranslation(origName, scope))
            {
                yield break;
            }

            if (chaControl != null && chaControl.chaFile != null)
            {
                if (TranslationHelper.CardNameManager.CardNeedsTranslation(chaControl.chaFile) &&
                    chaControl.TryGetTranslationHelperController(out var translationController))
                {
                    translationController.TranslateCardNames();
                    yield return(translationController.WaitOnTranslations());
                }
            }
            else
            {
                yield return(null); // give more accurate translation a chance to finish

                if (textComponent == null)
                {
                    yield break;
                }
                if (textComponent.text != origName &&
                    !TranslationHelper.NameNeedsTranslation(textComponent.text, scope))
                {
                    Logger?.DebugLogDebug(
                        $"{nameof(ComponentTranslationHelpers)}.{nameof(TranslateFullNameCoroutine)}: translated elsewhere, aborting async translation");
                    yield break;
                }
            }

            var job = TranslationHelper.CardNameManager.TranslateCardName(origName, scope, false, handler);

            yield return(chaControl != null
                ? chaControl.StartMonitoredCoroutine(job)
                : TranslationHelper.Instance.StartCoroutine(job));
        }
Ejemplo n.º 2
0
        private IEnumerator TranslateName(string originalName, NameScope nameScope,
                                          TranslationResultHandler callback)
        {
            Logger.DebugLogDebug($"TranslateName(\"{originalName}\", {nameScope}, {callback})");
            yield return(TranslateNameLimiter.Start());

            void CallbackWrapper(ITranslationResult translationResult)
            {
                TranslateNameLimiter.EndImmediately();
                callback(translationResult);
            }

            var cardTranslationMode = TranslationHelper.Instance.CurrentCardLoadTranslationMode;
            var suffixedName        = originalName;

            var activeSuffix = new KeyValuePair <string, string>(string.Empty, string.Empty);

            if (cardTranslationMode == CardLoadTranslationMode.Disabled ||
                !GeBoAPI.Instance.AutoTranslationHelper.IsTranslatable(suffixedName))
            {
                Logger.DebugLogDebug($"TranslateName: nothing to do: {originalName}");
                CallbackWrapper(new SimpleTranslationResult(false, originalName, "Disabled or already translated"));
                yield break;
            }


            if (cardTranslationMode >= CardLoadTranslationMode.CacheOnly)
            {
                Logger.DebugLogDebug($"TranslateName: attempting translation (XUA cached): {originalName}");
                if (NameTranslator.TryTranslateName(originalName, nameScope, out var translatedName))
                {
                    translatedName = CleanTranslationResult(translatedName, string.Empty);
                    if (originalName != translatedName)
                    {
                        Logger.DebugLogDebug($"TranslateName: Translated card name (XUA cached): {originalName} -> {translatedName}");
                        CallbackWrapper(new SimpleTranslationResult(true, translatedName));
                        yield break;
                    }
                }

                yield return(null);

                if (TranslationHelper.TranslateNameWithSuffix.Value && nameScope.Sex != CharacterSex.Unspecified)
                {
                    activeSuffix = Suffixes[(int)nameScope.Sex];
                    suffixedName = string.Concat(originalName, activeSuffix.Key);
                    Logger.DebugLogDebug($"TranslateName: attempting translation (XUA cached/suffixed): {suffixedName}");
                    if (NameTranslator.TryTranslateName(suffixedName, nameScope, out var translatedName2))
                    {
                        translatedName2 = CleanTranslationResult(translatedName2, activeSuffix.Value);
                        if (suffixedName != translatedName2)
                        {
                            Logger.DebugLogDebug($"TranslateName: Translated card name (XUA cached/suffixed): {originalName} -> {translatedName2}");
                            CallbackWrapper(new SimpleTranslationResult(true, translatedName2));
                            yield break;
                        }
                    }
                    yield return(null);
                }
            }


            if (cardTranslationMode == CardLoadTranslationMode.FullyEnabled)
            {
                // suffixedName will be origName if TranslateNameWithSuffix is off, so just use it here
                Logger.DebugLogDebug($"TranslateName: attempting translation (XUA async): {suffixedName}");
                NameTranslator.TranslateNameAsync(suffixedName, nameScope, result =>
                {
                    if (result.Succeeded)
                    {
                        var translatedName = CleanTranslationResult(result.TranslatedText, activeSuffix.Value);
                        if (suffixedName != translatedName)
                        {
                            Logger.DebugLogDebug($"TranslateName: Translated card name (XUA async): {originalName} -> {translatedName}");
                            CallbackWrapper(new SimpleTranslationResult(true, translatedName));
                            return;
                        }
                    }

                    CallbackWrapper(result);
                });
            }
            else
            {
                CallbackWrapper(new SimpleTranslationResult(false, originalName, "Unable to translate name"));
            }
        }