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
Parents
  • 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.

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

    Regards

    Paul Filkin | RWS

    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:

     

    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);
    }
    }
Reply
  • 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);
    }
    }
Children