In this page we'll explain how to edit an sdlxliff from selected project in Batch Task plugin context. Like in previous documentation we'll also use Project Anonymizer plugin for code example. Full source code of the project can be found here .
When we want to edit an sdlxliff file we need a BilingualProcessor.
How to create a Bilingual Processor
1. Create a new class which inherits AbstractBilingualContentProcessor class.
2. Override ProcessParagraphUnit() method.
public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
{
base.ProcessParagraphUnit(paragraphUnit);
if (paragraphUnit.IsStructure) { return; }
foreach (var segmentPair in paragraphUnit.SegmentPairs.ToList())
{
var segmentVisitor = new SegmentVisitor();
segmentVisitor.VisitText(segmentPair.Source);
}
}
Once we created the processor we need to add it to IMultiFileConverter object from ConfigureConverter method.
protected override void ConfigureConverter(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
{
multiFileConverter.AddBilingualProcessor(new BilingualContentHandlerAdapter(new YourCustomProcessor()));
}
Please make sure you override OnFileComplete() method in your application. If you don't override this method your changes on the file will not be saved.
public override bool OnFileComplete(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
{
return true;
}
In the processor class appears following object SegmentVisitor(). In order to read the information from each segment you need implement visitor pattern.
How to implement Visitor Pattern
1. Create a class which implements IMarkupDataVisitor interface.
2. Create a method which receive an ISegment object as parameter like this:
public void VisitText(ISegment segment)
{
VisitChildren(segment);
}
Where VisitChildren() has the following implementation:
private void VisitChildren(IAbstractMarkupDataContainer container)
{
if (container == null)
return;
foreach (var item in container.ToList())
{
item.AcceptVisitor(this);
}
}
3. Call VisitChildren() from VisitSegment() method like this:
public void VisitSegment(ISegment segment)
{
VisitChildren(segment);
}
4.
The last step is to add a implementation to VisitText() method. This method is fired automatically when we have a text in the segment.
public void VisitText(IText text)
{
text.Properties.Text = "New text we add to segment"
}
More logic should be added in your implementation which will fit your needs. In the above example for each segment from your file will contains only this "New text we add to segment".