Search Results for

    Show / Hide Table of Contents

    Adding the File Writer Class

    In this chapter you will learn how to implement the file writer component, which generates the target BIL file from an intermediary (SDLXliff) document.

    Add the Writer Class

    Start by adding a class called e.g. BilWriter.cs to your project. Like the parser component, this class uses the following namespaces of the bilingual and the native APIs:

    • Sdl.FileTypeSupport.Framework.BilingualApi
    • Sdl.FileTypeSupport.Framework.NativeApi

    Since the writer is going to generate XML target files, you should also add the System.Xml namespace. The class needs to be derived from the following class and interfaces:

    • AbstractBilingualFileTypeComponent
    • IBilingualWriter
    • INativeOutputSettingsAware

    Add the Members for the File Properties

    Start by adding the following global class members:

    • C#
    private IPersistentFileConversionProperties _originalFileProperties;
    private INativeOutputFileProperties _nativeFileProperties;
    private XmlDocument _targetFile;
    

    The _originalFileProperties object provides access to the source BIL file. From this object you can retrieve any information on the original file (e.g. the original source language, encoding, file path and name, etc.).

    The _nativeFileProperties object provides access to the (proposed) target BIL file information - most importantly to the target file output path.

    The _targetFile object is an XML DOM object that we will use for constructing the actual target BIL file output later.

    Now add the following (required) members of the INativeOutputSettingsAware interface to initialize the objects that provide access to the original file information and the native output file information:

    • C#
    public void GetProposedOutputFileInfo(IPersistentFileConversionProperties fileProperties, IOutputFileInfo proposedFileInfo)
    {
        _originalFileProperties = fileProperties;
    }
    
    public void SetOutputProperties(INativeOutputFileProperties properties)
    {
        _nativeFileProperties = properties;
    }
    

    Next, add the members of the IBilingualWriter interface. Start with the SetFileProperties() method, which we use to load the original BIL document into our _targetFile DOM object:

    • C#
    public void SetFileProperties(IFileProperties fileInfo)
    {
        _targetFile = new XmlDocument();
        _targetFile.Load(_originalFileProperties.OriginalFilePath);
    }
    
    #region "initialize"
    public void Initialize(IDocumentProperties documentInfo)
    {
    
    }
    #endregion
    

    Then add the Initialize() method, which we leave empty for the moment. We will use this member later (see Adding the Text Extractor Class) to create another object through with we will access the segment content:

    • C#
    public void Initialize(IDocumentProperties documentInfo)
    {
    
    }
    

    Create the Paragraph Units in the Output File

    Add another (required) member of the IBilingualWriter interface. This method is used to loop through the paragraph units of the intermediary (SDLXliff) file and output the unit elements to our target BIL file. We will leave this method empty for the moment and fill it with the required application logic later.

    • C#
    public void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
    {
    
    }
    

    Save and Complete the Process

    Add the following (required) members of the IBilingualWriter interface, which are used to complete the native output file and to complete the entire parsing process. We will use the FileComplete() method to save the target BIL file and to set the DOM object to null.

    We will leave the Complete() method empty, as it is not required in this simple implementation. You may wonder why the interface offers two (seemingly similar) members, i.e. one to complete a file, another one to complete the writing process. Remember that Trados Studio allows you to merge several native files into one big intermediary document, from which the individual target files can be generated when saving the document as target. You can use these two methods, for example, to first generate each native target file individually and then to conclude the entire writing process.

    • C#
    public void FileComplete()
    {
        _targetFile.Save(_nativeFileProperties.OutputFilePath);
        _targetFile = null;
    }
    
    public void Complete()
    {
    
    }
    

    Putting it All Together

    The skeleton writer class looks as shown below. This is the minimum amount of code required to build a file type plug-in with a writer component. You could actually build your project now, and the file writer will even produce a target BIL file. However, this target file will do nothing more than output the content of the original BIL document, which is embedded in the intermediary document as a dependency file. In the following chapters you will learn how to generate a BIL target file that includes the actual translations and comments entered in Trados Studio by manipulating the target file DOM object programmatically.

    • C#
    using System;
    using System.Xml;
    using Sdl.FileTypeSupport.Framework.BilingualApi;
    using Sdl.FileTypeSupport.Framework.NativeApi;
    
    namespace Sdk.Snippets.Bilingual
    {
        class BilWriter : AbstractBilingualFileTypeComponent, IBilingualWriter, INativeOutputSettingsAware
        {
            #region "global"
            private IPersistentFileConversionProperties _originalFileProperties;
            private INativeOutputFileProperties _nativeFileProperties;
            private XmlDocument _targetFile;
            #endregion
    
            #region "INativeOutputSettingsAware members"
            public void GetProposedOutputFileInfo(IPersistentFileConversionProperties fileProperties, IOutputFileInfo proposedFileInfo)
            {
                _originalFileProperties = fileProperties;
            }
    
            public void SetOutputProperties(INativeOutputFileProperties properties)
            {
                _nativeFileProperties = properties;
            }
            #endregion
    
    
            #region "IBilingualWriter members"
    
    
            #region "load file"
            public void SetFileProperties(IFileProperties fileInfo)
            {
                _targetFile = new XmlDocument();
                _targetFile.Load(_originalFileProperties.OriginalFilePath);
            }
    
            #region "initialize"
            public void Initialize(IDocumentProperties documentInfo)
            {
    
            }
            #endregion
    
            #endregion
    
            #region "paragraphs"
            public void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
            {
    
            }
            #endregion
    
            #region "save file and complete"
            public void FileComplete()
            {
                _targetFile.Save(_nativeFileProperties.OutputFilePath);
                _targetFile = null;
            }
    
            public void Complete()
            {
    
            }
            #endregion
    
            #endregion
    
            #region Implementation of IDisposable
    
            public void Dispose()
            {
                throw new NotImplementedException();
            }
    
            #endregion
        }
    }
    

    Reference the Component in the File Type Component Builder

    Do not forget to reference the file writer class by adding the following code to the IFileTypeComponentBuilder implementation:

    • C#
    public IFileGenerator BuildFileGenerator(string name)
    {
        return this.FileTypeManager.BuildFileGenerator(new BilWriter());
    }
    

    See Also

    Adding the Text Extractor Class

    Generating the Paragraph Units

    Mapping the Segment Confirmation Levels

    Outputting all Comments

    Note

    This content may be out-of-date. To check the latest information on this topic, inspect the libraries using the Visual Studio Object Browser.

    • Improve this Doc

    On this page

    • Add the Writer Class
    • Add the Members for the File Properties
    • Create the Paragraph Units in the Output File
    • Save and Complete the Process
    • Putting it All Together
    • Reference the Component in the File Type Component Builder
    • See Also
    Back to top Generated by DocFX