Adding TM Fields |
This page shows how to add fields to a translation memory. For more information on what TM fields are and what their purpose is, please refer to Configuring Translation Memories.
As we are going to implement the functionality for defining TM fields in a separate class, create a new class called TmFieldGenerator. Let us assume that you need to add a picklist field called Customer and a free text field called Project id. Picklist fields are always associated with a pre-defined list of values, which can later be selected as required at runtime. Text fields, on the other hand, can be filled with any text. Apart from picklist and text, fields can also be of the types number and date/time. However, since picklist and text fields constitute the most common types, we fill focus on those two.
The first step is decide whether you want to configure a field as a free text field or a picklist field. Fields that should be associated with only a limited number of values that should be used consistently, will usually be of the type picklist. Common examples are Subject, Document type, Customer, etc. Free text fields should be used when at runtime there are virtually limitless possible values. Common examples here are Project id, Comment, etc.
Start by adding a public function called AddFields, which takes the file path and name as string parameter. This function can be called as shown below:
TMFieldGenerator objFieldGenerator = new TMFieldGenerator();
objFieldGenerator.AddFields(_translationMemoryFilePath);
Apply the ValueType property to define the field as a picklist that can hold multiple values. Finally, add two values, e.g. Microsoft and SDL:
FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); FieldDefinition listField = new FieldDefinition(); listField.Name = "Customer"; listField.ValueType = FieldValueType.MultiplePicklist; listField.PicklistItems.Add("SDL"); listField.PicklistItems.Add("Microsoft");
The second example should be a free text field, which we call Project id. Therefore, create a free text field object:
FieldDefinition textField = new FieldDefinition(); textField.Name = "Project id"; textField.ValueType = FieldValueType.MultipleString;
namespace Sdl.SDK.LanguagePlatform.Samples.TmAutomation { using Sdl.LanguagePlatform.TranslationMemory; using Sdl.LanguagePlatform.TranslationMemoryApi; public class TMFieldGenerator { #region "AddFields" public void AddFields(string tmPath) { #region "listField" FileBasedTranslationMemory tm = new FileBasedTranslationMemory(tmPath); FieldDefinition listField = new FieldDefinition(); listField.Name = "Customer"; listField.ValueType = FieldValueType.MultiplePicklist; listField.PicklistItems.Add("SDL"); listField.PicklistItems.Add("Microsoft"); #endregion #region "textField" FieldDefinition textField = new FieldDefinition(); textField.Name = "Project id"; textField.ValueType = FieldValueType.MultipleString; #endregion #region "add" tm.FieldDefinitions.Add(listField); tm.FieldDefinitions.Add(textField); #endregion tm.Save(); } #endregion } }