Exporting to a TMX File
Sometimes you may want to export TM content to an external TMX file, for example, to create backups or merge content into another TM.
Add a New Class
Add a new class named TmExporter to your project. Then implement a method named ExportTMXFile(). Call it as shown below:
var tmExporter = new TmExporter();
tmExporter.ExportTMXFile(_translationMemoryFilePath, _exportFilePath);
tmExporter.RunFilteredExport(_translationMemoryFilePath, _exportFilePath);
The method requires the TM path and the TMX export file path as parameters.
Start by opening the TM and creating an exporter object:
var tm = new FileBasedTranslationMemory(tmPath);
var exporter = new TranslationMemoryExporter(tm.LanguageDirection);
Note that the TranslationMemoryExporter object requires the TM language direction. Like a TM, a TMX file is bilingual and uses a language direction that must match the TM direction. Pass the TM language direction when you create the exporter.
Next, you can set the ChunkSize. The chunk size determines the maximum number of units read from the TM at one time. If the TM is on a local disk, a larger chunk size is usually fine. If the export runs over a network, keep the chunk size smaller to reduce latency. In this example, set the chunk size to 20, which roughly matches the size of the sample TM:
exporter.ChunkSize = 20;
The default chunk size is 50 (DefaultTranslationUnitChunkSize), and the maximum is 200 (MaxTranslationUnitChunkSize).
Before you execute the export, subscribe to an event that fires after each batch, or chunk, is exported:
exporter.BatchExported += new EventHandler<BatchExportedEventArgs>(this.exporter_BatchExported);
Add the following member to your class. The ExportTMXFile() method triggers it:
private void exporter_BatchExported(object sender, BatchExportedEventArgs e)
{
string info;
info = "Total TUs processed: " + e.TotalProcessed + "\n";
info += "Total TUs exported: " + e.TotalExported + "\n";
MessageBox.Show(info, "Export statistics for batch");
e.Cancel = false;
}
After each batch is processed, the code reports the total number of units processed and the number actually exported.
Finally, call the Export() method to perform the export. This method requires the TMX export file path. The second boolean parameter determines whether an existing export file is overwritten.
exporter.Export(exportFilePath, true);
In the example above, the overwrite parameter is set to true. If you set it to false and a file with the same name already exists, the export throws an error that your code must handle.
The complete ExportTMXFile() method looks like this:
public void ExportTMXFile(string tmPath, string exportFilePath)
{
#region "open"
var tm = new FileBasedTranslationMemory(tmPath);
var exporter = new TranslationMemoryExporter(tm.LanguageDirection);
#endregion
#region "chunk"
exporter.ChunkSize = 20;
#endregion
#region "FireEvent"
exporter.BatchExported += new EventHandler<BatchExportedEventArgs>(this.exporter_BatchExported);
#endregion
#region "execute"
exporter.Export(exportFilePath, true);
#endregion
}
Run a Filtered Export
If you do not want to export the entire TM, you can run a filtered export. This lets you export a subset of the TM content, such as all translation units with a Customer field value of Microsoft or all TUs created after 1 January 2010. The overall workflow is the same as for a full export, but you add a filter before you run the export. Start by implementing another method named RunFilteredExport(), which takes the TM path and the TMX export file path as string parameters. First, open the TM and create a TM exporter object:
var tm = new FileBasedTranslationMemory(tmPath);
var exporter = new TranslationMemoryExporter(tm.LanguageDirection);
The main difference from the full export is that you set the FilterExpression property on the exporter. A separate helper method builds the filter expression.
exporter.FilterExpression = this.GetFilterSimple();
Like before, subscribe to the batch event and call Export() to perform the export. The complete RunFilteredExport() method looks like this:
public void RunFilteredExport(string tmPath, string exportFilePath)
{
#region "OpenForFilter"
var tm = new FileBasedTranslationMemory(tmPath);
var exporter = new TranslationMemoryExporter(tm.LanguageDirection);
#endregion
#region "FilterDefinition"
exporter.FilterExpression = this.GetFilterSimple();
#endregion
#region "DoFilteredExport"
exporter.BatchExported += new EventHandler<BatchExportedEventArgs>(this.exporter_BatchExported);
exporter.Export(exportFilePath, true);
#endregion
}
Define the Filter for the Export (Simple)
This section shows how to implement the helper method that creates a simple export filter. Add a method named GetFilterSimple() that returns a FilterExpression object:
private FilterExpression GetFilterSimple()
Suppose you want the export to include only TUs where the Customer field equals Microsoft. In this example, Customer is a picklist field that allows multiple values. The following sample code shows how to define the field name and value and build the criterion:
var fieldName = new PicklistItem("Customer");
var fieldValue = new MultiplePicklistFieldValue("Microsoft");
fieldValue.Add(fieldName);
Next, use the AtomicExpression class to create the filter expression to return to the export method. Pass the field value and the operator. In this case, the filter uses Equal. Other possible operators include Contains, ContainsNot, Greater, and Smaller.
var filter = new AtomicExpression(fieldValue, AtomicExpression.Operator.Equal);
return filter;
Define the Filter for the Export (Advanced)
Suppose you want a more advanced filter with two or more criteria, such as exporting all TUs where the Customer field equals Microsoft or the Project id text field contains 2010. Add another helper method named GetFilterAdvanced():
private FilterExpression GetFilterAdvanced()
The first step is almost identical to the previous example: define the first criterion and create the first filter expression with the AtomicExpression class.
var fieldName1 = new PicklistItem("Customer");
var fieldValue1 = new MultiplePicklistFieldValue("Microsoft");
fieldValue1.Add(fieldName1);
var expression1 = new AtomicExpression(fieldValue1, AtomicExpression.Operator.Equal);
Then define the second criterion, such as Project id containing 2010, and use the AtomicExpression class to construct the second expression:
var fieldName2 = new MultipleStringFieldValue("Project id");
fieldName2.Add("2010");
var expression2 = new AtomicExpression(fieldName2, AtomicExpression.Operator.Contains);
The final filter object uses the ComposedExpression class, which combines expression1 and expression2 with an OR operator and returns the complete filter expression.
var filter = new ComposedExpression(expression1, ComposedExpression.Operator.Or, expression2);
return filter;
Putting it All Together
The complete class should now look like this:
namespace SDK.LanguagePlatform.Samples.TmAutomation
{
using System;
using System.Windows.Forms;
using Sdl.LanguagePlatform.TranslationMemory;
using Sdl.LanguagePlatform.TranslationMemoryApi;
public class TmExporter
{
#region "export"
public void ExportTMXFile(string tmPath, string exportFilePath)
{
#region "open"
var tm = new FileBasedTranslationMemory(tmPath);
var exporter = new TranslationMemoryExporter(tm.LanguageDirection);
#endregion
#region "chunk"
exporter.ChunkSize = 20;
#endregion
#region "FireEvent"
exporter.BatchExported += new EventHandler<BatchExportedEventArgs>(this.exporter_BatchExported);
#endregion
#region "execute"
exporter.Export(exportFilePath, true);
#endregion
}
#endregion
#region "event"
private void exporter_BatchExported(object sender, BatchExportedEventArgs e)
{
string info;
info = "Total TUs processed: " + e.TotalProcessed + "\n";
info += "Total TUs exported: " + e.TotalExported + "\n";
MessageBox.Show(info, "Export statistics for batch");
e.Cancel = false;
}
#endregion
#region "RunFilteredExport"
public void RunFilteredExport(string tmPath, string exportFilePath)
{
#region "OpenForFilter"
var tm = new FileBasedTranslationMemory(tmPath);
var exporter = new TranslationMemoryExporter(tm.LanguageDirection);
#endregion
#region "FilterDefinition"
exporter.FilterExpression = this.GetFilterSimple();
#endregion
#region "DoFilteredExport"
exporter.BatchExported += new EventHandler<BatchExportedEventArgs>(this.exporter_BatchExported);
exporter.Export(exportFilePath, true);
#endregion
}
#endregion
#region "GetFilterSimple"
private FilterExpression GetFilterSimple()
{
#region "SimpleCriterion"
var fieldName = new PicklistItem("Customer");
var fieldValue = new MultiplePicklistFieldValue("Microsoft");
fieldValue.Add(fieldName);
#endregion
#region "SimpleFilter"
var filter = new AtomicExpression(fieldValue, AtomicExpression.Operator.Equal);
return filter;
#endregion
}
#endregion
#region "GetFilterAdvanced"
private FilterExpression GetFilterAdvanced()
{
#region "AdvancedCriterion1"
var fieldName1 = new PicklistItem("Customer");
var fieldValue1 = new MultiplePicklistFieldValue("Microsoft");
fieldValue1.Add(fieldName1);
var expression1 = new AtomicExpression(fieldValue1, AtomicExpression.Operator.Equal);
#endregion
#region "AdvancedCriterion2"
var fieldName2 = new MultipleStringFieldValue("Project id");
fieldName2.Add("2010");
var expression2 = new AtomicExpression(fieldName2, AtomicExpression.Operator.Contains);
#endregion
#region "AdvancedFilter"
var filter = new ComposedExpression(expression1, ComposedExpression.Operator.Or, expression2);
return filter;
#endregion
}
#endregion
}
}