Store metadata per segment (SetMetaData)

Hi,

I would like to store some metadata in a project per segment (e.g. an additional identifier). For example, for segment 10, this would look like: ActiveDocument.SegmentPairs.ElementAt(10).Target.Properties.TranslationOrigin.SetMetaData("MyKey", "Meta data information");


This used to work in the past. However, after doing this on a Japanese translation segment, it does not seem to work anymore. After executing this code, "MyKey" is not added.

However, in the debugger, when I invoke this method, the item is added to MetaData.

Does anyone has a clue?

Are there other (better) ways to store information at a segment?

Best regards,
Henk
  • Hello Henk,

    There has been some enhancements in this area in CU10 for Studio 2014.  So if you only just updated then this is probably why your functionality broke.  Take a look at this article written by ... he can't respond right now as he's on a flight but if you're still struggling he can probably look later on.

    Integration API enhancements introduced in SDL Studio CU10

    Romulus Crisan
    Latest SDL Studio update, cumulative update 10, was released on 13th of May and together with this update there a few additions to the integration API. If you want to see the entire list of changes included in this update have a look here. Now lets get down to business

    These changes will also apply for Studio 2015 so good for you to incorporate them now.

    Regards

    Paul Filkin | RWS Group

    ________________________
    Design your own training!

    You've done the courses and still need to go a little further, or still not clear? 
    Tell us what you need in our Community Solutions Hub

  • Thanks Paul!

    Good to know that the changes in CU10 could cause this.

    The article shows the following example.

    var segmentPair = _editorController.ActiveDocument.ActiveSegmentPair;

    segmentPair.Properties.IsLocked = !segmentPair.Properties.IsLocked;

    _editorController.ActiveDocument.UpdateSegmentPairProperties(segmentPair.Properties);

    Unfortunately, UpdateSegmentPairProperties() does not seem be available via the API.

    Henk

  • The reason this is not working is because the property you are changing is actually a clone of the first one. More on this you can find in here:

    Adding comments to segments - Studio Developers Q&A - Trados Studio Developers - RWS Community

    community.sdl.com
    Hi, I would like to add comments to paragraphunits in an existing sdlxliff document this way: var pup = Controller.ActiveDocument.ActiveSegmentPair.GetParagraphUnitProperties
     

    Now the suggestion made by Paul is correct but after you reply I made some deeper investigation and it seems that this exact feature didn't catch the CU10 release and it will be available only Studio 2015. I will update my article and mention this.

    To try an fix your problem you can use Process Segment Pairs method which can do the exact same thing as the update method but you will need to add more boilerplate code.

    Romulus Crisan | Translation Productivity Development Manager | SDL | (twitter) @cromica_82 | (blog) http://www.romuluscrisan.com/

  • Thank you so much Romulus. You made my day! This workaround seems promising. I will do some tests and upload a code sample later.
    Henk
  • Hi Romulus, I added the following class. When you call the contructor, then it will add the metadata to the specified segment id.

    public class SegmentpairMetadata
    {
    int _segmentid = 0;
    string _key = null;
    string _value = null;

    /// <summary>
    /// Ctor. Set the meta data for the segment.
    /// </summary>
    public SegmentpairMetadata(Document activeDocument, int segmentId, string key, string value)
    {
    _segmentid = segmentId;
    _key = key;
    _value = value;

    activeDocument.ProcessSegmentPairs("DQF_metadata", UpdateSegmentPairProperties);
    }

    private void UpdateSegmentPairProperties(ISegmentPair segmentPair, CancelEventArgs e)
    {
    if (segmentPair.Properties.Id.Id == _segmentid.ToString(CultureInfo.InvariantCulture))
    {
    segmentPair.Properties.TranslationOrigin.SetMetaData(_key, _value);
    }
    }
    }
  • This does not seem to work if multiple files are opened.

    It is possible that multiple files are open in the active document. In that case you need to check if the segment pair belongs to the correct file where you want to add your metadata.

    For some reason, setting the metadata for the 2nd file does not seem to work.

    public SegmentpairMetadata(Document activeDocument, string currentfileId, int segmentId, string key, string value)
    {
    _currentfileId = currentfileId; //This is the File ID
    _segmentid = segmentId;
    _key = key;
    _value = value;
    activeDocument.ProcessSegmentPairs("DQF_metadata", UpdateSegmentPairProperties);
    }

    private void UpdateSegmentPairProperties(ISegmentPair segmentPair, CancelEventArgs e)
    {
    if ((_currentfileId == null) || (segmentPair.GetProjectFile().Id.ToString() != _currentfileId.ToLower()))
    {
    return;
    }

    if (segmentPair.Properties.Id.Id == _segmentid.ToString(CultureInfo.InvariantCulture))
    {
    segmentPair.Properties.TranslationOrigin.SetMetaData(_key, _value);
    }
    }
  • So are you saying that the SetMetaData function is not working when you call it or that the above code is not working? The reason I'm asking is because I know that there are some problem with determining the ActiveDocument in this multiple file case. Not sure how currentfileid is determined but if this is null you'll have to do a workaround in how you obtain the ActiveDocument. You can find the workaround here.

    Romulus Crisan | Translation Productivity Development Manager | SDL | (twitter) @cromica_82 | (blog) http://www.romuluscrisan.com/

  • Hi Romulus,
    Thanks for this response. The problem is not that the ActiveDocument is null. It has a valid pointer and I can access all document information and have access to the 2 files that are opened.

    The _currentfileid is the ID (Guid) of the ActiveFile (obtained via ActiveDocument.ActiveFile.Id). In the code snippet above, instead of a parameter, I could have used the assignment _currentfileid = ActiveDocument.ActiveFile.Id

    The method visits each segmentpair in the ActiveDocument. If the code finds the segment-ID for that particular file, then it should assign the meta data. However, this assignment only works for the first file, not for the second. It looks like the assignment is done on a clone of the object.

    Please let me know if you need more details.
    -Henk
  • So this means the issue is either with SetMetaDate method or that the segmentPair is just a clone. Are you able to try to do some tests on Studio 2015? The reason I'm asking is because in 2015 Integration API you would be able to use the method UpdateSegmentPair instead of ProcessSegment. More details about this method here. This way we might be able to find if it's cloning issue.

    Romulus Crisan | Translation Productivity Development Manager | SDL | (twitter) @cromica_82 | (blog) http://www.romuluscrisan.com/

  • The UpdateSegmentPair also does not work. I am using a class that inherits from AbstractAutoSuggestProvider. It provides access to the active document and also events when I change segments. 

    Update. The same happens with the EditorController. In the following code, "ActiveDocument_ActiveSegmentChanged" is invoked. The metadata is only set for the first file, not for the second. 

    EditorController _editorController;

    class MyProjectViewPart : AbstractViewPartController

    {
    protected override Control GetContentControl()
    {
    return _control.Value;
    }

    protected override void Initialize()
    {

    _editorController = SdlTradosStudio.Application.GetController<EditorController>();

    _editorController.ActiveDocument.ActiveSegmentChanged += ActiveDocument_ActiveSegmentChanged;

    }


    void ActiveDocument_ActiveSegmentChanged(object sender, EventArgs e)
    {
    var segmentPair = _editorController.ActiveDocument.ActiveSegmentPair;
    segmentPair.Properties.TranslationOrigin.SetMetaData("henk", "henk");
    _editorController.ActiveDocument.UpdateSegmentPair(segmentPair);
    }

1 2 3