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:

  1. Get an instance of the PageManager class.
  2. Check whether a parent page node is set.
    If you do not specify the parent node, the page is set under the root node.
  3. Check whether a page with the same ID already exists. 
    If a page with such ID does not exist, create the page by calling the CreatePage method of the PageManager class.
  4. Get the page data by passing the culture to the GetPageData method of the page node.
  5. Set the properties of the page data and page node objects. Passing the culture when setting the Title  of the page node ensures that the page is localized for that specific culture.
  6. Set the ApprovalWorkflowState  to Published by passing the culture for the desired localized version.
  7. 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.
New to Sitefinity?