EPiServer  /  CMS July 31, 2012

Figuring out the current language branch in EPiServer CMS

Need to figure out the actual language context we’re in, disregarding fallback or replacement languages in EPiServer CMS? The PreferredCulture property of the ContentLanguage class to the rescue!

Sometimes we need to figure out what language context we’re in in EPiServer. If we’re coding a template we may be tempted to look at the LanguageBranch property of the CurrentPage property. However, in other situations that may not be available and that won’t actually tell us what language context we’re in but what language branch the current page is loaded from. In most situations those two values will be the same, but if the page has fallback or replacement languages configured it won’t.

To figure out the actual language context we’re in, disregarding fallback or replacement languages, we can instead use the static PreferredCulture property of the ContentLanguage class located in the EPiServer.Globalization namespace. Like this:

//using EPiServer.Globalization;

CultureInfo currentCulture = ContentLanguage.PreferredCulture;
string languageBranch = currentCulture.Name;

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

Comments

comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS