以特定语言以编程方式创建sitecore项目

Dhe*_*iri 3 c# sitecore sitecore6

我试图以编程方式创建一个sitecore项目.一切顺利.但我创建的项目是使用语言"EN-GB",但创建的新项目是使用语言"EN".有没有办法在创建项目时设置语言值.

var currentDatabase = Sitecore.Configuration.Factory.GetDatabase(selectedItem.Database.Name);

    if (currentDatabase != null)
    {
        var rootItem = currentDatabase.GetItem(RootItemPath);

        if (rootItem != null)
        {
            //Create new item
            var item = rootItem.Add(selectedItem.Name, CommunityProjectTemplateId);

            if (item != null)
            {
                item.Fields.ReadAll();
                item.Editing.BeginEdit();

                try
                {
                    //Add values for the fields
                    item.Fields["Overview Body Content"].Value = selectedItem.GetStringField("ProjectOverview");
                }
                catch (Exception)
                {
                    item.Editing.EndEdit();
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

Ruu*_*ier 10

在检索根项之前,需要切换上下文语言.

using (new LanguageSwitcher("en-gb"))
{
    var rootItem = currentDatabase.GetItem(RootItemPath);
    var item = rootItem.Add(selectedItem.Name, CommunityProjectTemplateId);

    // Do your editing on item here...
}
Run Code Online (Sandbox Code Playgroud)

  • 但是为什么它不从配置文件中选择默认语言? (2认同)

Gab*_*bar 5

如果您尝试在en-GB中添加项目,请确保通过执行此操作获得的"根项目"为en-GB.

Language language;
Language.TryParse("en-GB", out language);
var rootItem = currentDatabase.GetItem(RootItemID, language);
Run Code Online (Sandbox Code Playgroud)

之后,如果您尝试添加项目,则将使用该语言.使用语言切换器也是一个好主意,但在更改语言环境时要小心.