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 atLStringproperty 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
- Get an instance of the
PageManagerclass. - Find the page node using the page node
ID.
If a page node with the sameIDdoes not exist, discontinue the execution of the method. - Set localized values for the desired
PageNodeproperties. - Set the localization strategy to
Syncedby calling theInitializePageLocalizationStrategymethod of the page manager.
NOTE : You set the localization strategy only once when adding a translation for the first time. - Get the page data by calling the
GetPageDatamethod of the page node. - Set localized values for the desired
PageDataproperties. - 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.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
- Get an instance of the
PageManagerclass. - Find the page node using the page node
ID.
If a page node with the sameIDdoes not exist, discontinue the execution of the method. - Set localized values for the desired
PageNodeproperties. - Set the localization strategy to
Splitby calling theInitializePageLocalizationStrategymethod 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:CultureNavigationNode
- (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 contentSource culture- the culture from which you copy contentTarget culture- the culture to which you copy content
- Get the page data by calling the
GetPageDatamethod of the page manager and pass the target culture. - Set localized values for the
PageDataproperties. - 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.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.
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.