How to create a new language page for an existing item?
Hello,
I need to know how to create a new language split page for an existing Page Node. I have tried so many varations, that I have actually corrupted some of my existing page nodes. Can somebody post some example code showing how to accomplish this? In 3.x there was a method called CopyNewLanguage or CreateNewLanguage in a Teleric CMS library that doesn't seem to exist in 4.x. This is my last attempt (which caused badness to my database):
var mgr = App.WorkWith().Page(pageNode.Id);
PageInfo info = pageInfo;
mgr.Do(
p=>
if (p.Page != null)
var pageData = new PageData Id = Guid.NewGuid();
mgr.PageManager.CopyPageData(p.Page, pageData, CultureInfo.GetCultureInfo("en"), updatedLanguage, true);
pageData.ApplicationName = p.Page.ApplicationName;
pageData.LocalizationStrategy = LocalizationStrategy.Split;
pageData.UiCulture = updatedLanguage.Name;
pageData.Culture = updatedLanguage.Name;
pageData.HtmlTitle = info.Title;
p.Page.LocalizationStrategy = LocalizationStrategy.Split;
if (p.Page.PageLanguageLink == null)
p.Page.PageLanguageLink = new PageLanguageLink();
//p.Page.PageLanguageLink.LinkId = Guid.NewGuid();
p.Page.PageLanguageLink.LanguageLinks.Add(p.Page);
p.Page.PageLanguageLink.LanguageLinks.Add(pageData);
).SaveChanges();
Regards,
Ray Simpson
Any help on this issue?
Hello Ray,
Please find below a sample method that will check whether such node already exists and if not will create it with the desired localization strategy (Split) and in the culture you have passed as argument:
public
static
bool
CreateLocalizedPage(Guid pageId,
string
pageName, Guid parentPageId,
bool
isHomePage,
bool
showInNavigation,
string
culture =
"en"
)
//The CurrentUICulture must be set to the desired culture for the page while translating it.
//At the end of the method the CurrentUICulture is restored to its original value.
var cultureInfo =
new
CultureInfo(culture);
var currentCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
var pageDataId = Guid.NewGuid();
PageManager manager = PageManager.GetManager();
PageData pageData =
null
;
PageNode pageNode =
null
;
var result =
false
;
var initialPageNode = manager.GetPageNodes().Where(n => n.Id == pageId).SingleOrDefault();
if
(initialPageNode !=
null
&& LanguageExistsForPage(initialPageNode.Page, cultureInfo))
return
result;
result =
true
;
if
(initialPageNode ==
null
)
var parentId = parentPageId;
if
(parentId == Guid.Empty)
parentId = SiteInitializer.FrontendRootNodeId;
//Create Page
PageNode parent = manager.GetPageNode(parentId);
pageNode = manager.CreatePage(parent, pageId, NodeType.Standard);
pageData = manager.CreatePageData(pageDataId);
pageData.Culture = Thread.CurrentThread.CurrentCulture.ToString();
pageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
pageNode.Page = pageData;
pageNode.Name = pageName;
pageNode.ShowInNavigation =
true
;
pageNode.DateCreated = DateTime.UtcNow;
pageNode.LastModified = DateTime.UtcNow;
else
//TranslatePage
manager.InitializePageLocalizationStrategy(initialPageNode, LocalizationStrategy.Split,
false
);
pageNode = GetPageNodeForLanguage(initialPageNode.Page, cultureInfo);
pageData = pageNode.Page;
pageData.TranslationInitialized =
true
;
pageData.IsAutoCreated =
false
;
pageNode.UrlName[cultureInfo] = Regex.Replace(pageName.ToLower(), UrlNameCharsToReplace, UrlNameReplaceString);
pageNode.Description[cultureInfo] = pageName;
pageNode.Title[cultureInfo] = pageName;
pageNode.ShowInNavigation = showInNavigation;
pageData.HtmlTitle[cultureInfo] = pageName;
pageData.Title[cultureInfo] = pageName;
pageData.Description[cultureInfo] = pageName;
manager.SaveChanges();
if
(isHomePage)
App.WorkWith().Page(pageId).SetAsHomePage().SaveChanges();
Thread.CurrentThread.CurrentUICulture = currentCulture;
return
result;
Thank you for responding Boyan. The code sample you pasted has several methods and varibles that do not exist (that I can find). They are:
GetPageNodeForLanguage
LanguageExistsForPage (which I changed to initialPageNode.Page.PageLanguageLink.GetPageForLanguage(cultureInfo))
And The vars:
UrlNameCharsToReplace
UrlNameReplaceString
I am going to try and implement what you sent, but I have a question about a section of code you have:
manager.InitializePageLocalizationStrategy(initialPageNode, LocalizationStrategy.Split,
false
);
pageNode = GetPageNodeForLanguage(initialPageNode.Page, cultureInfo);
This appears to be getting the pagedata in the language that I am trying to create, before I create it? If so, I don't understand how I will get that data before its created since I need to create the new page data, unless the method GetPageNodeForLanguage returns the blank page data.. ?
Regards,
Ray Simpson
Boyan,
This is what I have come up with, and it works. Any improvements would be nice, I also post this for anybody that will search for it the way I did :)
UPDATE: Forgot this line of code, it goes on the very bottom (sets the current thread back)
Thread.CurrentThread.CurrentUICulture = currentCulture;
01.
var currentCulture = Thread.CurrentThread.CurrentUICulture;
02.
Thread.CurrentThread.CurrentUICulture = updatedLanguage;
03.
manager.InitializePageLocalizationStrategy(pageNode, LocalizationStrategy.Split,
false
);
04.
pageNode =
05.
pageNode.Page.PageLanguageLink.LanguageLinks.FirstOrDefault(
06.
pd => pd.UiCulture == updatedLanguage.Name).NavigationNode;
07.
08.
var newPageData = pageNode.Page;
09.
newPageData.TranslationInitialized =
true
;
10.
newPageData.IsAutoCreated =
false
;
11.
12.
pageNode.UrlName[updatedLanguage] = updatedLanguage.TwoLetterISOLanguageName +
"_"
+
13.
pageInfo.PageName;
14.
pageNode.Description[updatedLanguage] = pageInfo.Description;
15.
pageNode.Title[updatedLanguage] = pageInfo.Title;
16.
pageNode.ShowInNavigation =
true
;
17.
18.
newPageData.HtmlTitle[updatedLanguage] = pageInfo.Title;
19.
newPageData.Title[updatedLanguage] = pageInfo.Title;
20.
newPageData.Description[updatedLanguage] = pageInfo.Description;
21.
22.
manager.SaveChanges();
Hi Ray Simpson
,
Thank you very much for sharing with us your solution on the issue, we really appreciate you sharing this with the rest of the community. Your Telerik points have been updated accordingly, once again thank you for your active involvement!
Regards,
Boyan Barnev
the Telerik team
So I have run into another problem with the translation item that I didn't account for until I tried to add a new language to the system and then tried to generate the new language pages.
So my question for you is, if the page already exists, and has the InitializePageLocalizationStrategy already called, how do we add a new language data? Your example only shows for a null page and one that needs to be split as it where. I believe it I am close with with this.. but I corrupted all of my page data (again) with experiments and am trying to weed it out of the database by hand:
var newPageData = manager.CreatePageData(Guid.NewGuid());
var currentCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = updatedLanguage;
newPageData.TranslationInitialized = true;
newPageData.IsAutoCreated = false;
newPageData.Culture = Thread.CurrentThread.CurrentUICulture.ToString();
newPageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
pageNode.UrlName[updatedLanguage] = pageInfo.PageName + "_" + updatedLanguage;
pageNode.Description[updatedLanguage] = pageInfo.Description;
pageNode.Title[updatedLanguage] = pageInfo.Title;
pageNode.ShowInNavigation = true;
newPageData.HtmlTitle[updatedLanguage] = pageInfo.HtmlTitle;
newPageData.Title[updatedLanguage] = pageInfo.Title;
newPageData.Description[updatedLanguage] = pageInfo.Description;
newPageData.Keywords[updatedLanguage] = pageInfo.Keywords;
//pageNode.Page.PageLanguageLink.LanguageLinks.Add(newPageData);
pageNode.Page = newPageData;
//newPageData.NavigationNode.
Log.Debug("Saving new langauge page basic information.");
manager.SaveChanges();
Am I missing anything?
So I have run into another problem with the translation item that I didn't account for until I tried to add a new language to the system and then tried to generate the new language pages.
So my question for you is, if the page already exists, and has the InitializePageLocalizationStrategy already called, how do we add a new language data? Your example only shows for a null page and one that needs to be split as it where. I believe it I am close with with this.. but I corrupted all of my page data (again) with experiments and am trying to weed it out of the database by hand:
var newPageData = manager.CreatePageData(Guid.NewGuid());
var currentCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = updatedLanguage;
newPageData.TranslationInitialized = true;
newPageData.IsAutoCreated = false;
newPageData.Culture = Thread.CurrentThread.CurrentUICulture.ToString();
newPageData.UiCulture = Thread.CurrentThread.CurrentUICulture.ToString();
pageNode.UrlName[updatedLanguage] = pageInfo.PageName + "_" + updatedLanguage;
pageNode.Description[updatedLanguage] = pageInfo.Description;
pageNode.Title[updatedLanguage] = pageInfo.Title;
pageNode.ShowInNavigation = true;
newPageData.HtmlTitle[updatedLanguage] = pageInfo.HtmlTitle;
newPageData.Title[updatedLanguage] = pageInfo.Title;
newPageData.Description[updatedLanguage] = pageInfo.Description;
newPageData.Keywords[updatedLanguage] = pageInfo.Keywords;
//pageNode.Page.PageLanguageLink.LanguageLinks.Add(newPageData);
pageNode.Page = newPageData;
//newPageData.NavigationNode.
Log.Debug("Saving new langauge page basic information.");
manager.SaveChanges();
Am I missing anything?
Been a few days, any responses forth coming on this?
Regards,
Ray Simpson
Is there a non internal call to a method like this one:
internal PageNode CreatePageNodeLanguageVersion(PageNode sourceNode, CultureInfo sourceLanguage, CultureInfo targetLanguage, bool copyDataFromSource)
Or should I just simply duplicate this method so I can create the new data node in the new language?
Is it a holiday week?
Hello Ray,
I apologize for the delayed response.Please note that for our Sitefinity SDK samples, we have several projects which are being built dynamically entirely through code (that is all the pages, controls, users, and content is generated upon project initialization in the application's Global.asax codebehind. Most of the code used is contained in the SampleUtilities project in the SDK, this is where the initial code samples we were discussing are coming from. The Education Starter Kit, for example is one of these projects, which I'd strongly recommend you to take a look at if you want to get a better idea of what's happening when constructing the page through code.
Kind regards,
Boyan Barnev
the Telerik team
Hi,
Is there any update on this? How to create translation for a node when InitializePageLocalizationStrategy already called?
Thanks.