Kentico CMS 7.0 Developer's Guide

Translation handlers

Translation handlers

Previous topic Next topic Mail us feedback on this topic!  

Translation handlers

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

When transferring data to a different instance of Kentico — via import/export or staging — IDs of the same objects can differ after the transfer. Kentico handles the situation for system objects, but not for custom objects. For example, when you stage a document type or a custom table that has a field containing object IDs, then the IDs are not guaranteed to stay the same once staged to the target server.

 

You can use the following two handlers to make sure the IDs are translated correctly:

 

RegisterRecords - use this handler to provide additional information for your custom field translation.

TranslateColumns - use this handler to translate the field value using the provided information.

 

Example

 

You have a custom table that contains a UserID field. The field contains the ID of a user to whom the item belongs. Because the UserID field is a custom field, you want to ensure its correct translation during import/export and staging.

 

1. See how you can register the handlers.

 

2. Use the following code to ensure the correct translation of the UserID field.

 

[C#]

 

using System;

 
using CMS.SettingsProvider;
using CMS.GlobalHelper;
using CMS.SiteProvider;

 
/// <summary>
/// Ensure translation of UserID column in a custom table item
/// </summary>
[CMSModuleLoaderAttribute]
public partial class CMSModuleLoader
{

  private class CMSModuleLoaderAttribute : CMSLoaderAttribute

   {

      public override void Init()
       {

          // Register the events
          ColumnsTranslationEvents.RegisterRecords.Execute += new EventHandler<ColumnsTranslationEventArgs>(RegisterRecords_Execute);
          ColumnsTranslationEvents.TranslateColumns.Execute += new EventHandler<ColumnsTranslationEventArgs>(TranslateColumns_Execute);
       }

 
      void RegisterRecords_Execute(object sender, ColumnsTranslationEventArgs e)
       {

          if (e.ObjectType == CustomTableItemProvider.GetObjectType("customtable.sampletable"))
           {
              // Register the object type, user ID, and username for custom field translation
              int userId = ValidationHelper.GetInteger(e.Data.GetValue("UserID"), 0);
               e.TranslationHelper.RegisterRecord(PredefinedObjectType.USER, userId, UserInfoProvider.GetUserNameById(userId), 0);
           }
       }

 
      void TranslateColumns_Execute(object sender, ColumnsTranslationEventArgs e)
       {

          if (e.ObjectType == CustomTableItemProvider.GetObjectType("customtable.sampletable"))
           {
              // Translate the field using the column name, User ID, and object type of user
              e.TranslationHelper.TranslateColumn(e.Data, "UserID", PredefinedObjectType.USER, 0, true, true);
           }
       }
   }
}