| Looping through Translation Memories |
Iterating through all the translation units in a TM can be useful to perform various maintenance-related tasks. Suppose you need to batch-delete a number of TUs, e.g. all TUs that do belong to a certain customer, or all TUs that are older than two years, as they can be considered outdated. In this case, you can loop through the TUs and apply a delete action to each TU that fulfills (or does not fulfill) a specific criteria. In this chapter we would like to demonstrate how to programmatically handle such a use case by deleting all TUs that belong to the client Microsoft, i.e. where the TM field Customer has the value Microsoft.
Start by adding new class called TmIterator to your project. Implement a public function called Iterate, which takes the TM file name and path as string parameter. For this sample scenario, let us assume that the aim is to iterate through a given TM and output the source segments of each translation unit that has the field value Customer -> Microsoft.
Start by opening the TM as shown below:
FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath);RegularIterator tmIterator = new RegularIterator(1);
TranslationUnit[] Tus = tm.LanguageDirection.GetTranslationUnits(ref tmIterator);while (Tus.Count<TranslationUnit>() > 0)
foreach (TranslationUnit Tu in Tus)
foreach (FieldValue item in Tu.FieldValues)
if (item.Name == "Customer")
if (item.ToString() == "Microsoft") { MessageBox.Show(Tu.SourceSegment.ToPlain()); }
Tus = tm.LanguageDirection.GetTranslationUnits(ref tmIterator);#region "while" while (Tus.Count<TranslationUnit>() > 0) #endregion { #region "LoopTus" foreach (TranslationUnit Tu in Tus) #endregion { #region "LoopValues" foreach (FieldValue item in Tu.FieldValues) #endregion { #region "DetermineFieldName" if (item.Name == "Customer") #endregion { #region "DetermineFieldValue" if (item.ToString() == "Microsoft") { MessageBox.Show(Tu.SourceSegment.ToPlain()); } #endregion } } } #region "update" Tus = tm.LanguageDirection.GetTranslationUnits(ref tmIterator); #endregion }
Your complete class should now look as shown below:
namespace Sdl.SDK.LanguagePlatform.Samples.TmAutomation { using System.Linq; using System.Windows.Forms; using Sdl.LanguagePlatform.TranslationMemory; using Sdl.LanguagePlatform.TranslationMemoryApi; public class TMIterator { public void Iterate(string tmPath) { #region "open" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); #endregion #region "iterator" RegularIterator tmIterator = new RegularIterator(1); #endregion #region "GetTUs" TranslationUnit[] Tus = tm.LanguageDirection.GetTranslationUnits(ref tmIterator); #endregion #region "loop" #region "while" while (Tus.Count<TranslationUnit>() > 0) #endregion { #region "LoopTus" foreach (TranslationUnit Tu in Tus) #endregion { #region "LoopValues" foreach (FieldValue item in Tu.FieldValues) #endregion { #region "DetermineFieldName" if (item.Name == "Customer") #endregion { #region "DetermineFieldValue" if (item.ToString() == "Microsoft") { MessageBox.Show(Tu.SourceSegment.ToPlain()); } #endregion } } } #region "update" Tus = tm.LanguageDirection.GetTranslationUnits(ref tmIterator); #endregion } #endregion } } }