Add a translation to a localized page

To add a translation for other languages to a page, you choose one of the two types of localization strategies:

  • Synced
    All languages share a single page object and localization is done at LString property level. Thus, every new translation of your page has the same page widgets and layout. If you change the page widgets and layout of any of the translated pages, these changes are propagated to the other language versions as well.
  • Split
    Have a completely different page object for each language. Thus, when you add a translation to your page, you can choose whether to copy the page widgets and layout from other translated page or create your page layout from scratch.

Add a translation using Synced localization strategy

  1. Get an instance of the PageManagerclass.
  2. Find the page node using the page node ID.
    If a page node with the same ID does not exist, discontinue the execution of the method.
  3. Set localized values for the desired PageNode properties.
  4. Set the localization strategy to Synced by calling the InitializePageLocalizationStrategy method of the page manager.
    NOTE : You set the localization strategy only once when adding a translation for the first time.
  5. Get the page data by calling the GetPageData method of the page node.
  6. Set localized values for the desired PageData properties.
  7. Set the ApprovalWorkflowState to Published by passing the culture for the desired localized version.
  8. Save the changes.
C#
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Localization;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;

namespace SitefinityWebApp
{
    public partial class CopyPageDataToAnotherLanguage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sourcePageTitle = "Translate test";
            string titleOfNewTranslatedPage = "Translate test SV";
            string targetCulture = "sv";

            TranslateSyncedPage(sourcePageTitle, targetCulture, titleOfNewTranslatedPage);
        }

        public static void TranslateSyncedPage(string titleOfMainPage, string targetCulture, string titleOftranslatedPage)
        {
            // Get the PageNode
            PageManager manager = PageManager.GetManager();
            PageNode pageNode = manager.GetPageNodes().FirstOrDefault(x => x.Title == titleOfMainPage);

            if (pageNode == null)
            {
                return; // PageNode not found
            }

            CultureInfo targetCultureInfo = CultureInfo.GetCultureInfo(targetCulture);

            // Set values for PageNode properties
            pageNode.Title.SetString(targetCultureInfo, titleOftranslatedPage);
            pageNode.Description.SetString(targetCultureInfo, titleOftranslatedPage);
            pageNode.UrlName.SetString(targetCultureInfo, Regex.Replace(titleOftranslatedPage.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"));

            if (pageNode.LocalizationStrategy != LocalizationStrategy.Synced)
            {
                // Set the pageNode LocalizationStrategy for the first method call
                manager.InitializePageLocalizationStrategy(pageNode, LocalizationStrategy.Synced, false);
            }

            PageData pageData = pageNode.GetPageData();

            // Set values for PageData properties 
            pageData.HtmlTitle.SetString(targetCultureInfo, titleOftranslatedPage);
            pageData.Description.SetString(targetCultureInfo, titleOftranslatedPage);
            pageData.Keywords.SetString(targetCultureInfo, titleOftranslatedPage);

            // Publish the page
            pageNode.ApprovalWorkflowState.SetString(targetCultureInfo, "Published");
            manager.SaveChanges();

            var pageDataId = manager.GetPageNode(pageNode.Id).PageId;
            var page = manager.EditPage(pageDataId, targetCultureInfo);

            //publishes draft page
            manager.PublishPageDraft(page, targetCultureInfo);
            manager.SaveChanges();
        }
    }
}

Add a translation using Split localization strategy

  1. Get an instance of the PageManager class.
  2. Find the page node using the page node ID.
    If a page node with the same ID does not exist, discontinue the execution of the method.
  3. Set localized values for the desired PageNode properties.
  4. Set the localization strategy to Splitby calling the InitializePageLocalizationStrategy method of the page manager.
    NOTE : You set the localization strategy only once when adding a translation for the first time. For subsequent translations, you must create an empty page data object and set values for the following properties:
    • Culture
    • NavigationNode
  5. (Optional) To copy the page content from another translation, call the InitializeSplitPagemethod of the page manager and pass the following parameters:
    • Page node - the node from which you copy page content
    • Source culture - the culture from which you copy content
    • Target culture - the culture to which you copy content
  6. Get the page data by calling the GetPageData method of the page manager and pass the target culture.
  7. Set localized values for the PageData properties.
  8. Set the ApprovalWorkflowState to Published by passing the culture for the desired localized version.
  9. Save the changes.
C#
using System;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Telerik.Sitefinity.Localization;
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 TranslateSplitPage(Guid pageNodeId, string targetCulture, string pageName, bool copyContents, string sourceCulture)
        {
            // Get the PageNode
            PageManager manager = PageManager.GetManager();
            PageNode pageNode = manager.GetPageNodes().FirstOrDefault(x => x.Id == pageNodeId);

            if (pageNode == null)
            {
                return; // PageNode not found
            }

            CultureInfo targetCultureInfo = SystemManager.CurrentContext.AppSettings.GetCultureByName(targetCulture);

            // Set values for PageNode properties
            pageNode.Title.SetString(targetCultureInfo, pageName);
            pageNode.Description.SetString(targetCultureInfo, pageName);
            pageNode.UrlName.SetString(targetCultureInfo, Regex.Replace(pageName.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-"));

            PageData pageData = null;

            if (pageNode.LocalizationStrategy != LocalizationStrategy.Split)
            {
                // Set the pageNode LocalizationStrategy for the first method call
                manager.InitializePageLocalizationStrategy(pageNode, LocalizationStrategy.Split, false);
            }
            else
            {
                // For subsequent method calls create an empty page data 
                pageData = manager.CreatePageData();
                pageData.Culture = targetCultureInfo.Name;
                pageData.NavigationNode = pageNode;
            }

            if (copyContents)
            {
                // Copy page contents from source culture
                CultureInfo sourceCultureInfo = SystemManager.CurrentContext.AppSettings.GetCultureByName(sourceCulture);
                manager.InitializeSplitPage(pageNode, sourceCultureInfo, targetCultureInfo);
            }

            // Retreive the page data of the page node
            pageData = pageNode.GetPageData(targetCultureInfo);

            // Set values for PageData properties
            pageData.HtmlTitle.SetString(targetCultureInfo, pageName);
            pageData.Description.SetString(targetCultureInfo, pageName);
            pageData.Keywords.SetString(targetCultureInfo, pageName);

            // Publish draft page
            var page = manager.EditPage(pageData.Id, targetCultureInfo);
            manager.PublishPageDraft(page, targetCultureInfo);
            // Publish the page 
            pageNode.ApprovalWorkflowState.SetString(targetCultureInfo, "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.