Processing Files and Application Logic
Implement the functionality that changes the status of document segments.
How to trigger the batch task
Go back to the MyCustomBatchTask.cs class. This class is triggered when you decide to run the batch task. This class inherits from the following abstract class:
// A batch task plug-in needs to implement the AbstractFileContentProcessingAutomaticTask class
public class MyCustomBatchTask : AbstractFileContentProcessingAutomaticTask
Declare a member that stores the plug-in settings, as well as a string variable that is 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;
Initialise the task settings object and start constructing the report XML string by adding the root element. The root element should contain the selected confirmation level value in 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() + "'>";
}
How to process the SDLXliff file
You can programmatically access the file that is currently processed through the following member. In a "Hello World"-type 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>";
}
As SDLXliff is an XML-compliant file type, you could process it using the standard XML API. However, we recommend that you use the Trados Studio Bilingual API to process the file. This way, we add a new class to our project called FileReader.cs.
The FileReader.cs class needs to reference the following libraries:
using Sdl.Core.Globalization
using Sdl.FileTypeSupport.Framework.BilingualApi
It also needs to 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 be processed, and the text file name and path that is 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;
How to output the segment content to a text file
With the following member we start by creating the text output file. This file is created in the same folder as the corresponding SDLXliff file and the *.txt extension is appended.
// We use this member to create the TXT export file
public override void SetFileProperties(IFileProperties fileInfo)
{
_outFile = new StreamWriter(_inputFilePath + ".txt");
}
In the next step, we loop through each paragraph unit of the SDLXliff file. We make sure to process only paragraph units that actually contain segments. When we encounter a paragraph unit that only contains structure tags (i.e. no localizable segments), we abort. When looping through the segment pairs, we 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 the file processing is done, we 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 our implementation does not actually use it. The file complete member is called when processing a file is finished. However, as users could merge SDLXliff files, the following member must be present to determine what happens when the process has been completed 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();
}
How to complete the Report String
Go back to the MyCustomBatchTask.cs class. Here we do the following:
We continue constructing the XML string for the report by adding the name of the file currently processed, its target language and the date/time at which it was processed.
We create a FileReader object to which we pass the current SDLXliff file name, as well as our 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>";
}
We complete the task by adding the closing XML report string with the closing root element. Then we generate the report 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 missing, the report will not be listed under the specific target language, but rather above all available target languages of the corresponding project.