Sitecore Difference between Sitecore.Context.Language & Sitecore.Context.ContentLanguage

Kamran picture Kamran · Jan 19, 2016 · Viewed 8.8k times · Source

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!

Answer

Vlad Iobagiu picture Vlad Iobagiu · Jan 19, 2016

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);
 }