Updating a Translation Memory |
In this chapter you will learn how to add content to a TM. While translators work they typically add translation units to the TM, or edit existing ones, e.g. by replacing past translations. In addition, updating a TM can also involve adding TM field information (e.g. project id, client) to the translation units. The following examples illustrate how to handle these common TM editing scenarios programmatically.
Start by adding a new class called TmUpdater to your project.
One of the most common use cases is, of course, to add new translation units (TUs) to a TM. This usually happens while translators are translating segment by segment. For the next step we suppose that you would like to add a simple TU with plain text segments. Moreover, we assume that you need to add a picklist field value, i.e. Customer = Microsoft (see Adding TM Fields).
Start by implementing a function called AddTu, which takes the TM file name and path as string parameter, and which can be called as shown in the example below:
TMUpdater update = new TMUpdater();
update.AddTu(_translationMemoryFilePath);
update.AddTuExtended(_translationMemoryFilePath);
update.EditTu(_translationMemoryFilePath);
update.DeleteTu(_translationMemoryFilePath);
FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationUnit tu = new TranslationUnit();
tu.SourceSegment = new Segment(tm.LanguageDirection.SourceLanguage); tu.TargetSegment = new Segment(tm.LanguageDirection.TargetLanguage); tu.SourceSegment.Add("A dialog box will open."); tu.TargetSegment.Add("Es öffnet sich ein Dialogfenster.");
Note |
---|
If the source/target segment text to add requires tags - e.g. for inline formatting -, you need to specify an element rather than a simple string. |
Let us assume that you would also like to add a picklist field value to the new TU. Start by creating the picklist field value object and add it to the TU object:
MultiplePicklistFieldValue value = new MultiplePicklistFieldValue("Customer"); value.Add("Microsoft"); tu.FieldValues.Add(value);
MultiplePicklistFieldValue value = new MultiplePicklistFieldValue("Customer"); value.Add("Microsoft"); tu.FieldValues.Add(value);
private ImportSettings GetImportSettings() { ImportSettings settings = new ImportSettings(); settings.CheckMatchingSublanguages = true; settings.ExistingFieldsUpdateMode = ImportSettings.FieldUpdateMode.Merge; return settings; }
Note that information such as the creation date and user is added automatically by the API, and does not have to be explicitly set by your application.
There are also other kinds of information that you may add to a TU. One example is the confirmation level. TUs may have confirmation levels such as draft, approved, rejected, etc., which indicate the quality and reliability of a translation. The confirmation level can be set through the TranslationUnitConfirmationLevel property. Below is an example of how to set the TU confirmation level to approved:
tu.ConfirmationLevel = ConfirmationLevel.ApprovedTranslation;
Another example of further information that you may add is the TranslationUnitFormat, which can be used to indicate whether a TU is in the format of SDL Trados Studio 2015, Translator's Workbench, TTX, etc. Below you see how the format is set to SDL Trados Studio 2015:
tu.Format = TranslationUnitFormat.SDLTradosStudio2009;
TUs may be created by filling a TM through interactive translation, from translation suggestions provided by a machine translation system, alignment of existing documents, etc. If you want to indicate the origin of a given TU, you can apply the TranslationUnitOrigin property. In the example below the TU origin is set to TM (i.e. generated by filling a translation memory through interactive translation):
tu.Origin = TranslationUnitOrigin.TM;
public void AddTu(string tmPath) { #region "open" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); TranslationUnit tu = new TranslationUnit(); #endregion #region "segments" tu.SourceSegment = new Segment(tm.LanguageDirection.SourceLanguage); tu.TargetSegment = new Segment(tm.LanguageDirection.TargetLanguage); tu.SourceSegment.Add("A dialog box will open."); tu.TargetSegment.Add("Es öffnet sich ein Dialogfenster."); #endregion #region "AddField" MultiplePicklistFieldValue value = new MultiplePicklistFieldValue("Customer"); value.Add("Microsoft"); tu.FieldValues.Add(value); #endregion #region "ConfirmationLevel" tu.ConfirmationLevel = ConfirmationLevel.ApprovedTranslation; #endregion #region "format" tu.Format = TranslationUnitFormat.SDLTradosStudio2009; #endregion #region "origin" tu.Origin = TranslationUnitOrigin.TM; #endregion #region "StuctureContext" tu.StructureContexts = new string[] { "H" }; #endregion #region "AddTu" tm.LanguageDirection.AddTranslationUnit(tu, this.GetImportSettings()); tm.Save(); MessageBox.Show("TU has been added successfully."); #endregion }
You can apply structure context information through the StructureContexts class as shown in the code example below:
tu.StructureContexts = new string[] { "H" };
TUs are often changed when the translator decides that an existing target segment of a suggested translation unit should be changed, e.g. because the translation that was previously entered is incorrect or outdated. In the following step you will learn how to programmatically handle cases in which the target segment of an existing TU should be replaced by a new translation.
Start by adding a function called EditTu, which takes the TM file name and path as string parameter, and which can be called as shown below:
TMUpdater update = new TMUpdater();
update.AddTu(_translationMemoryFilePath);
update.AddTuExtended(_translationMemoryFilePath);
update.EditTu(_translationMemoryFilePath);
update.DeleteTu(_translationMemoryFilePath);
private SearchSettings GetSearchSettings() { SearchSettings settings = new SearchSettings(); settings.MinScore = 100; return settings; }
public void EditTu(string tmPath) { FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); SearchResults results = tm.LanguageDirection.SearchText(this.GetSearchSettings(), "A dialog box will open."); foreach (SearchResult item in results) { if (item.ScoringResult.Match == 100) { item.MemoryTranslationUnit.TargetSegment.Clear(); item.MemoryTranslationUnit.TargetSegment.Add("Ein Dialogfeld wird geöffnet."); item.MemoryTranslationUnit.SystemFields.UseCount++; break; } } tm.Save(); }
Another use case is the deletion of a particular translation unit, for example, because it is outdated, incorrect, etc. Add a new function to your class, which you call DeleteTu. Here, we proceed similar to the editing of a TU (see section above), i.e. we first do an exact search for the source segment. We then retrieve the id of the TU that was found, and apply a delete action as shown in the following code example:
FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); SearchResults results = tm.LanguageDirection.SearchText(this.GetSearchSettings(), "A dialog box will open."); foreach (SearchResult item in results) { if (item.ScoringResult.Match == 100) { PersistentObjectToken tuId = item.MemoryTranslationUnit.ResourceId; tm.LanguageDirection.DeleteTranslationUnit(tuId); break; } }
Note |
---|
Each TU possesses a unique id by which it can be identified within the TM. |
It is also possible to 'wipe' the entire TM by applying the DeleteAllTranslationUnits, which will delete all translation units that are stored in the TM.
tm.LanguageDirection.DeleteAllTranslationUnits();
Note |
---|
Deleting all TUs in a TM is a potentially dangerous operation, which should only be performed in exceptional cases, as this batch deletion cannot be undone. |