Create a localized page
To create a localized page, you set the required culture by passing it as a parameter to the Lstring properties of the page.
In the code example below, you perform the following:
- Get an instance of the
PageManagerclass. - Check whether a parent page node is set.
If you do not specify the parent node, the page is set under the root node. - Check whether a page with the same ID already exists.
If a page with such ID does not exist, create the page by calling theCreatePagemethod of thePageManagerclass. - Get the page data by passing the culture to the
GetPageDatamethod of the page node. - Set the properties of the page data and page node objects. Passing the culture when setting the
Titleof the page node ensures that the page is localized for that specific culture. - Set the
ApprovalWorkflowStatetoPublishedby passing the culture for the desired localized version. - Save the changes.
C#
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Services;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Multilingual
{
public partial class MultilingualSnippets
{
public static void CreateLocalizedPage(Guid pageId, string pageName, Guid parentPageNodeId, string targetCulture)
{
CultureInfo culture = CultureInfo.GetCultureInfo(targetCulture);
PageManager manager = PageManager.GetManager();
PageData pageData = null;
PageNode pageNode = null;
// Get the parent node Id
if (parentPageNodeId == Guid.Empty)
{
parentPageNodeId = SiteInitializer.CurrentFrontendRootNodeId;
}
PageNode parent = manager.GetPageNode(parentPageNodeId);
// Check whether page exists
var initialPageNode = manager.GetPageNodes().Where(n => n.Id == pageId).FirstOrDefault();
if (initialPageNode == null)
{
// Create the page
pageNode = manager.CreatePage(parent, pageId, NodeType.Standard);
}
pageNode.Title.SetString(culture, pageName);
pageNode.Description.SetString(culture, pageName);
pageNode.Name = pageName;
pageNode.UrlName.SetString(culture, Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"));
pageData = pageNode.GetPageData();
pageData.HtmlTitle.SetString(culture, pageName);
pageData.Description.SetString(culture, pageName);
pageData.Keywords.SetString(culture, pageName);
pageNode.ApprovalWorkflowState.SetString(culture, "Published");
manager.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.