Lock segment(s) without launching SDL Studio through API

Dear All,

My goal is to automate locking segment(s) (based on XML tags) on SDL project before translators start their job. I tried to archive it through Project Template but unfortunately it is not possible (SDL Studio 2019 SR3). Maybe I miss something?

Thanks to Project Template I am able to "mark" segments which I want to lock. Next step is to create small app which will do the job in sdlxliff file.

So:

- I created: public class ParagraphUnitsProcessor : AbstractBilingualContentProcessor where I get the Collection of IParagraphUnit from sdlxliff

- I iterate through the collection, select segments based on XML tags from source, and get SegmentPairs from IParagraphUnit 

- I iterate through SegmentPairs and lock the segment as follow

segmentPair.Properties.IsLocked = true;
segmentPair.Properties.ConfirmationLevel = Sdl.Core.Globalization.ConfirmationLevel.Unspecified;

I miss the part (basically do not know) is the way how to save the job, so that next time the project is launched segments will be locked.

Would you be so kind and let me know if what I did since now is correct and how to save the locked segments?

Do I need to use Visitor for this task?

Thank you so much!

Kind regards,

Michal.

emoji
Parents
  • Full code for retrieving IParagraphUnits:

    ParagraphUnitsProcessor processor;
    
    try
        {
            IFileTypeManager fileTypeManager = DefaultFileTypeManager.CreateInstance(true);
            IMultiFileConverter converter = fileTypeManager.GetConverterToDefaultBilingual(inputPath, outputPath, default);
            processor = new ParagraphUnitsProcessor();
            converter.AddBilingualProcessor(processor);
            converter.Parse();
        }
    catch
    {
        throw;
    }

    LockSegment method:

    foreach (var paragraph in paragraphUnits)
                {
                    ITagPair stag = paragraph.Source.FirstOrDefault() as ITagPair;
    
                    //Validate if it's correct segment
                    if (stag != null &&
                        stag.TagProperties.DisplayText == "term" &&
                        stag.TagProperties.TagContent.Contains("ep:class=\"Amendment Number\""))
                    {
                        // Lock the segmsnt
                        var segmentPairs = paragraph.SegmentPairs;
                        foreach (var segmentPair in segmentPairs)
                        {
                            segmentPair.Properties.IsLocked = true;
                            segmentPair.Properties.ConfirmationLevel = Sdl.Core.Globalization.ConfirmationLevel.Unspecified;
                        }
                    }
                }

    ParagraphUnitsProcessor:

    public class ParagraphUnitsProcessor : AbstractBilingualContentProcessor
        {
            #region Public Constructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="ParagraphUnitsProcessor"/> class.
            /// </summary>
            /// <param name="isIncludeStructure">
            /// if set to <c>true</c> includes structure <see cref="IParagraphUnit"/> instances.
            /// </param>
            public ParagraphUnitsProcessor(in bool isIncludeStructure = false)
            {
                // Initializes properties.
                IsIncludeStructure = isIncludeStructure;
                ParagraphUnitsList = new List<IParagraphUnit>();
            }
    
            #endregion Public Constructors
    
            #region Public Properties
    
            /// <summary>
            /// Gets a value indicating whether this instance is include structure <see cref="IParagraph"/> instances.
            /// </summary>
            /// <value>
            /// <c>true</c> if this instance is include structure <see cref="IParagraph"/> instances; otherwise, <c>false</c>.
            /// </value>
            public bool IsIncludeStructure { get; }
    
            /// <summary>
            /// Gets the <see cref="IList{T}"/> of <see cref="IParagraphUnit"/> instances.
            /// </summary>
            /// <value>The <see cref="IList{T}"/> of <see cref="IParagraphUnit"/> instances.</value>
            public List<IParagraphUnit> ParagraphUnitsList { get; }
            
            #endregion Public Properties        
    
            #region Public Methods
    
            /// <inheritdoc/>
            public override void Complete()
            {
                base.Complete();
                ParagraphUnitsList.TrimExcess();            
            }
    
            /// <inheritdoc/>
            public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
            {
                base.ProcessParagraphUnit(paragraphUnit);
    
                if (paragraphUnit is null)
                {
                    return;
                }
    
                if (paragraphUnit.IsStructure &&
                    !IsIncludeStructure)
                {
                    return;
                }
    
                ParagraphUnitsList.Add(paragraphUnit);
            }
    
            #endregion Public Methods  
        }

    emoji
  • I found the solution!

    I document it here in case if anyone else would like to do something similar.

    The trick is in ProcessParagraphUnit method of ParagraphUnitsProcessor class. Basically, every manipulation you want to do on the IParagraphUnit MUST BE done in this place.

    So in my case, LockSegment method needs to go to "public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)" method without foreach loop and should looks like this:

    /// <inheritdoc/>
    public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
    {
        base.ProcessParagraphUnit(paragraphUnit);
    
        if (paragraphUnit is null)
        {
            return;
        }
    
        if (paragraphUnit.IsStructure &&
                    !IsIncludeStructure)
        {
            return;
        }
        
        ITagPair stag = paragraph.Source.FirstOrDefault() as ITagPair;
    
        //Validate if it's correct segment
        if (stag != null &&
            stag.TagProperties.DisplayText == "term" &&
            stag.TagProperties.TagContent.Contains("ep:class=\"Amendment Number\""))
        {
            // Lock the segmsnt
            var segmentPairs = paragraph.SegmentPairs;
            foreach (var segmentPair in segmentPairs)
            {
                segmentPair.Properties.IsLocked = true;
                segmentPair.Properties.ConfirmationLevel = Sdl.Core.Globalization.ConfirmationLevel.Unspecified;
            }
        }
    }

    emoji
Reply
  • I found the solution!

    I document it here in case if anyone else would like to do something similar.

    The trick is in ProcessParagraphUnit method of ParagraphUnitsProcessor class. Basically, every manipulation you want to do on the IParagraphUnit MUST BE done in this place.

    So in my case, LockSegment method needs to go to "public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)" method without foreach loop and should looks like this:

    /// <inheritdoc/>
    public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
    {
        base.ProcessParagraphUnit(paragraphUnit);
    
        if (paragraphUnit is null)
        {
            return;
        }
    
        if (paragraphUnit.IsStructure &&
                    !IsIncludeStructure)
        {
            return;
        }
        
        ITagPair stag = paragraph.Source.FirstOrDefault() as ITagPair;
    
        //Validate if it's correct segment
        if (stag != null &&
            stag.TagProperties.DisplayText == "term" &&
            stag.TagProperties.TagContent.Contains("ep:class=\"Amendment Number\""))
        {
            // Lock the segmsnt
            var segmentPairs = paragraph.SegmentPairs;
            foreach (var segmentPair in segmentPairs)
            {
                segmentPair.Properties.IsLocked = true;
                segmentPair.Properties.ConfirmationLevel = Sdl.Core.Globalization.ConfirmationLevel.Unspecified;
            }
        }
    }

    emoji
Children
No Data