Пример #1
0
        // In this simple test we check if the number
        // of the segments in the database is incremented
        // when we add try to add a line

        public void TestAddLine()
        {
            //Arrange
            var segment = new SegmentViewModel
            {
                Line1          = "Dummy",
                Line2          = "Dummy",
                Original1      = "Dummy",
                Original2      = "Dummy",
                TranslationID  = 1,
                TimestampEnd   = "00:06:30,000",
                TimestampStart = "00:06:10,000",
            };
            var mockUnitOfWork = new MockUnitOfWork();

            for (int i = 0; i < 3; i++)
            {
                var reposegment = new TranslationSegment();
                mockUnitOfWork.TranslationSegmentRepository.Insert(reposegment);
            }
            ;


            var controller  = new TranslationController(mockUnitOfWork);
            int countbefore = mockUnitOfWork.TranslationSegmentRepository.Get().ToList().Count;
            // Act
            var result = controller.AddLine(segment);

            // Assert
            Assert.IsTrue(mockUnitOfWork.TranslationSegmentRepository.Get().ToList().Count == countbefore + 1);
        }
Пример #2
0
        public ActionResult AddLine(SegmentViewModel segment)
        {
            if (ModelState.IsValid)
            {
                string startTime = segment.TimestampStart;
                int    insertPos = 1;
                var    segments  = _unitOfWork.TranslationSegmentRepository.Get()
                                   .Where(ts => ts.TranslationID == segment.TranslationID)
                                   .OrderBy(ts => ts.SegmentID)
                                   .ToList();

                // Find the insert position
                for (int i = 0; i < segments.Count; i++)
                {
                    string tmp = segments.ElementAt(i).Timestamp.Substring(0, 12);
                    if (string.Compare(tmp, startTime) > 0)
                    {
                        insertPos = segments.ElementAt(i).SegmentID;
                        break;
                    }
                    insertPos = segments.ElementAt(i).SegmentID + 1;
                }

                // increment segmentId on all segments with segmentId greater than the new segment
                for (int i = insertPos; i <= segments.Count; i++)
                {
                    segments.ElementAt(i - 1).SegmentID++;
                }

                // Insert the new segment
                var translationSegment = new TranslationSegment
                {
                    Line1         = segment.Line1,
                    Line2         = segment.Line2,
                    Original1     = segment.Original1,
                    Original2     = segment.Original2,
                    TranslationID = segment.TranslationID,
                    Timestamp     = segment.TimestampStart + " --> " + segment.TimestampEnd,
                    SegmentID     = insertPos
                };
                _unitOfWork.TranslationSegmentRepository.Insert(translationSegment);
                _unitOfWork.Save();
                return(RedirectToAction("Index", new { id = segment.TranslationID }));
            }
            return(View(segment));
        }
Пример #3
0
        // here we make a dummy translation with 53
        // segments max 50 segments are displayed at
        // a time on the site( 50 on every page)
        // the second page should include 3 segments
        public void TestTranslationIndex()
        {
            //Arrange

            var translation = new Translation
            {
                ID         = 1,
                MediaID    = 2,
                LanguageID = 3,
                Media      = new Media {
                    Title = "Dummy"
                },
                Language = new Language {
                    Name = "Dummy2"
                },
                TranslationSegments = new List <TranslationSegment>()
            };

            for (int i = 1; i < 54; i++)
            {
                var segment = new TranslationSegment
                {
                    SegmentID   = i,
                    Translation = translation
                };
                translation.TranslationSegments.Add(segment);
            }
            var mockUnitOfWork = new MockUnitOfWork();

            mockUnitOfWork.TranslationRepository.Insert(translation);
            var controller = new TranslationController(mockUnitOfWork);

            //Act
            var result = controller.Index(1, 2);

            // Assert
            var viewResult = (ViewResult)result;
            PagedList <TranslationSegment> model = viewResult.Model as PagedList <TranslationSegment>;

            Assert.IsTrue(model.Count == 3);
        }
Пример #4
0
        /// <summary>
        /// Parses .srt files into Translations.
        /// Srt files are of the form:
        ///
        /// ID
        /// TIMESTAMP
        /// TEXTLINE1
        /// TEXTLINE2
        /// </summary>
        /// <param name="path"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static Translation Parse(string path, string format = "srt")
        {
            var translation = new Translation {
                TranslationSegments = new List <TranslationSegment>()
            };

            using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
            {
                string nextLine;
                string tmp = "";
                while ((nextLine = sr.ReadLine()) != null)
                {
                    // Consume extra white spaces between segments in the srt file.
                    while (nextLine == "")
                    {
                        nextLine = sr.ReadLine();
                    }
                    TranslationSegment transSeg = new TranslationSegment();

                    // If the segment id in the srt file is not an integer we throw an error.
                    int segmentId = 0;
                    if (!int.TryParse(nextLine, out segmentId))
                    {
                        throw new SubtitleParseException("Villa við lestur .srt skráar, auðkenni þýðingarbúts var \"" + nextLine + "\" en þarf að vera heiltala");
                    }

                    // Get rid of extra translation lines found in .srt files from subtitle sites
                    // often containing invalid timestamps and other invalid information.
                    // These  lines usually have an id of 9999 or 0.
                    // Known bug: if The translation contains more than 9999 translation segments
                    // the rest will be ignored after 9999.
                    if (int.Parse(nextLine) == 9999 || int.Parse(nextLine) == 0)
                    {
                        return(translation);
                    }
                    transSeg.SegmentID = segmentId;

                    // Checking if the timestamp matches the format that is required
                    // 00:00:00,000 --> 00:00:00,000
                    nextLine = sr.ReadLine();
                    Regex rgx = new Regex(@"^\d\d:[0-5]\d:[0-5]\d,\d\d\d\s-->\s\d\d:[0-5]\d:[0-5]\d,\d\d\d$");
                    if (!rgx.IsMatch(nextLine))
                    {
                        throw new SubtitleParseException("Villa við lestur .srt skráar, tímastimpill númer " + segmentId + " var ekki á réttu formi");
                    }

                    // Read the actual text into Line1 and Line2, if the segment contains more than 2 lines, line 2-n are
                    // concatenated into one line and stored in Line2,
                    transSeg.Timestamp = nextLine;
                    if ((nextLine = sr.ReadLine()) != "" && nextLine != null)
                    {
                        transSeg.Line1     = nextLine;
                        transSeg.Original1 = nextLine;
                    }
                    while ((nextLine = sr.ReadLine()) != "" && nextLine != null)
                    {
                        tmp += nextLine;
                    }
                    if (tmp != "")
                    {
                        transSeg.Line2     = tmp;
                        transSeg.Original2 = tmp;
                    }
                    translation.TranslationSegments.Add(transSeg);
                    tmp = "";
                }
            }

            return(translation);
        }