Processing Files and Application Logic
Implement this functionality to update the status of document segments.
Triggering the Batch Task
Go to the MyCustomBatchTask.cs class. This class is triggered when you run the batch task. It inherits from the following abstract class:
// A batch task plug-in needs to implement the AbstractFileContentProcessingAutomaticTask class
public class MyCustomBatchTask : AbstractFileContentProcessingAutomaticTask
Declare a member to store the plug-in settings, and a string variable used to construct the XML stream for the task report content:
// These variables store the plug-in settings,
// the report string content
// and the language direction
private MyCustomBatchTaskSettings _settings;
private string _reportContent;
Initialize the task settings object and start constructing the report XML string by adding the root element. The root element should include the selected confirmation level value as an attribute:
// The task is initialized
// This means that the settings are retrieved,
// and we start constructing the report content string
protected override void OnInitializeTask()
{
_settings = GetSetting<MyCustomBatchTaskSettings>();
_reportContent +="<report segmentStatus='" + ((ConfirmationLevel)_settings.ConfirmationLevelSetting).ToString() + "'>";
}
Processing the SDLXliff File
You can programmatically access the file currently being processed through the following member. In a "Hello World" implementation, you could output the name and path of the processed file.
// Here we continue constructing the report string.
// Also we trigger the actual task by creating a FileReader object
// to which we pass the settings and the SDLXliff file name
protected override void ConfigureConverter(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
{
// We output each file name in the report
// and the date/time at which processing started
_reportContent += "<file>";
_reportContent += "<fileName>" + projectFile.Name + "</fileName>";
_reportContent += "<language>" + projectFile.Language.ToString() + "</language>";
_reportContent += "<processTime>" + DateTime.Now.ToString() + "</processTime>";
// We initialize the class that performs the actual work
FileReader _task = new FileReader(_settings, projectFile.LocalFilePath);
multiFileConverter.AddBilingualProcessor(_task);
multiFileConverter.Parse();
_reportContent += "</file>";
}
SDLXliff is an XML-compliant file type, so you could process it through the standard XML API. However, we recommend using the Trados Studio Bilingual API. To do this, add a new class to your project called FileReader.cs.
The FileReader.cs class must reference the following libraries:
using Sdl.Core.Globalization
using Sdl.FileTypeSupport.Framework.BilingualApi
It must also inherit from the following abstract class:
// This class performs the actual work, it needs to inherit from the
// AbstractBilingualContentProcessor class to process bilingual SDLXliff files
public class FileReader : AbstractBilingualContentProcessor
Add the following members to store the task settings, the file name and path of the SDLXliff file to process, and the text file name and path used to output the exported segments:
// Variables to retrieve the task settings
// as well as the SDLXliff file patch to process and
// the path of the TXT file that contains the exported content
private readonly MyCustomBatchTaskSettings _taskSettings;
private readonly string _inputFilePath;
private StreamWriter _outFile;
Outputting Segment Content to a Text File
Use the following member to create the text output file. This file is created in the same folder as the corresponding SDLXliff file, with the *.txt extension appended.
// We use this member to create the TXT export file
public override void SetFileProperties(IFileProperties fileInfo)
{
_outFile = new StreamWriter(_inputFilePath + ".txt");
}
Next, loop through each paragraph unit in the SDLXliff file. Process only paragraph units that contain segments. When a paragraph unit contains only structure tags (that is, no localizable segments), skip it. While looping through segment pairs, write all segments with the selected confirmation status to the output text file:
// This member loops through all the segments, determines the segment status,
// and then outputs the content to the text file (if applicable)
public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
{
// Check if this paragraph actually contains segments
// If not, it is just a structure tag content, which is not processed
if (paragraphUnit.IsStructure)
{
return;
}
// If the paragraph contains segment pairs, we loop through them,
// determine their confirmation status, and depending on the status
// output the text content to a TXT file
foreach (ISegmentPair item in paragraphUnit.SegmentPairs)
{
int segmentStatus = _taskSettings.ConfirmationLevelSetting;
if (item.Properties.ConfirmationLevel == (ConfirmationLevel)segmentStatus)
_outFile.WriteLine(item.Source + ";" + item.Target);
}
}
Once file processing is complete, close the text output file.
// Here we close the TXT file
public override void FileComplete()
{
base.FileComplete();
_outFile.Close();
}
The following member is required by the interface, although this implementation does not use it directly. The file complete member is called when file processing finishes. Because users can merge SDLXliff files, this member must be present to determine what happens when processing is complete for the entire merged file.
// Not really used in this implementation.
// Users can merge files and process them as one.
// If a single file is processed, the FileComlete member is invoked
// If all single files in a merged file are completed, then the Complete member is invoked
public override void Complete()
{
base.Complete();
}
Completing the Report String
Go back to the MyCustomBatchTask.cs class. Then do the following:
Continue constructing the XML string for the report by adding the name of the file currently processed, its target language, and the date and time it was processed.
Create a FileReader object and pass the current SDLXliff file name and your settings object:
// Here we continue constructing the report string.
// Also we trigger the actual task by creating a FileReader object
// to which we pass the settings and the SDLXliff file name
protected override void ConfigureConverter(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
{
// We output each file name in the report
// and the date/time at which processing started
_reportContent += "<file>";
_reportContent += "<fileName>" + projectFile.Name + "</fileName>";
_reportContent += "<language>" + projectFile.Language.ToString() + "</language>";
_reportContent += "<processTime>" + DateTime.Now.ToString() + "</processTime>";
// We initialize the class that performs the actual work
FileReader _task = new FileReader(_settings, projectFile.LocalFilePath);
multiFileConverter.AddBilingualProcessor(_task);
multiFileConverter.Parse();
_reportContent += "</file>";
}
Complete the task by adding the closing XML report string with the closing root element. Then generate the report by using the CreateReport method implemented by the interface.
// At the end of the task, we output the
public override void TaskComplete()
{
// We close the XML report string
_reportContent += "</report>";
// We call the method that actually creates the report in Studio
// In this method we pass the report name, description, the actual
// XML report content string and the optional language direction parameter.
CreateReport("SDK Sample Task Report", "Sample batch task plug-in report", _reportContent);
}
This method requires the report name, description, and the XML string for the report content. You may also add an optional language direction parameter. If this parameter is omitted, the report is not listed under a specific target language, but above all available target languages for the corresponding project.