예제 #1
0
        private void FindScript(ResScript res)
        {
            var script = res.GetScript();

            if (script == null)
            {
                return;
            }

            var sections = script.Get <StringSection>();

            if (sections == null)
            {
                return;
            }

            List <StringSection> trSections = null;

            if (_translate != null)
            {
                var tr = _translate.Get(res).GetScript();
                trSections = tr.Get <StringSection>();
            }

            for (int i = 0; i < sections.Count; i++)
            {
                var sec = sections[i];
                for (int ind = 0; ind < sec.Strings.Count; ind++)
                {
                    StringConst str = sec.Strings[ind];
                    if (IsPass(str.Value))
                    {
                        AddResult(res, ind, str.Value);
                    }
                    else if (trSections != null && IsPass(trSections[i].Strings[ind].Value))
                    {
                        AddResult(res, ind, trSections[i].Strings[ind].Value);
                    }
                }
            }
        }
예제 #2
0
        private async Task ApplyTranslate(ResScript r)
        {
            try
            {
                var vol = Book.GetVolume(r.ToString());
                if (vol == null) // Ресурс без перевода
                {
                    return;
                }

                if (String.IsNullOrEmpty(vol.DateChange)) // Пропускаем части без перевода
                {
                    return;
                }

                if (_cache != null && _cache.TryGetValue(r.ToString(), out string changed) && changed.Equals(vol.DateChange)) // Пропускаем неизмененные части
                {
                    return;
                }

                var enScr = r.GetScript(false);
                var ruScr = r.GetScript(true);

                var enStrings = enScr.AllStrings.Where(s => !s.IsClassName).ToArray();
                var ruStrings = ruScr.AllStrings.Where(s => !s.IsClassName).ToArray();

                var translates = await Book.GetTranslates(vol.URL);

                bool hasTranslate = false;

                var notaEn = translates.Keys.ToHashSet();

                for (int i = 0; i < enStrings.Length; i++)
                {
                    var en = enStrings[i].Value;
                    if (String.IsNullOrEmpty(en))
                    {
                        continue;
                    }

                    if (!translates.TryGetValue(en, out var tr))
                    {
                        en = en.Replace("\n", "\r\n");
                        if (!translates.TryGetValue(en, out tr))
                        {
                            //Console.WriteLine($"Missing tex {r} - {en}");
                            continue;
                        }
                    }

                    notaEn.Remove(en);

                    var ru = ruStrings[i].Value;
                    if (tr.Equals(ru)) // Пропускаем старый перевод
                    {
                        continue;
                    }

                    hasTranslate = true;

                    ruStrings[i].Value = tr;
                }

                if (notaEn.Count > 0)
                {
                    Console.WriteLine($"Not found nota in {r.FileName}");
                    foreach (var nota in notaEn)
                    {
                        Console.WriteLine(nota);
                    }
                }

                if (hasTranslate)
                {
                    Console.WriteLine(r);
                    r.SaveTranslate(ruScr.GetBytes());
                    _hasChanges = true;
                }
            }
            finally
            {
                _progress++;
                ReportProgress?.Invoke(_progress * 100 / _total);
            }
        }