예제 #1
0
        public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure)
            {
                base.ProcessParagraphUnit(paragraphUnit);
                return;
            }

            foreach (var segmentPair in paragraphUnit.SegmentPairs)
            {
                SegmentPairInfo segmentPairInfo = null;
                try
                {
                    segmentPairInfo = SegmentPairProcessor.GetSegmentPairInfo(segmentPair);
                }
                catch
                {
                    // catch all; ignore
                }

                var status = segmentPair.Properties.ConfirmationLevel.ToString();
                var match  = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);

                var targetSegment = segmentPair.Target;

                //capture if segment contains tracked changes
                //var hasTrackedChanges = false;

                var segmentIdentifier = string.Empty;
                if (SegmentExists(paragraphUnit.Properties.ParagraphUnitId.Id, segmentPair.Properties.Id.Id, ref segmentIdentifier))
                {
                    var noOverwrite   = !_importOptions.OverwriteTranslations && segmentPair.Target.Any();
                    var excludeFilter = false;
                    if (_importOptions.ExcludeFilterIds != null)
                    {
                        excludeFilter = (segmentPair.Properties.IsLocked && _importOptions.ExcludeFilterIds.Exists(a => a == "Locked")) ||
                                        _importOptions.ExcludeFilterIds.Exists(a => a == status) ||
                                        _importOptions.ExcludeFilterIds.Exists(a => a == match);
                    }

                    if (noOverwrite || excludeFilter)
                    {
                        if (!string.IsNullOrEmpty(_importOptions.StatusTranslationNotUpdatedId))
                        {
                            var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusTranslationNotUpdatedId, true, out var result);
                            var statusTranslationNotUpdated = success ? result : ConfirmationLevel.Unspecified;

                            segmentPair.Target.Properties.ConfirmationLevel = statusTranslationNotUpdated;
                            segmentPair.Properties.ConfirmationLevel        = statusTranslationNotUpdated;

                            status = segmentPair.Properties.ConfirmationLevel.ToString();
                            match  = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);
                        }

                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Excluded, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Excluded, segmentPairInfo);
                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Total, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Total, segmentPairInfo);

                        continue;
                    }

                    var tokenVisitor = new TokenVisitor();
                    tokenVisitor.VisitSegment(targetSegment);
                    var originalText = tokenVisitor.PlainText.ToString();

                    try
                    {
                        if (segmentPair.Target.Properties.TranslationOrigin == null)
                        {
                            targetSegment.Properties.TranslationOrigin = _segmentBuilder.ItemFactory.CreateTranslationOrigin();
                        }
                        else
                        {
                            var currentTranslationOrigin = (ITranslationOrigin)targetSegment.Properties.TranslationOrigin.Clone();
                            targetSegment.Properties.TranslationOrigin.OriginBeforeAdaptation = currentTranslationOrigin;
                        }

                        SetTranslationOrigin(targetSegment);

                        var updatedSegment = _updatedSegments[segmentIdentifier];
                        if (updatedSegment.TranslationTokens.Count > 0)
                        {
                            targetSegment = _segmentBuilder.GetUpdatedSegment(targetSegment, updatedSegment.TranslationTokens, segmentPair.Source);
                        }

                        if (updatedSegment.BackTranslationTokens.Count > 0)
                        {
                            var backTranslations = JsonConvert.SerializeObject(updatedSegment.BackTranslationTokens);
                            segmentPair.Target.Properties.TranslationOrigin.SetMetaData("back-translation", backTranslations);
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Problem when merging content of segment " + segmentPair.Properties.Id.Id, ex);
                    }

                    tokenVisitor.Process(targetSegment);
                    var updatedText = tokenVisitor.PlainText.ToString();

                    if (string.Compare(originalText, updatedText, StringComparison.Ordinal) != 0)
                    {
                        if (!string.IsNullOrEmpty(_importOptions.StatusTranslationUpdatedId))
                        {
                            var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusTranslationUpdatedId, true,
                                                                            out var result);
                            var statusTranslationUpdated = success ? result : ConfirmationLevel.Unspecified;

                            targetSegment.Properties.ConfirmationLevel = statusTranslationUpdated;
                            segmentPair.Properties.ConfirmationLevel   = statusTranslationUpdated;

                            status = segmentPair.Properties.ConfirmationLevel.ToString();
                            match  = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);
                        }

                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Processed, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Processed, segmentPairInfo);
                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Total, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Total, segmentPairInfo);
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(_importOptions.StatusTranslationNotUpdatedId))
                        {
                            var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusTranslationNotUpdatedId, true, out var result);
                            var statusTranslationNotUpdated = success ? result : ConfirmationLevel.Unspecified;

                            targetSegment.Properties.ConfirmationLevel = statusTranslationNotUpdated;
                        }

                        status = targetSegment.Properties.ConfirmationLevel.ToString();
                        match  = Enumerators.GetTranslationOriginType(targetSegment.Properties.TranslationOrigin, _analysisBands);

                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Excluded, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Excluded, segmentPairInfo);
                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Total, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Total, segmentPairInfo);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(_importOptions.StatusSegmentNotImportedId))
                    {
                        var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusTranslationNotUpdatedId, true, out var result);
                        var statusTranslationNotUpdated = success ? result : ConfirmationLevel.Unspecified;

                        segmentPair.Target.Properties.ConfirmationLevel = statusTranslationNotUpdated;
                        segmentPair.Properties.ConfirmationLevel        = statusTranslationNotUpdated;

                        status = segmentPair.Properties.ConfirmationLevel.ToString();
                        match  = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);
                    }

                    AddWordCounts(status, ConfirmationStatistics.WordCounts.NotProcessed, segmentPairInfo);
                    AddWordCounts(match, TranslationOriginStatistics.WordCounts.NotProcessed, segmentPairInfo);
                    AddWordCounts(status, ConfirmationStatistics.WordCounts.Total, segmentPairInfo);
                    AddWordCounts(match, TranslationOriginStatistics.WordCounts.Total, segmentPairInfo);
                }
            }

            base.ProcessParagraphUnit(paragraphUnit);
        }