How does TranslationUnit.SetValue() works?

Hello!

I'm trying to use the two methods "TranslationUnit.SetValue()" and "TranslationUnit.GetValue()"  in order to store a custom value per-tu in my plugin implementation. However I'm not able to make it work. In my search function, I've added this code snippet:

public SearchResults SearchTranslationUnit(SearchSettings settings, TranslationUnit translationUnit)
{
logger.Info("BEFORE SET: " + translationUnit.GetValue("my-custom-value")?.GetValueString());
translationUnit.SetValue(new SingleStringFieldValue("my-custom-value", "Hello world"), true);
logger.Info("AFTER SET: " + translationUnit.GetValue("my-custom-value")?.GetValueString());
...

So my guess was that, after the first SET, I would have being able to get the value "Hello world" in the first "BEFORE SET". Problem is that, no matter how many time a focus the same segment in the Editor page, the value is never actually stored in the Translation Unit, and my log file is full of these messages:

[2019-10-28 10:32:29.8553] INFO MyProviderLanguageDirection - BEFORE SET: 
[2019-10-28 10:32:29.8553] INFO MyProviderLanguageDirection - AFTER SET: "Hello world"
[2019-10-28 10:32:30.3620] INFO MyProviderLanguageDirection - BEFORE SET:
[2019-10-28 10:32:30.3818] INFO MyProviderLanguageDirection - AFTER SET: "Hello world"

So what am I doing wrong?

Thanks for your help!

Parents Reply Children
  • No luck:

    public SearchResults SearchTranslationUnit(SearchSettings settings, TranslationUnit translationUnit)
    {
    logger.Info("On-focus value is: " + translationUnit.FieldValues.Lookup("my-custom-value"));

    // Creating custom field value
    var fieldDefinition = new FieldDefinition("my-custom-value", FieldValueType.SingleString);
    var fieldValue = fieldDefinition.CreateValue();
    fieldValue.Add("Hello world");
    translationUnit.FieldValues.Add(fieldValue);

    // Creating "echo" result
    translationUnit.TargetSegment = translationUnit.SourceSegment;
    translationUnit.Origin = TranslationUnitOrigin.MachineTranslation;
    translationUnit.ConfirmationLevel = Sdl.Core.Globalization.ConfirmationLevel.Draft;

    // Creating result object
    SearchResults results = new SearchResults();
    results.SourceSegment = translationUnit.SourceSegment;
    results.Add(new SearchResult(translationUnit)
    {
    TranslationProposal = translationUnit,
    ScoringResult = new ScoringResult()
    {
    BaseScore = 0
    }
    });

    return results;
    }