How to create multilingual pages programmatically?

Posted by Community Admin on 04-Aug-2018 15:27

How to create multilingual pages programmatically?

All Replies

Posted by Community Admin on 27-May-2011 00:00

Hi Sitefinity Guys

I have to generate some pages (like 5000) out of a sql database. To create a simple page is easy.

But in my case i have 5 Languages in the system and i want to generate the language versions of each page too.

But how? 

I tried some approaches but they all failed.

Can you give me a hint? or better an example code?

Regards :)

Dave

Posted by Community Admin on 30-May-2011 00:00

Hello Dave,

It depends on you want to have synced or split pages. For synced pages you should only change the CurrentUICulture and keep working the with the PageManager. This will create a page on the current language.

Here is the programmatic way

            var pageNode = pageManager.CreatePage(PageLocation.Frontend, Guid.Empty, NodeType.Standard);
            var page = pageNode.Page;
            page.LocalizationStrategy = Localization.LocalizationStrategy.Split;
            page.UiCulture = lang.Name;
            page.Culture = lang.Name;
            page.IsAutoCreated = true;
            pageNode.UrlName.SetString(lang, sourceNode.UrlName[CultureInfo.InvariantCulture]);

Regards,
Ivan Dimitrov
the Telerik team

Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 30-May-2011 00:00

Thanks for your reply.

I built your code in, but i get an Exception: 

You cannot save your changes, becase a page with such URL already exists. Change the page URL and try again


// Create a node in the SiteMap for that page.
           PageNode node = mMgr.CreatePageNode();
           mMgr.ChangeParent(node, pParentnode);
           pParentnode.Nodes.Add(node);
 
           // set the node properties
           node.Title = pTitle;
           node.ShowInNavigation = true;
           node.UrlName = pTitle;
 
           PageData pageData = mMgr.CreatePageData();
 
           var alltemplates = mMgr.GetTemplates();
 
           foreach (var template in alltemplates)
           
               if (template.Title.Equals(pTemplate))
               
                   pageData.Template = template;
 
               
           
 
           pageData.HtmlTitle = pTitle;
           pageData.Title = pTitle;
 
           pageData.LocalizationStrategy = LocalizationStrategy.Synced;
           pageData.Culture = "en";
           pageData.UiCulture = "en";
 
           // Set the page status as published. The default status is draft
           pageData.Status = ContentLifecycleStatus.Live;
 
 
           // associate the node with the PageData object
           node.Page = pageData;
 
           foreach (var lang in currentSettings.DefinedFrontendLanguages)
           
               if (!lang.TwoLetterISOLanguageName.ToLower().Equals("en"))
               
                   var pageNode = mMgr.CreatePage(PageLocation.Frontend, Guid.Empty, NodeType.Standard);
                   var page = pageNode.Page;
                   page.LocalizationStrategy = LocalizationStrategy.Split;
                   page.UiCulture = lang.Name;
                   page.Culture = lang.Name;
                   page.IsAutoCreated = true;
                   pageNode.UrlName.SetString(lang, node.UrlName[CultureInfo.InvariantCulture]);
               
           



Hmm well the Exception makes sense, but what is the right way?

Thanks in advance :)

Dave

Posted by Community Admin on 30-May-2011 00:00

Hi,

I try this :

// Create a node in the SiteMap for that page.
PageNode node = mMgr.CreatePageNode();
mMgr.InitializePageLocalizationStrategy(node , Telerik.Sitefinity.Localization.LocalizationStrategy.Synced, true);
 
Don't create a PageData manually :

PageDraft pDraft = mMgr.EditPage(node.Page.Id, false);
PageData pData = pDraft.ParentPage;

For your error :
node.UrlName["en"] = new Lstring(pageNameEn);
node.UrlName["fr"] = new Lstring(pageNameFr);

Regards,
Nicolas

This thread is closed