Exemple #1
0
        public override void ImportPo(string inputFile, bool save = true, bool parallel = true)
        {
            var dataStream = DataStreamFactory.FromFile(inputFile, FileOpenMode.Read);
            var binary     = new BinaryFormat(dataStream);
            var binary2Po  = new Yarhl.Media.Text.Binary2Po();
            var po         = binary2Po.Convert(binary);

            _text = GetText();
            var tmp = _text.Text.Replace(LineEnding.ShownLineEnding, LineEnding.PoLineEnding);

            if (string.IsNullOrEmpty(tmp))
            {
                tmp = "<!empty>";
            }
            var entry = po.FindEntry(tmp);

            if (!string.IsNullOrEmpty(entry.Translated))
            {
                _text.Translation = entry.Translated.Replace(LineEnding.PoLineEnding, LineEnding.ShownLineEnding);
            }

            if (save)
            {
                SaveChanges();
            }
        }
Exemple #2
0
        public override void ImportPo(string inputFile, bool save = true, bool parallel = true)
        {
            using (DataStream dataStream = DataStreamFactory.FromFile(inputFile, FileOpenMode.Read))
            {
                var binary    = new BinaryFormat(dataStream);
                var binary2Po = new Yarhl.Media.Text.Binary2Po();
                Po  po        = binary2Po.Convert(binary);

                LoadBeforeImport();
                var dictionary = new ConcurrentDictionary <string, Subtitle>();
                foreach (Subtitle subtitle in _subtitles)
                {
                    dictionary[GetContext(subtitle)] = subtitle;
                }

                void UpdateSubtitleFromPoEntry(PoEntry entry)
                {
                    string context = entry.Context;

                    if (!dictionary.TryGetValue(context, out Subtitle subtitle))
                    {
                        return;
                    }

                    if (entry.Original == "<!empty>" || string.IsNullOrEmpty(subtitle.Text))
                    {
                        subtitle.Translation = subtitle.Text;
                    }
                    else
                    {
                        if (entry.Original.Replace(LineEnding.PoLineEnding, LineEnding.ShownLineEnding) !=
                            subtitle.Text)
                        {
                            // El texto original de la entrada no coincide con el del subtítulo, así que no podemos aplicar la traducción
                            subtitle.Translation = subtitle.Text;
                        }
                        else
                        {
                            string translation = entry.Translated;
                            subtitle.Translation = string.IsNullOrEmpty(translation)
                                ? subtitle.Text
                                : translation.Replace(LineEnding.PoLineEnding, LineEnding.ShownLineEnding);
                        }
                    }
                }

                if (parallel)
                {
                    Parallel.ForEach(po.Entries, UpdateSubtitleFromPoEntry);
                }
                else
                {
                    foreach (PoEntry entry in po.Entries)
                    {
                        UpdateSubtitleFromPoEntry(entry);
                    }
                }
            }

            if (save && NeedSaving)
            {
                SaveChanges();
            }
        }