Adding comments to sdlxliff file target segments

Hello,

I'm looking for some examples about adding comments to target segments. I have looked in the community Github repo, but I could only find how to read the comments from segments using a segment visitor.

Therefore, I was wondering if there was anything exposed to add comments to target segments, if possible, without using the editor controller. Could you point me into the right direction?

Thanks!

Kind regards,
Arturo

Parents
  • Hi  ,

    I did not try this myself but in the Trados Batch Anonymizer there is an example in the CommentVisitor where they change the author in an ICommentMarker.
    The visitor is used in the UserNameService class.
    An ICommentMarker has ICommentProperties which you should be able to use to retrieve and edit the IComments.
    They do this inside a batch task.

    I am not sure about this but if you don't want to use a visitor and already have your ISegmentPair / target ISegment, you should be able to filter the sub elements you get with target.AllSubItems and check if it is a ICommentMarker.

    As far as i know if you do not want to use the editor you must do your changes inside of an AbstractBilingualContentProcessors ProcessParagraphUnit method so that the changes are saved. In the Anonymizer example you can see this in the AnonymizerProcessor. Be sure to call base.ProcessParagraphUnit(paragraphUnit); in the ProcessParagraphUnit method of the AbstractBilingualContentProcessor. Otherwise your changes will not be saved (or it will just delete all the segments in your file. Ask me how i know)

    You need a way to get the IParagraphUnit into the AbstractBilingualContentProcessor. If you are doing this inside of a batch task it does this automatically for you.
    There are probably other ways where you can get these given to you by Studio but if you want to do this manually you need to load the sdlxliff file and process the AbstractBilingualContentProcessor.

    You can do this by getting an IFileTypeManager via the DefaultFileTypeManager. 

    var fTm = DefaultFileTypeManager.CreateInstance(true);


    The boolean argument loads your file types. If you do not set it to true it defaults to false and you can not import the sdlxliff.

    After that you can create a IMultiFileConverter with 

    IMultiFileConverter converter = ftm.GetConverterToDefaultBilingual(PATH_TO_SDLXLIFF_FILE, PATH_TO_SDLXLIFF_FILE, null); //first path is input, second path is output file


    Now you can put your AbstractBilingualContentProcessor into the converter with 
    converter.AddBilingualProcessor(collector);


    And then you have to call 

    converter.Parse()


    This will call the ProcessParagraphUnit method in the AbstractBilingualContentProcessor for all the IParagraphUnits in the file.

    Be aware that if you have tags in your segments they are not the placeholders that you see if you get the segments from the editor. They are raw. (I am still searching for a convenient way to get them into the same format without doing it manually)

    Sorry that this is a bit messy but I hope it helps.
    I really hope there is a better solution for this bloated process. I did not find one but i'd be glad to hear.

    Best
    Lukas

Reply
  • Hi  ,

    I did not try this myself but in the Trados Batch Anonymizer there is an example in the CommentVisitor where they change the author in an ICommentMarker.
    The visitor is used in the UserNameService class.
    An ICommentMarker has ICommentProperties which you should be able to use to retrieve and edit the IComments.
    They do this inside a batch task.

    I am not sure about this but if you don't want to use a visitor and already have your ISegmentPair / target ISegment, you should be able to filter the sub elements you get with target.AllSubItems and check if it is a ICommentMarker.

    As far as i know if you do not want to use the editor you must do your changes inside of an AbstractBilingualContentProcessors ProcessParagraphUnit method so that the changes are saved. In the Anonymizer example you can see this in the AnonymizerProcessor. Be sure to call base.ProcessParagraphUnit(paragraphUnit); in the ProcessParagraphUnit method of the AbstractBilingualContentProcessor. Otherwise your changes will not be saved (or it will just delete all the segments in your file. Ask me how i know)

    You need a way to get the IParagraphUnit into the AbstractBilingualContentProcessor. If you are doing this inside of a batch task it does this automatically for you.
    There are probably other ways where you can get these given to you by Studio but if you want to do this manually you need to load the sdlxliff file and process the AbstractBilingualContentProcessor.

    You can do this by getting an IFileTypeManager via the DefaultFileTypeManager. 

    var fTm = DefaultFileTypeManager.CreateInstance(true);


    The boolean argument loads your file types. If you do not set it to true it defaults to false and you can not import the sdlxliff.

    After that you can create a IMultiFileConverter with 

    IMultiFileConverter converter = ftm.GetConverterToDefaultBilingual(PATH_TO_SDLXLIFF_FILE, PATH_TO_SDLXLIFF_FILE, null); //first path is input, second path is output file


    Now you can put your AbstractBilingualContentProcessor into the converter with 
    converter.AddBilingualProcessor(collector);


    And then you have to call 

    converter.Parse()


    This will call the ProcessParagraphUnit method in the AbstractBilingualContentProcessor for all the IParagraphUnits in the file.

    Be aware that if you have tags in your segments they are not the placeholders that you see if you get the segments from the editor. They are raw. (I am still searching for a convenient way to get them into the same format without doing it manually)

    Sorry that this is a bit messy but I hope it helps.
    I really hope there is a better solution for this bloated process. I did not find one but i'd be glad to hear.

    Best
    Lukas

Children
  • Hi Lukas,

    Thanks for your detailed answer!

    It turns out that it was easier than expected and I was missing something very important that isn't part of the Visual Studio template of the plugin...

    Please make sure you override OnFileComplete() method in your application. If you don't override this method your changes on the file will not be saved.

    I forgot to override this method, and my changes were not being saved :')
    Here is my code in case this is useful for anyone. This is inside the ProcessParagraphUnit method of a class derived from AbstractBilingualContentProcessor
    var comment = CreateCommentContainer("Comment content", "Comment author", Severity.Low, DateTime.Now, "1");
    segmentPair.Target.MoveAllItemsTo(comment as IAbstractMarkupDataContainer);
    segmentPair.Target.Add(comment as IAbstractMarkupData);
    This method I took it from the community github repo:
    private IAbstractMarkupData CreateCommentContainer(string text, string author, Severity severity, DateTime dateTime, string version)
    {
    var comment = PropertiesFactory.CreateComment(text, author, severity);
    comment.Date = dateTime;
    comment.Version = version;
    
    var commentProperties = PropertiesFactory.CreateCommentProperties();
    commentProperties.Add(comment);
    var commentMarker = ItemFactory.CreateCommentMarker(commentProperties);
    
    return commentMarker;
    }