Skip to content

Instantly share code, notes, and snippets.

@cromica
Last active March 9, 2016 13:23
Show Gist options
  • Save cromica/7a2ae9e07687a1913b8b to your computer and use it in GitHub Desktop.
Save cromica/7a2ae9e07687a1913b8b to your computer and use it in GitHub Desktop.
Sample code for creating a new segment using SDL Studio Integration API
//Get the editor
var editorController = SdlTradosStudio.Application.GetController<EditorController>();
if (editorController == null) return;
if (editorController.ActiveDocument == null) return;
//get the active segment pair from the current active document in the editor
var activeSegmentPair = editorController.ActiveDocument.ActiveSegmentPair;
if (activeSegmentPair == null) return;
//Create an instance of the document item factory that is need to create segment elements
IDocumentItemFactory documentItemFactory = DefaultDocumentItemFactory.CreateInstance();
//Create the text element
ITextProperties firstTextProp = documentItemFactory
.PropertiesFactory
.CreateTextProperties("Place the photo printer on a flat, clean and dust-free surface,");
IText firstText = documentItemFactory.CreateText(firstTextProp);
//Create bold tag start element
IStartTagProperties boldStartTagProperties = documentItemFactory
.PropertiesFactory
.CreateStartTagProperties("<cf bold=\"on\">");
boldStartTagProperties.Formatting.Add(new Bold(true));
//Create bold tag end element
IEndTagProperties boldEndTagProperties = documentItemFactory
.PropertiesFactory
.CreateEndTagProperties("</cf>");
//Create the tag pair that is the equivalent of <cf></cf>
ITagPair boldTagPair = documentItemFactory
.CreateTagPair(boldStartTagProperties, boldEndTagProperties);
//Create bold text element
ITextProperties boldTextProperties = documentItemFactory
.PropertiesFactory
.CreateTextProperties("in a dry location");
IText boldText = documentItemFactory.CreateText(boldTextProperties);
//Add the text content inside the start and end tag pairs
boldTagPair.Add(boldText);
//Create the text element
ITextProperties secondTextProp = documentItemFactory
.PropertiesFactory
.CreateTextProperties(", and ");
IText secondText = documentItemFactory.CreateText(secondTextProp);
//Create italic tag start element
IStartTagProperties italicStartTagProperties = documentItemFactory
.PropertiesFactory
.CreateStartTagProperties("<cf italic=\"on\">");
boldStartTagProperties.Formatting.Add(new Italic(true));
//Create italic tag end element
IEndTagProperties italicEndTagProperties = documentItemFactory
.PropertiesFactory
.CreateEndTagProperties("</cf>");
//Create the tag pair that is the equivalent of <cf></cf>
ITagPair italicTagPair = documentItemFactory
.CreateTagPair(italicStartTagProperties, italicEndTagProperties);
//Create the text element
ITextProperties italicTextProp = documentItemFactory
.PropertiesFactory
.CreateTextProperties("out of direct sunlight");
IText italicText = documentItemFactory.CreateText(italicTextProp);
//Add the text content inside the start and end tag pairs
italicTagPair.Add(italicText);
//Create the text element
ITextProperties thirdTextProp = documentItemFactory
.PropertiesFactory
.CreateTextProperties(".");
IText thirdText = documentItemFactory.CreateText(thirdTextProp);
//Create the new target segment
ISegment newSegment = documentItemFactory.CreateSegment(activeSegmentPair.Target.Properties);
//Add the newly content elements
newSegment.Add(firstText);
newSegment.Add(boldTagPair);
newSegment.Add(secondText);
newSegment.Add(italicTagPair);
newSegment.Add(thirdText);
//Create a new temporary segment pair that we will use to update the existing segment pair
ISegmentPair tempSegmentPair = documentItemFactory.CreateSegmentPair(activeSegmentPair.Source, newSegment);
editorController.ActiveDocument.UpdateSegmentPair(tempSegmentPair);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment