示例#1
0
            protected override IEnumerator StartJobs()
            {
                var jobs = ListPool <Coroutine> .Get();

                var fullName = _chaFile.GetFullName();

                var handled = HashSetPool <int> .Get();

                if (TranslationHelper.Instance.NamePresetManager.TryTranslateCardNames(_chaFile, out var result))
                {
                    foreach (var entry in result)
                    {
                        FullNameHandled = FullNameHandled || entry.Key == "fullname";
                        var i = GeBoAPI.Instance.ChaFileNameToIndex(entry.Key);
                        if (i < 0 || string.IsNullOrEmpty(entry.Value) || _chaFile.GetName(i) == entry.Value)
                        {
                            continue;
                        }
                        _chaFile.SetTranslatedName(i, entry.Value);
                        handled.Add(i);
                    }
                }

                var sex = _chaFile.GetSex();

                foreach (var name in _chaFile.EnumerateNames())
                {
                    if (handled.Contains(name.Key))
                    {
                        continue;
                    }

                    FullNameHandled = FullNameHandled || name.Value == fullName;

                    var i       = name.Key;
                    var chaFile = _chaFile;

                    var nameScope = new NameScope(sex, _chaFile.GetNameType(i));

                    if (TryGetRecentTranslation(nameScope, name.Value, out var cachedName))
                    {
                        // if true, but name unchanged then name in NoTranslate
                        if (name.Value != cachedName)
                        {
                            chaFile.SetTranslatedName(i, cachedName);
                        }

                        continue;
                    }

                    if (!StringUtils.ContainsJapaneseChar(name.Value))
                    {
                        NoTranslate[nameScope].Add(name.Value);
                        continue;
                    }

                    JobStarted();


                    var job = StartCoroutine(Instance.TranslateCardName(name.Value,
                                                                        nameScope,
                                                                        Handlers.UpdateCardName(chaFile, i),
                                                                        Handlers.AddNameToAutoTranslationCache(name.Value, true),
                                                                        JobCompleteHandler));

                    jobs.Add(job);
                }
                HashSetPool <int> .Release(handled);

                foreach (var job in jobs)
                {
                    yield return(job);
                }
                ListPool <Coroutine> .Release(jobs);
            }
示例#2
0
 public IEnumerator TranslateCardName(string originalName, NameScope scope,
                                      params TranslationResultHandler[] callbacks)
 {
     return(TranslateCardName(originalName, scope, false, callbacks));
 }
示例#3
0
        private IEnumerator TranslateName(string originalName, NameScope nameScope,
                                          TranslationResultHandler callback)
        {
            Logger.DebugLogDebug($"TranslateName(\"{originalName}\", {nameScope}, {callback})");
            yield return(TranslationHelper.Instance.StartCoroutine(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 TranslationResult(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 (!TranslationHelper.NameStringComparer.Equals(originalName, translatedName))
                    {
                        Logger.DebugLogDebug(
                            $"TranslateName: Translated card name (XUA cached): {originalName} -> {translatedName}");
                        CallbackWrapper(new TranslationResult(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 translatedName))
                    {
                        translatedName = CleanTranslationResult(translatedName, activeSuffix.Value);
                        if (!TranslationHelper.NameStringComparer.Equals(suffixedName, translatedName))
                        {
                            Logger.DebugLogDebug(
                                $"TranslateName: Translated card name (XUA cached/suffixed): {originalName} -> {translatedName}");
                            CallbackWrapper(new TranslationResult(true, translatedName));
                            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 (!TranslationHelper.NameStringComparer.Equals(suffixedName, translatedName))
                        {
                            Logger.DebugLogDebug(
                                $"TranslateName: Translated card name (XUA async): {originalName} -> {translatedName}");
                            CallbackWrapper(new TranslationResult(true, translatedName));
                            return;
                        }
                    }

                    CallbackWrapper(result);
                });
            }
            else
            {
                CallbackWrapper(new TranslationResult(false, originalName, "Unable to translate name"));
            }
        }
示例#4
0
 internal static bool NameNeedsTranslation(string name, NameScope scope)
 {
     return(!name.IsNullOrWhiteSpace() &&
            !NoTranslate[scope].Contains(name) &&
            StringUtils.ContainsJapaneseChar(name));
 }