Localize content items
To localize content items, you set the required culture by passing it as a parameter to the Lstring properties of the item. The following code example demonstrates how to localize a News item.
NOTE: The
IDargument is assigned to the master version of the News item. For more information about the different version of a content item, see For developers: Content lifecycle.
In the example below, you perform the following:
- Get an instance of the
NewsManagerclass. - Check whether a News item with the same ID already exists.
If a News item with the same ID does not exist, create the item by calling theCreateNewsItemmethod of theNewsManagerclass.
You can create a News item with either predefined or auto-generated ID, depending on which overload of the method you use. TheIDargument is assigned to theIDproperty of the master version of the item. - Set the properties of the News item object.
To ensure that the News item is localized for a specific culture, create a newusingscope with theCultureRegionclass and set theTitleinside. - Recompile and validate the URLs of the News item.
- Set the
ApprovalWorkflowStatetoPublishedby passing the culture for the required localized version. - Publish the News item to a Live state using the content lifecycle of the
NewsManagerclass. - Save the changes.
C#
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
namespace SitefinityWebApp
{
public partial class MultilingualSnippets
{
public static void CreateLocalizedNewsItem(Guid masterNewsId, string newsTitle, string newsContent, string targetCulture)
{
CultureInfo culture = SystemManager.CurrentContext.AppSettings.GetCultureByName(targetCulture);
using (new CultureRegion(culture))
{
NewsManager newsManager = NewsManager.GetManager();
NewsItem newsItem = newsManager.GetNewsItems().Where(item => item.Id == masterNewsId).FirstOrDefault();
if (newsItem == null)
{
//The news item is created as a master. The newsId is assigned to the master.
newsItem = newsManager.CreateNewsItem(masterNewsId);
newsItem.DateCreated = DateTime.UtcNow;
}
//Set the properties of the news item.
newsItem.Title = newsTitle;
newsItem.GetString("Content").SetString(culture, newsContent);
newsItem.Summary = newsContent;
newsItem.UrlName = Regex.Replace(newsTitle.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
//Check for duplicate URLs
newsManager.RecompileAndValidateUrls(newsItem);
//Set the WorkflowStatus and Publish
newsItem.ApprovalWorkflowState.SetString(culture, "Published");
newsManager.Lifecycle.Publish(newsItem, culture);
//Save the changes.
newsManager.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.