当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Sitecore Difference between Sitecore.Context.Language & Sitecore.Context.ContentLanguage

问题描述:

For my website:

The output of Sitecore.Context.Language is da

and the output of Sitecore.Context.ContentLanguage is en.

lblTest.Text = Sitecore.Context.Language+" & "+ Sitecore.Context.ContentLanguage;

output: da & en

What is the basic difference between these two terms? And how I can modify them to use some other language?

Thanks!

网友答案:

Content Language represent the default editing language in the client (Content Editor). It's a property in site definition of every website.

Sitecore.Context.Language represent the context language of the website when you navigate it.

For example if you have danish editors, then Content Language will be Danish. You can have multiple context languages, because visitors can switch their language.

Sitecore.Context.Language is a smart property, which means it follows the lazy load pattern: if code accesses this property when nothing has set it, the getter for the property contains logic to determine the context language.

Sitecore uses the Sitecore.Pipelines.HttpRequest.LanguageResolver processor in the httpRequestBegin pipeline to determine the context language.

If you want to change the content language you need to modify the site definition. To change programatically context language you can use:

 Sitecore.Context.Language=yourlanguage;

UPDATE after I read the comments to change context language you can use:

 var myItem = Sitecore.Context.Item;
 using (new Sitecore.Globalization.LanguageSwitcher("da-DK"))
 {
    myItem = myItem.Database.GetItem(myItem.ID);
 }
分享给朋友:
您可能感兴趣的文章:
随机阅读: