简体   繁体   English

如何在 EPiServer 10 中以编程方式“翻译”页面

[英]How to programmatically "translate" a page in EPiServer 10

I am currently creating a new page from code to use as my startpage for a sitedefinition which I also create from code..however.. even if I publish the newly created startpage I always end up with the following message in the CMS UI:我目前正在从代码创建一个新页面,用作我也从代码创建的站点定义的起始页......但是......即使我发布了新创建的起始页,我总是在 CMS UI 中得到以下消息:

"This content is in English. It does not exist in svenska. Would you like to translate it now?" “此内容是英文的。在 svenska 中不存在。您现在要翻译吗?”

How can I "Translate" the page from programmatically and then publish it as well?如何以编程方式“翻译”页面,然后也发布它? I haven't been able to find anything related to this here or while googling it.我在这里或在谷歌搜索时找不到与此相关的任何内容。

You need to use the CreateLanguageBranch available in the IContentRepository.您需要使用CreateLanguageBranch提供的 CreateLanguageBranch。

In my example below Swedish is the default language on the site在我下面的示例中,瑞典语是网站上的默认语言

var parent = ContentReference.RootPage;

IContentRepository contentRepository = 
    EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
StartpagePage startpage = contentRepository.GetDefault<StartpagePage>(parent);

startpage.PageName = "Teststartsida";
startpage.Title = "Teststartsida";

// this will create a startpage in the default language, Swedish in my case, 
// use SaveAction.Publish and save the page into a new variable
var createdPage = contentRepository.Save(startpage,
    EPiServer.DataAccess.SaveAction.Publish, 
    AccessLevel.NoAccess);

// invoke CreateLanguageBranch with LanguageSelector
var startpageLanguageBranch = 
    contentRepository.CreateLanguageBranch<StartpagePage>(createdPage, 
        new LanguageSelector("en"));

startpageLanguageBranch.PageName = "Start page test";
startpageLanguageBranch.Title = "Start page test";

// this will create a languagebranch in the language stated with the LanguageSelector. 
// Use SaveAction.Save
contentRepository.Save(startpageLanguageBranch, 
    EPiServer.DataAccess.SaveAction.Save, 
    AccessLevel.NoAccess);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM