Copy source segment to target segment in Trados Editor

I have been trying to copy a segment text from source to target, but I couldn't be able to do that.

Is there a way to do this?

I tried the following code, but it didn't work. If there is a text or blank character in the target field, then I can do that.

doc.ProcessSegmentPairs("Copy",
                   (segPair, eventArg) =>
                   {
                       foreach (IAbstractMarkupData markupData in segPair.Source)
                       {                             
                               var text = markupData as IText;

                               if (text != null)
                               {
                                   (((IAbstractMarkupData )segPair.Target) as IText).Text = text.Properties.Text;
                               }                       
                       }
                   });

Parents
  • I asked the same question here: https://community.sdl.com/developers/language-developers/f/57/t/2907

    First off, segPair.Target is of type ISegment. If you look ISegment extends ICollection<IAbstractMarkupData> among other interfaces. So you would need to replace

    (((IAbstractMarkupData )segPair.Target) as IText).Text = text.Properties.Text;

    with

    foreach (IAbstractMarkupData markupData in segPair.Target)
    {
    var targetText = markupData as IText;

    if (targetText != null)
    {
    targetText.Properties.Text = sourceText.Properties.Text;
    }
    }

    Now, here is the catch, and there is a limitation in the SDK here. If the target segment is empty, the ICollection<IAbstractMarkupData> is also empty, thus the code in your foreach loop won't execute.

    In order to add some text to the empty target segment you would need some code like this

    // Start of Code that would allow me add text to empty target segment
    IText text = new Text();
    if (!segPair.Target.Any())
    {
    text.Properties.Text = sourceText;
    segPair.Target.Add(text);
    }
    // End of code to add text to empty segment
    else
    {
    // Iterate over all of the markup and replace the text
    foreach (IAbstractMarkupData markupData in segPair.Target)
    {
    text = markupData as IText;

    if (text != null)
    {
    text.Properties.Text = sourceText;
    }
    }
    }
    }

    But the problem here is that the Text object that is the implementation of the IText interface is in the "Sdl.FileTypeSupport.Framework.Implementation" assembly which is not authorized for use by third party plugins. So unless they update the SDK, I don't think there is a way that you can execute a "Copy Source to Target" unless you manually enter at least one character into the segment that you are trying to copy content to.

    The last thing to note is the function would only be copying over text from the source to the target, if there were any tags or any other markup this code would not copy those.

Reply
  • I asked the same question here: https://community.sdl.com/developers/language-developers/f/57/t/2907

    First off, segPair.Target is of type ISegment. If you look ISegment extends ICollection<IAbstractMarkupData> among other interfaces. So you would need to replace

    (((IAbstractMarkupData )segPair.Target) as IText).Text = text.Properties.Text;

    with

    foreach (IAbstractMarkupData markupData in segPair.Target)
    {
    var targetText = markupData as IText;

    if (targetText != null)
    {
    targetText.Properties.Text = sourceText.Properties.Text;
    }
    }

    Now, here is the catch, and there is a limitation in the SDK here. If the target segment is empty, the ICollection<IAbstractMarkupData> is also empty, thus the code in your foreach loop won't execute.

    In order to add some text to the empty target segment you would need some code like this

    // Start of Code that would allow me add text to empty target segment
    IText text = new Text();
    if (!segPair.Target.Any())
    {
    text.Properties.Text = sourceText;
    segPair.Target.Add(text);
    }
    // End of code to add text to empty segment
    else
    {
    // Iterate over all of the markup and replace the text
    foreach (IAbstractMarkupData markupData in segPair.Target)
    {
    text = markupData as IText;

    if (text != null)
    {
    text.Properties.Text = sourceText;
    }
    }
    }
    }

    But the problem here is that the Text object that is the implementation of the IText interface is in the "Sdl.FileTypeSupport.Framework.Implementation" assembly which is not authorized for use by third party plugins. So unless they update the SDK, I don't think there is a way that you can execute a "Copy Source to Target" unless you manually enter at least one character into the segment that you are trying to copy content to.

    The last thing to note is the function would only be copying over text from the source to the target, if there were any tags or any other markup this code would not copy those.

Children
No Data