Click or drag to resize
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 the user decides to run the batch task. This class implements the following interface:

The Abstract Task Interface
// A batch task plug-in needs to implement the AbstractFileContentProcessingAutomaticTask interface
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:

The Task Settings
// 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:
Report XML String
// 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 rocess the SDL XLIFF file

You can programmatically access the file that is currently processed through the following member. In a 'Hello World'-type implementation you could just output the name and path of the processed file.

Configuring the Converter
// 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 SDL XLIFF 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 SDL XLIFF is an XML-compliant file type, you could just process it using the standard XML API. However, we recommend that you use the SDL 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 implement the following interface:
Interface to Implement
// This class performs the actual work, it needs to implement the AbstractBilingualContentProcessor
// interface to process bilingual SDL XLIFF files
public class FileReader : AbstractBilingualContentProcessor

Add the following members to store the task settings, the file name and path of the SDL XLIFF file to be processed, and the text file name and path that is used to output the exported segments:

The Required Variables
// Variables to retrieve the task settings
// as well as the SDL XLIFF 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 SDL XLIFF file and the *.txt extension is appended.

Creating the Output File
// 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 SDL XLIFF 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:

Outputing the Segment Pairs
// 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.

File is Complete
// 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 SDL XLIFF files, the following member must be present to determine what happens when the process has been completed for the entire merged file.

Job is Complete
// 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 SDL XLIFF file name as well as our settings object:

Configure converter
// 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 SDL XLIFF 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.

Complete task and report string
// 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.

Community
Edit

Be the first to Edit the community content of this topic.
Comments