public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure || !paragraphUnit.SegmentPairs.Any())
            {
                return;
            }

            foreach (var segmentPair in paragraphUnit.SegmentPairs)
            {
                var segmentPairInfo = new SegmentPairInfo
                {
                    FileId          = _fileProperties.FileConversionProperties.FileId.Id,
                    ParagraphUnit   = paragraphUnit,
                    SegmentPair     = segmentPair,
                    ParagraphUnitId = paragraphUnit.Properties.ParagraphUnitId.Id,
                    SegmentId       = segmentPair.Properties.Id.Id
                };

                try
                {
                    segmentPairInfo.SourceWordCounts = SegmentPairProcessor.GetSegmentPairInfo(segmentPair)?.SourceWordCounts;
                }
                catch
                {
                    // catch all; ignore
                }

                SegmentPairInfos.Add(segmentPairInfo);
            }
        }
        public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure)
            {
                UpdateParagraphUnit(paragraphUnit);
                return;
            }

            if (!paragraphUnit.SegmentPairs.Any())
            {
                return;
            }

            var segmentPairs = new List <ISegmentPair>();

            foreach (var segmentPair in paragraphUnit.SegmentPairs)
            {
                if (_subSegmentParagraphUnitIds.Contains(paragraphUnit.Properties.ParagraphUnitId.Id) ||
                    _selectedSegments.Exists(a =>
                                             a.ParagraphUnitId == paragraphUnit.Properties.ParagraphUnitId.Id &&
                                             a.SegmentId == segmentPair.Properties.Id.Id))
                {
                    segmentPairs.Add(segmentPair);

                    var subSegmentParagraphUnitIds = GetSubSegmentParagraphUnitIds(segmentPair);
                    foreach (var paragraphUnitId in subSegmentParagraphUnitIds.Where(paragraphUnitId =>
                                                                                     !_subSegmentParagraphUnitIds.Exists(a => a == paragraphUnitId)))
                    {
                        _subSegmentParagraphUnitIds.Add(paragraphUnitId);
                    }

                    var segmentPairInfo = new SegmentPairInfo
                    {
                        FileId          = _fileProperties.FileConversionProperties.FileId.Id,
                        ParagraphUnit   = paragraphUnit,
                        SegmentPair     = segmentPair,
                        ParagraphUnitId = paragraphUnit.Properties.ParagraphUnitId.Id,
                        SegmentId       = segmentPair.Properties.Id.Id
                    };

                    try
                    {
                        segmentPairInfo.SourceWordCounts = SegmentPairProcessor.GetSegmentPairInfo(segmentPair)?.SourceWordCounts;
                    }
                    catch
                    {
                        // catch all; ignore
                    }

                    SegmentPairInfos.Add(segmentPairInfo);
                }
            }

            AddSegmentPairs(paragraphUnit, segmentPairs);
        }
        public void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure || !paragraphUnit.SegmentPairs.Any())
            {
                return;
            }

            var transUnit = new TransUnit
            {
                Id = paragraphUnit.Properties.ParagraphUnitId.Id
            };

            foreach (var segmentPair in paragraphUnit.SegmentPairs)
            {
                var segmentPairInfo = SegmentPairProcessor.GetSegmentPairInfo(segmentPair);
                var status          = segmentPair.Properties.ConfirmationLevel.ToString();
                var match           = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);

                if (_exportOptions.ExcludeFilterIds != null)
                {
                    if ((segmentPair.Properties.IsLocked && _exportOptions.ExcludeFilterIds.Exists(a => a == "Locked")) ||
                        _exportOptions.ExcludeFilterIds.Exists(a => a == status) ||
                        _exportOptions.ExcludeFilterIds.Exists(a => a == match))
                    {
                        AddWordCounts(status, ConfirmationStatistics.WordCounts.Excluded, segmentPairInfo);
                        AddWordCounts(match, TranslationOriginStatistics.WordCounts.Excluded, segmentPairInfo);
                        continue;
                    }
                }

                SegmentVisitor.VisitSegment(segmentPair.Source);
                if (SegmentVisitor.HasRevisions)
                {
                    throw new Exception(PluginResources.Message_UnableToProcessFileWithTrackChanges +
                                        Environment.NewLine +
                                        PluginResources.Message_AccecptRejectTrackChangesBeforeExporting);
                }

                var sourceElements = SegmentVisitor.Elements;
                if (SegmentVisitor.Comments.Count > 0)
                {
                    foreach (var comment in SegmentVisitor.Comments)
                    {
                        if (!Xliff.DocInfo.Comments.ContainsKey(comment.Key))
                        {
                            Xliff.DocInfo.Comments.Add(comment.Key, comment.Value);
                        }
                    }
                }
                UpdateTagIds();

                if (segmentPair.Target?.Count == 0 && _exportOptions.CopySourceToTarget)
                {
                    segmentPair.Target.Clear();

                    foreach (var sourceElement in segmentPair.Source)
                    {
                        segmentPair.Target.Add(sourceElement.Clone() as IAbstractMarkupData);
                    }
                }

                SegmentVisitor.VisitSegment(segmentPair.Target);
                if (SegmentVisitor.HasRevisions)
                {
                    throw new Exception(PluginResources.Message_UnableToProcessFileWithTrackChanges +
                                        Environment.NewLine +
                                        PluginResources.Message_AccecptRejectTrackChangesBeforeExporting);
                }
                var targetElements = SegmentVisitor.Elements;
                if (SegmentVisitor.Comments.Count > 0)
                {
                    foreach (var comment in SegmentVisitor.Comments)
                    {
                        if (!Xliff.DocInfo.Comments.ContainsKey(comment.Key))
                        {
                            Xliff.DocInfo.Comments.Add(comment.Key, comment.Value);
                        }
                    }
                }
                UpdateTagIds();

                var newSegmentPair = new SegmentPair(_segmentBuilder)
                {
                    Id     = segmentPair.Properties.Id.Id,
                    Source = new Source {
                        Elements = sourceElements
                    },
                    Target = new Target {
                        Elements = targetElements
                    },
                    IsLocked          = segmentPair.Properties.IsLocked,
                    ConfirmationLevel = segmentPair.Properties.ConfirmationLevel,
                    TranslationOrigin = segmentPair.Properties.TranslationOrigin
                };
                transUnit.SegmentPairs.Add(newSegmentPair);

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

            if (transUnit.SegmentPairs.Count > 0)
            {
                Xliff.Files[Xliff.Files.Count - 1].Body.TransUnits.Add(transUnit);
            }
        }
        public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure || !paragraphUnit.SegmentPairs.Any())
            {
                base.ProcessParagraphUnit(paragraphUnit);
                return;
            }

            var importedTransUnit = GetTransUnit(paragraphUnit);

            if (importedTransUnit == null)
            {
                if (!string.IsNullOrEmpty(_importOptions.StatusSegmentNotImportedId))
                {
                    var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusSegmentNotImportedId, true, out var result);
                    var statusSegmentNotImported = success ? result : ConfirmationLevel.Unspecified;

                    foreach (var segmentPair in paragraphUnit.SegmentPairs)
                    {
                        var segmentPairInfo = SegmentPairProcessor.GetSegmentPairInfo(segmentPair);

                        segmentPair.Target.Properties.ConfirmationLevel = statusSegmentNotImported;

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

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

                base.ProcessParagraphUnit(paragraphUnit);
                return;
            }

            foreach (var segmentPair in paragraphUnit.SegmentPairs)
            {
                var segmentPairInfo = SegmentPairProcessor.GetSegmentPairInfo(segmentPair);

                var importedSegmentPair = importedTransUnit.SegmentPairs.FirstOrDefault(a => a.Id == segmentPair.Properties.Id.Id);
                if (importedSegmentPair == null)
                {
                    if (!string.IsNullOrEmpty(_importOptions.StatusSegmentNotImportedId))
                    {
                        var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusSegmentNotImportedId, true, out var result);
                        var statusSegmentNotImported = success ? result : ConfirmationLevel.Unspecified;

                        segmentPair.Target.Properties.ConfirmationLevel = statusSegmentNotImported;
                    }

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

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

                    continue;
                }

                var noOverwrite   = !_importOptions.OverwriteTranslations && segmentPair.Target.Any();
                var excludeFilter = false;
                if (_importOptions.ExcludeFilterIds != null)
                {
                    var status = segmentPair.Properties.ConfirmationLevel.ToString();
                    var match  = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);

                    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;
                    }

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

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

                    continue;
                }

                UpdateTargetSegment(segmentPair, importedSegmentPair, segmentPairInfo);
            }

            base.ProcessParagraphUnit(paragraphUnit);
        }
        public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure || !paragraphUnit.SegmentPairs.Any())
            {
                base.ProcessParagraphUnit(paragraphUnit);
                return;
            }

            var importedTransUnit = GetTransUnit(paragraphUnit);

            if (importedTransUnit == null)
            {
                if (!string.IsNullOrEmpty(_importOptions.StatusSegmentNotImportedId))
                {
                    var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusSegmentNotImportedId, true, out var result);
                    var statusSegmentNotImported = success ? result : ConfirmationLevel.Unspecified;

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

                        segmentPair.Target.Properties.ConfirmationLevel = statusSegmentNotImported;

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

                        AddWordCounts(status, segmentPairInfo, match);
                    }
                }

                base.ProcessParagraphUnit(paragraphUnit);
                return;
            }

            if (importedTransUnit.Contexts.Count > 0)
            {
                var contexts = paragraphUnit.Properties.Contexts.Contexts;
                foreach (var importContext in importedTransUnit.Contexts)
                {
                    var existingContext = contexts.FirstOrDefault(a =>
                                                                  string.Compare(a.ContextType, importContext.ContextType, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                                                  string.Compare(a.DisplayCode, importContext.DisplayCode, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                                                  string.Compare(a.DisplayName, importContext.DisplayName, StringComparison.CurrentCultureIgnoreCase) == 0 &&
                                                                  string.Compare(a.Description, importContext.Description, StringComparison.CurrentCultureIgnoreCase) == 0);

                    if (existingContext == null)
                    {
                        var newContext = _segmentBuilder.CreateContextInfo(importContext);
                        contexts.Add(newContext);
                    }
                }
            }

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

                var importedSegmentPair = importedTransUnit.SegmentPairs.FirstOrDefault(a => a.Id == segmentPair.Properties.Id.Id);
                if (importedSegmentPair == null)
                {
                    if (!string.IsNullOrEmpty(_importOptions.StatusSegmentNotImportedId))
                    {
                        var success = Enum.TryParse <ConfirmationLevel>(_importOptions.StatusSegmentNotImportedId, true, out var result);
                        var statusSegmentNotImported = success ? result : ConfirmationLevel.Unspecified;

                        segmentPair.Target.Properties.ConfirmationLevel = statusSegmentNotImported;
                    }

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

                    AddWordCounts(status, segmentPairInfo, match);

                    continue;
                }

                var noOverwrite   = !_importOptions.OverwriteTranslations && segmentPair.Target.Any();
                var excludeFilter = false;
                if (_importOptions.ExcludeFilterIds?.Count > 0)
                {
                    var status = segmentPair.Properties.ConfirmationLevel.ToString();
                    var match  = Enumerators.GetTranslationOriginType(segmentPair.Target.Properties.TranslationOrigin, _analysisBands);

                    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;
                    }
                    else
                    {
                        segmentPair.Target.Properties.ConfirmationLevel = importedSegmentPair.ConfirmationLevel;
                    }

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

                    AddWordCounts(status, segmentPairInfo, match);

                    continue;
                }

                UpdateTargetSegment(segmentPair, importedSegmentPair, segmentPairInfo);
            }

            base.ProcessParagraphUnit(paragraphUnit);
        }
Exemple #6
0
        public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            if (paragraphUnit.IsStructure)
            {
                return;
            }

            var priority = paragraphUnit.Properties.Contexts.Contexts.FirstOrDefault(
                a => a.ContextType == "Recommended" || a.ContextType.StartsWith("Alternative"));

            var textFunction = paragraphUnit.Properties.Contexts.Contexts.FirstOrDefault(
                a => string.Compare(a.DisplayName, "Text Function", StringComparison.CurrentCultureIgnoreCase) == 0);

            if (priority == null)
            {
                return;
            }

            if (_contextTypes.Count == 0)
            {
                _contextTypes.Add(priority.ContextType);
                _wordWriter.AddNewTable(priority.ContextType);
            }

            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);

                if (SkipSegment(segmentPair))
                {
                    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;
                }

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

                _tokenVisitor.Process(segmentPair.Source);
                var sourceText   = _tokenVisitor.PlainText.ToString();
                var sourceTokens = CloneList(_tokenVisitor.Tokens);

                _tokenVisitor.Process(segmentPair.Target);
                var targetText   = _tokenVisitor.PlainText.ToString();
                var targetTokens = CloneList(_tokenVisitor.Tokens);
                var comments     = _tokenVisitor.Comments;

                List <Token> backTranslationTokens = null;
                if (segmentPair.Target.Properties.TranslationOrigin != null &&
                    segmentPair.Target.Properties.TranslationOrigin.MetaDataContainsKey("back-translation"))
                {
                    //TODO check if we need to create TranslationOrigin??

                    var backTranslation =
                        segmentPair.Target.Properties.TranslationOrigin.GetMetaData("back-translation");

                    backTranslationTokens = JsonConvert.DeserializeObject <List <Token> >(backTranslation);
                }

                var textFunctionDescription = textFunction?.Description;
                if (string.IsNullOrEmpty(textFunctionDescription))
                {
                    textFunctionDescription = _previousTextFunctionDescription;
                }
                else
                {
                    _previousTextFunctionDescription = textFunctionDescription;
                }

                _wordWriter.WriteEntry(
                    segmentPair.Properties.Id.Id,
                    paragraphUnit.Properties.ParagraphUnitId.Id,
                    priority.ContextType,
                    textFunctionDescription,
                    sourceTokens,
                    targetTokens,
                    segmentPair.Properties,
                    backTranslationTokens);
            }
        }
Exemple #7
0
        public void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
        {
            #region  |  for comments that were added at the paragraph level  |

            var paragraphComments = new List <Comment>();

            if (paragraphUnit.Properties.Comments != null && paragraphUnit.Properties.Comments.Count > 0)
            {
                foreach (var commmentItem in paragraphUnit.Properties.Comments.Comments)
                {
                    var comment    = commmentItem;
                    var commentNew = new Comment {
                        Author = comment.Author
                    };
                    if (comment.DateSpecified)
                    {
                        commentNew.Date = comment.Date;
                    }
                    commentNew.Severity = comment.Severity.ToString();
                    commentNew.Text     = comment.Text;
                    commentNew.Version  = comment.Version;

                    paragraphComments.Add(commentNew);
                }
            }
            #endregion

            var index = -1;
            foreach (var segmentPair in paragraphUnit.SegmentPairs)
            {
                index++;

                var pair = new SegmentPair();

                ContentGeneratorProcessor.ProcessSegment(segmentPair.Source, IncludeTagText);
                ContentGeneratorProcessor.Segment.Culture = new CultureInfo(SourceLanguageId);

                try
                {
                    var results = SegmentPairProcessor.GetSegmentPairInfo(segmentPair);
                    if (results != null)
                    {
                        pair.SourceWords      = results.SourceWordCounts.Words;
                        pair.SourceChars      = results.SourceWordCounts.Characters;
                        pair.SourcePlaceables = results.SourceWordCounts.Placeables;
                        pair.SourceTags       = results.SourceWordCounts.Tags;
                    }
                }
                catch
                {
                    // catch all
                }

                pair.Source         = ContentGeneratorProcessor.PlainText.ToString();
                pair.SourceSections = ContentGeneratorProcessor.SegmentSections;

                ContentGeneratorProcessor.ProcessSegment(segmentPair.Target, IncludeTagText);
                pair.Target         = ContentGeneratorProcessor.PlainText.ToString();
                pair.TargetSections = ContentGeneratorProcessor.SegmentSections;
                pair.Id             = segmentPair.Properties.Id.Id;
                pair.SegmentStatus  = segmentPair.Properties.ConfirmationLevel.ToString();
                pair.IsLocked       = segmentPair.Properties.IsLocked;
                pair.Comments       = ContentGeneratorProcessor.Comments;

                #region  |  for comments that were added at the paragraph level  |

                if (paragraphComments.Count > 0 && index == 0)
                {
                    if (pair.Comments.Count > 0)
                    {
                        foreach (var commentParagraph in paragraphComments)
                        {
                            var foundComment = pair.Comments.Any(commentSegmentPair => string.CompareOrdinal(commentParagraph.Text.Trim(), commentSegmentPair.Text.Trim()) == 0);


                            if (!foundComment)
                            {
                                pair.Comments.Add(commentParagraph);
                            }
                        }
                    }
                    else
                    {
                        foreach (var comment in paragraphComments)
                        {
                            pair.Comments.Add(comment);
                        }
                    }
                }
                #endregion

                if (segmentPair.Properties.TranslationOrigin != null)
                {
                    pair.TranslationOrigin.IsRepeated = segmentPair.Properties.TranslationOrigin.IsRepeated;
                    pair.TranslationOrigin.IsStructureContextMatch = segmentPair.Properties.TranslationOrigin.IsStructureContextMatch;
                    pair.TranslationOrigin.MatchPercentage         = segmentPair.Properties.TranslationOrigin.MatchPercent;
                    pair.TranslationOrigin.OriginSystem            = segmentPair.Properties.TranslationOrigin.OriginSystem;
                    pair.TranslationOrigin.OriginType            = segmentPair.Properties.TranslationOrigin.OriginType;
                    pair.TranslationOrigin.RepetitionTableId     = segmentPair.Properties.TranslationOrigin.RepetitionTableId.Id;
                    pair.TranslationOrigin.TextContextMatchLevel = segmentPair.Properties.TranslationOrigin.TextContextMatchLevel.ToString();
                }

                #region  |  add the SegmentPair to the xParagraphs dictionary  |

                if (ParagraphUnits.ContainsKey(paragraphUnit.Properties.ParagraphUnitId.Id))
                {
                    var unit = ParagraphUnits[paragraphUnit.Properties.ParagraphUnitId.Id];
                    unit.SegmentPairs.Add(pair);
                    ParagraphUnits[paragraphUnit.Properties.ParagraphUnitId.Id] = unit;
                }
                else
                {
                    var segmentPairs = new List <SegmentPair> {
                        pair
                    };
                    ParagraphUnits.Add(paragraphUnit.Properties.ParagraphUnitId.Id, new ParagraphUnit(paragraphUnit.Properties.ParagraphUnitId.Id, segmentPairs));
                }
                #endregion
            }
        }
Exemple #8
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);
        }