| TM Fields Templates |
Server translation memories can use field templates (which are not available for file TMs). Image that you need to use the same fields (e.g. Customer and Project id) for a large number of TMs. Rather than adding the fields to the setup of each TM, you just define the fields once in a template, which then gets applied to the individual TMs. Moreover, when you change the template to contain a new field (e.g. Domain), this new field will then be instantly made available in all TMs that are based on this particular template.
On this page you will learn how to retrieve available field templates and how to create a template.
Start by adding a new class called ServerFieldTemplates to your project. This class should contain the functionality to retrieve existing field templates and create new templates.
Our first task will be to generate a list of all field templates that are hosted on the TM Server. To do this implement a function called GetFieldTemplates, which takes the translation provider server as parameter. First, add a string variable that will hold the field templates list. Add the templates information to this variable by looping through all field templates that are hosted on the server:
StringBuilder templateList = new StringBuilder(); foreach (ServerBasedFieldsTemplate template in tmServer.GetFieldsTemplates(FieldsTemplateProperties.All))
templateList.AppendFormat("Template name: {0}\n", template.Name); templateList.AppendFormat("Template description: {0}\n", template.Description);
templateList.AppendLine("Fields: "); foreach (FieldDefinition def in template.FieldDefinitions) { templateList.AppendFormat("{0}({1})", def.Name, def.ValueType.ToString()); } templateList.AppendLine(string.Empty);
public void GetFieldTemplates(TranslationProviderServer tmServer) { #region "LoopTemplates" StringBuilder templateList = new StringBuilder(); foreach (ServerBasedFieldsTemplate template in tmServer.GetFieldsTemplates(FieldsTemplateProperties.All)) #endregion { #region "GeneralTemplateInfo" templateList.AppendFormat("Template name: {0}\n", template.Name); templateList.AppendFormat("Template description: {0}\n", template.Description); #endregion #region "fields" templateList.AppendLine("Fields: "); foreach (FieldDefinition def in template.FieldDefinitions) { templateList.AppendFormat("{0}({1})", def.Name, def.ValueType.ToString()); } templateList.AppendLine(string.Empty); #endregion } MessageBox.Show(templateList.ToString(), "Template List"); }
When creating server TMs you can specify a field template. The TM will then 'inherit' the fields defined in the template. Imagine that you want to know which server TMs use a particular template. To do this, add a function called GetTmsForTemplate, which takes the translation provider server and the fields template name as parameters. First, retrieve the fields template that you want to select. The template can be referenced using the template name:
ServerBasedFieldsTemplate template = tmServer.GetFieldsTemplate(templateName, FieldsTemplateProperties.All);
StringBuilder tmList = new StringBuilder(); foreach (ServerBasedTranslationMemory tm in template.TranslationMemories) { tmList.AppendLine(tm.Name); } MessageBox.Show(tmList.ToString());
public void GetTmsForTemplate(TranslationProviderServer tmServer, string templateName) { #region "GetTemplate" ServerBasedFieldsTemplate template = tmServer.GetFieldsTemplate(templateName, FieldsTemplateProperties.All); #endregion #region "TmLoop" StringBuilder tmList = new StringBuilder(); foreach (ServerBasedTranslationMemory tm in template.TranslationMemories) { tmList.AppendLine(tm.Name); } MessageBox.Show(tmList.ToString()); #endregion }
In this step you will learn how to create a new server-based fields template. Imagine that you need to create a template that contains a text field called Project id and a list field called Client. Implement a function called CreateTemplate, which takes the translation provider server as parameter. Next, create a template object for the given server as shown below. Here, you also specify the general template information, i.e. the name and the (optional) description.
ServerBasedFieldsTemplate template = new ServerBasedFieldsTemplate(tmServer); template.Name = "Sample Template"; template.Description = "Fields template created by API";
FieldDefinition projField = new FieldDefinition("Project id", FieldValueType.MultipleString);
FieldDefinition clientField = new FieldDefinition("Client", FieldValueType.MultiplePicklist); clientField.PicklistItems.Add("Microsoft"); clientField.PicklistItems.Add("SAP");
template.FieldDefinitions.Add(projField); template.FieldDefinitions.Add(clientField);
template.Save();
MessageBox.Show("Unsaved changes? " + template.IsDirty.ToString());template.Delete();
public void CreateTemplate(TranslationProviderServer tmServer) { #region "CreateTemplate" ServerBasedFieldsTemplate template = new ServerBasedFieldsTemplate(tmServer); template.Name = "Sample Template"; template.Description = "Fields template created by API"; #endregion #region "AddTextField" FieldDefinition projField = new FieldDefinition("Project id", FieldValueType.MultipleString); #endregion #region "AddListField" FieldDefinition clientField = new FieldDefinition("Client", FieldValueType.MultiplePicklist); clientField.PicklistItems.Add("Microsoft"); clientField.PicklistItems.Add("SAP"); #endregion #region "AddFieldsToTemplate" template.FieldDefinitions.Add(projField); template.FieldDefinitions.Add(clientField); #endregion #region "save" template.Save(); MessageBox.Show("Unsaved changes? " + template.IsDirty.ToString()); #endregion #region "delete" template.Delete(); #endregion }
The complete class should look as shown below:
namespace Sdl.SDK.LanguagePlatform.Samples.TmAutomation { using System.Text; using System.Windows.Forms; using Sdl.LanguagePlatform.TranslationMemory; using Sdl.LanguagePlatform.TranslationMemoryApi; public class ServerFieldTemplates { #region "get" public void GetFieldTemplates(TranslationProviderServer tmServer) { #region "LoopTemplates" StringBuilder templateList = new StringBuilder(); foreach (ServerBasedFieldsTemplate template in tmServer.GetFieldsTemplates(FieldsTemplateProperties.All)) #endregion { #region "GeneralTemplateInfo" templateList.AppendFormat("Template name: {0}\n", template.Name); templateList.AppendFormat("Template description: {0}\n", template.Description); #endregion #region "fields" templateList.AppendLine("Fields: "); foreach (FieldDefinition def in template.FieldDefinitions) { templateList.AppendFormat("{0}({1})", def.Name, def.ValueType.ToString()); } templateList.AppendLine(string.Empty); #endregion } MessageBox.Show(templateList.ToString(), "Template List"); } #endregion #region "GetTms" public void GetTmsForTemplate(TranslationProviderServer tmServer, string templateName) { #region "GetTemplate" ServerBasedFieldsTemplate template = tmServer.GetFieldsTemplate(templateName, FieldsTemplateProperties.All); #endregion #region "TmLoop" StringBuilder tmList = new StringBuilder(); foreach (ServerBasedTranslationMemory tm in template.TranslationMemories) { tmList.AppendLine(tm.Name); } MessageBox.Show(tmList.ToString()); #endregion } #endregion #region "create" public void CreateTemplate(TranslationProviderServer tmServer) { #region "CreateTemplate" ServerBasedFieldsTemplate template = new ServerBasedFieldsTemplate(tmServer); template.Name = "Sample Template"; template.Description = "Fields template created by API"; #endregion #region "AddTextField" FieldDefinition projField = new FieldDefinition("Project id", FieldValueType.MultipleString); #endregion #region "AddListField" FieldDefinition clientField = new FieldDefinition("Client", FieldValueType.MultiplePicklist); clientField.PicklistItems.Add("Microsoft"); clientField.PicklistItems.Add("SAP"); #endregion #region "AddFieldsToTemplate" template.FieldDefinitions.Add(projField); template.FieldDefinitions.Add(clientField); #endregion #region "save" template.Save(); MessageBox.Show("Unsaved changes? " + template.IsDirty.ToString()); #endregion #region "delete" template.Delete(); #endregion } #endregion } }