| Exporting to a TMX File |
Sometimes you may want to export the content of a TM to an external TMX file, for example, for generating backups, importing the TMX file into another TM, thereby merging different TMs, etc.
Add a new class called TmExporter to your project. Then, implement a function called ExportTMXFile. This function can be called as shown below:
TMExporter objTmExport = new TMExporter();
objTmExport.ExportTMXFile(_translationMemoryFilePath, _exportFilePath);
objTmExport.RunFilteredExport(_translationMemoryFilePath, _exportFilePath);Start by opening the TM and by creating an exporter object:
FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationMemoryExporter exporter = new TranslationMemoryExporter(tm.LanguageDirection);
In the next step, you may specify the ChunkSize. With the chunk size you determine the maximum amount of units that should be read from the TM. As long as the TM file resides on a local disk, the chunk size can be large, as the whole TM could be read in one go. If the export is going over e.g. an Internet line, then the chunk size should be small enough for the packages to be sent over the WAN. In our example, the chunk size is set to 20, which corresponds roughly to the size of our small sample TM:
exporter.ChunkSize = 20;Before executing the export we fire an event after each batch (as specified by the chunk size) has been performed:
exporter.BatchExported += new EventHandler<BatchExportedEventArgs>(this.exporter_BatchExported);
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; }
Finally, you apply the Export method to carry out the actual export operation. This method requires the full name and path of the TMX import file. With the second boolean parameter you can determine whether any existing export file should be overwritten or not.
exporter.Export(exportFilePath, true);The full ExportTMXFile function looks as shown below:
public void ExportTMXFile(string tmPath, string exportFilePath) { #region "open" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationMemoryExporter 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 }
If you do not want to export the entire TM database content into a TMX file, but only a small part of it, you can run a filtered export. This allows you to export only a subset of the TM content, e.g. all translation units that have a Customer field with the value Microsoft, or all TUs that were created after 1st January 2010, etc. Running such an export is basically the same as the procedure described above. You just have to add a filter before the export is executed. Start by implementing another function called RunFilteredExport, which also takes the TM file name and path as well as the export file name and path as string parameters. First, open the TM and create a TM exporter object as shown below:
FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationMemoryExporter exporter = new TranslationMemoryExporter(tm.LanguageDirection);
exporter.FilterExpression = this.GetFilterSimple();public void RunFilteredExport(string tmPath, string exportFilePath) { #region "OpenForFilter" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationMemoryExporter 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 }
In this step, you will see how to implement the helper function that creates the filter for the export. Implement a new helper function called GetFilterSimple, which returns a FilterExpression object, i.e.:
private FilterExpression GetFilterSimple()PicklistItem fieldName = new PicklistItem("Customer"); MultiplePicklistFieldValue fieldValue = new MultiplePicklistFieldValue("Microsoft"); fieldValue.Add(fieldName);
AtomicExpression filter = new AtomicExpression(fieldValue, AtomicExpression.Operator.Equal); return filter;
Suppose you want to create a more advanced filter that has two (or more) criteria, e.g. export all TUs where the Customer field value equals MicrosoftOR in which the Project id text field contains the string 2010. Implement another helper function called GetFilterAdvanced, i.e.:
private FilterExpression GetFilterAdvanced()PicklistItem fieldName1 = new PicklistItem("Customer"); MultiplePicklistFieldValue fieldValue1 = new MultiplePicklistFieldValue("Microsoft"); fieldValue1.Add(fieldName1); AtomicExpression expression1 = new AtomicExpression(fieldValue1, AtomicExpression.Operator.Equal);
MultipleStringFieldValue fieldName2 = new MultipleStringFieldValue("Project id"); fieldName2.Add("2010"); AtomicExpression expression2 = new AtomicExpression(fieldName2, AtomicExpression.Operator.Contains);
ComposedExpression filter = new ComposedExpression(expression1, ComposedExpression.Operator.Or, expression2); return filter;
The complete class should now look as shown below:
namespace Sdl.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" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationMemoryExporter 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" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationMemoryExporter 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" PicklistItem fieldName = new PicklistItem("Customer"); MultiplePicklistFieldValue fieldValue = new MultiplePicklistFieldValue("Microsoft"); fieldValue.Add(fieldName); #endregion #region "SimpleFilter" AtomicExpression filter = new AtomicExpression(fieldValue, AtomicExpression.Operator.Equal); return filter; #endregion } #endregion #region "GetFilterAdvanced" private FilterExpression GetFilterAdvanced() { #region "AdvancedCriterion1" PicklistItem fieldName1 = new PicklistItem("Customer"); MultiplePicklistFieldValue fieldValue1 = new MultiplePicklistFieldValue("Microsoft"); fieldValue1.Add(fieldName1); AtomicExpression expression1 = new AtomicExpression(fieldValue1, AtomicExpression.Operator.Equal); #endregion #region "AdvancedCriterion2" MultipleStringFieldValue fieldName2 = new MultipleStringFieldValue("Project id"); fieldName2.Add("2010"); AtomicExpression expression2 = new AtomicExpression(fieldName2, AtomicExpression.Operator.Contains); #endregion #region "AdvancedFilter" ComposedExpression filter = new ComposedExpression(expression1, ComposedExpression.Operator.Or, expression2); return filter; #endregion } #endregion } }