Batch API: How to lock part of target segment

Hello,

I like to program a batch task that checks the target files for specific strings and locks them if found. Can you tell me how to achieve this, please.

I think I have to program a processor but I dont know from which class I have to inherit from. 

Is it even the right way to achieve this with a batch task?

Thanks for your help in advance.

Parents
  • Hi Robert,

    Please take a look at the Export to Excel sample application on Github:

    In order, first you have to inherit from AbstractFileContentProcessingAutomaticTask as shown at ExportToExcelTask.cs:

    * Since you want to modify the sdlxliff file make sure you override OnFileComplete and make it return true as shown below.

    The sample application does not do this because it does not modify the file.

    public override bool OnFileComplete(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
    {
        return true;
    }

     

    Next you add your processor by calling AddBilingualProcessor:

    * It is important here you wrap it in BilingualContentHandlerAdapter and your class inherits from AbstractBilingualContentHandler

    * You could also inherit from IBilingualContentHandler directly, AbstractBilingualContentHandler is just there as a convenience so you can override only what you need

    multiFileConverter.AddBilingualProcessor(new BilingualContentHandlerAdapter(new SegmentLocker(settings));

     

    The SegmentLocker class would look like this:

    public class SegmentLocker : AbstractBilingualContentHandler
    {
        	private readonly MySettings settings;
    
    	public SourceSegmentProcessor(MySettings settings)
    	{
    	    this.settings = settings;
    	}
    
    	public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
    	{
    		// Check if it is structure unit and ignore
    		if (paragraphUnit.IsStructure) { return; }
    
    		// Check each segment and lock here
    	}
    }

     Check out how the sample implementation does it here. However, note it uses AbstractBilingualContentProcessor which is not for writing to the file.

  • Hello Jesse,

    thank you so far. Now I am able to change the content of segements but I still have problems locking the wished parts.

    First I wrote a class that inherits from the IMarkupDataVisitor. So I was able to iterate the different content types. In the VisitText method I tried to surround text with a lock tag but I don't know how. Can you show me how?

    Then I tried to work directly on the segment pairs. There I was able to add or insert IAbstractMarkupData, but don't know how to get the index of a string from a segment, so that I can replace that string with an IAbstractMarkupData. Maybe you can help me there too?!

    Thank you.

  • Hi Robert,

    The following is *very* crude, but it should show the general idea:

    foreach (var pair in paragraphUnit.SegmentPairs)
    {
    	var source = pair.Source;
    // This assumes there is only one IText in the source, but you need to to use the visitor pattern
    // In real code I would do this in VisitText var markup = source.First(); if (markup is IText) { IText txt = (IText)markup;
    // Use Split here to break the segment at the point you want to insert the inline tag var remainderPart = txt.Split(txt.ToString().IndexOf("Hello"));
    // Remove the text you will replace with the inline tag var tmp = remainderPart.Properties.Text; remainderPart.Properties.Text = tmp.Replace("Hello", "");
    // Create the placeholder var placeHolderTagProps = ItemFactory.PropertiesFactory.CreatePlaceholderTagProperties(@"<locked name=""Hello""/>"); var placeHolderTag = ItemFactory.CreatePlaceholderTag(placeHolderTagProps);
    // The beginning part remains so add the rest afterwards source.Add(placeHolderTag); source.Add(remainderPart); } }

     

     All it does it take "Hello"s and make them placeholders.

    You should be able to adapt the above with your implementation of IAbstractMarkupData.

  • Hello Jesse,

    thank you for you last reply.

    I tried what you suggested but I don't get it working. I tried several approaches:
    - I tried to use parts of your code in the VisitorPattern but as soon as i modify the structure I get an error that this is not allowed
    - Then I created a tokens list with all relevant structural information I need. From this list I recreated the structure. I tried to add it to the ISegment. But this also did not work. I ended in an infinity loop.
    - Then I tried to create my own temporary segmentPair and passed it but then I got an error when opening the document in the Studio.

    Can you give me one more hint to reach my goal, please?! :) I am a little bit frustrated...

    Thank you
    Andre

    PS: Last time I used the account of my college, so don't be confused :)
  • Hi Andre,

    I made reference implementation to see how it is done.

    In this reference implementation you type in what word you want to replace in the settings and it will replace it with a placeholder.

    So for example, if you type "Name", it will replace with "<Name />". Note that it has to be a valid XML element name, for other things, you need to add an attribute and add it to the attribute instead.

    All the work is done inside of:

    Also, if you create a placeholder programmatically, you need to also return it back to plain text when generating translations!

    I'm currently developing a tool with this functionality:

  • Thank you very much, Jesse.
    I could adopt it for locking my words. Finally I got it working :D
Reply Children
No Data