Localize dynamic content items

To localize dynamic content items, you set the required culture by passing it as a parameter to the Lstring properties of the item.

In the following code example, you localize a dynamic content item that you created using the Dynamic Module Builder.

In the example below, you perform the following:

  1. Get an instance of the DynamicModuleManager class.
  2. Resolve the name of the dynamic content type using the TypeResolutionServiceclass.
  3. Check whether a dynamic content item with the same ID already exists.
    If an item with the same ID does not exist, create the dynamic content item by calling the CreateDataItem method of the DynamicModuleManager class.
  4. Set the properties of the dynamic content item object.
    Passing the culture when setting the Title ensures that the dynamic content item is localized for that specific culture.
  5. Recompile the URLs of the dynamic content item.
  6. Set the ApprovalWorkflowState of the desired localized version by passing the specific culture.
  7. Publish the dynamic content item to the Live state using the content lifecycle of the DynamicModuleManager class.
  8. Save the changes.
C#
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Security;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Utilities.TypeConverters;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Multilingual
{
    public partial class MultilingualSnippets
    {
        public static void CreateLocalizedDynamicContentItem(Guid itemId, string title, string description, string itemType, string targetCulture, string providerName)
        {
            CultureInfo culture = SystemManager.CurrentContext.AppSettings.GetCultureByName(targetCulture);

            DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(providerName);

            Type dynamicType = TypeResolutionService.ResolveType(itemType);

            DynamicContent dynamicItem = dynamicModuleManager.GetDataItems(dynamicType).FirstOrDefault(x => x.Id == itemId);

            if (dynamicItem == null)
            {
                dynamicItem = dynamicModuleManager.CreateDataItem(dynamicType, itemId, dynamicModuleManager.Provider.ApplicationName);
            }

            //Set the properties of the dynamic content item.
            dynamicItem.SetString("Title", title, culture);
            dynamicItem.SetString("Description", description, culture);
            dynamicItem.SetString("UrlName", Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"), culture);
            dynamicItem.SetValue("Owner", SecurityManager.GetCurrentUserId());
            dynamicItem.SetValue("PublicationDate", DateTime.Now);

            //Check for duplicate URLs
            dynamicModuleManager.RecompileDataItemsUrls(dynamicType);

            //Set the WorkflowStatus and Publish
            dynamicItem.ApprovalWorkflowState.SetString(culture, "Published");
            dynamicModuleManager.Lifecycle.Publish(dynamicItem, culture);

            //Save the changes.
            dynamicModuleManager.SaveChanges();
        }
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
New to Sitefinity?