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:
- Get an instance of the
DynamicModuleManagerclass. - Resolve the name of the dynamic content type using the
TypeResolutionServiceclass. - 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 theCreateDataItemmethod of theDynamicModuleManagerclass. - Set the properties of the dynamic content item object.
Passing the culture when setting theTitleensures that the dynamic content item is localized for that specific culture. - Recompile the URLs of the dynamic content item.
- Set the
ApprovalWorkflowStateof the desired localized version by passing the specific culture. - Publish the dynamic content item to the Live state using the content lifecycle of the
DynamicModuleManagerclass. - 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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.