Code snippets for working with languages in EPiServer

This article was migrated from an older iteration of our website, and it could deviate in design and functionality.


These are a few handy code snippets for working with languages and language versions in EPiServer, for example to check if a page exists for a specific language.

Estimated read time : 3 minutes

Jump to

Getting all languages for a page

If you want to get all language versions for a page you can use the DataFactory GetLanguageBranches method:

PageDataCollection allLanguages =
DataFactory.Instance.GetLanguageBranches(myPageReference);

If you use ETF and only want to get published language versions for a specific page, you can use the PageFactory GetPublishedLanguageBranches method:

PageDataCollection publishedLanguages = 
PageFactory.Instance.GetPublishedLanguageBranches(myPageReference);

Check if a page exists in a specific language

The following code snippet checks if a specific page exists in a specific language. In this example we check if the start page exists for the “fr” language branch.

// Check if the start page exists for the "fr" language branch
bool exists = DataFactory.Instance.GetLanguageBranches(PageReference.StartPage)
              .Any(p => p.LanguageBranch == "fr");

If you want to check if a specific page is published for a specific language, you can use the code snippet above but use the GetPublishedLanguageVersions method instead (requires ETF):

// Check if the start page has been published for the "fr" language branch
bool publishedVersionExists = PageFactory.Instance.GetPublishedLanguageBranches(PageReference.StartPage)
.Any(p => p.LanguageBranch == "fr");

Get the current language in edit mode

This is essentially a way to check what which language is selected in the “Show Page Tree” dropdown in edit mode (handy for example when you create edit plugins):

image

// Returns "en"
return HttpContext.Current.Request.Cookies["editlanguagebranch"] != null ? 
       HttpContext.Current.Request.Cookies["editlanguagebranch"].Value : 
       string.Empty;

Get all languages on a site

If you want to get a list of all enabled languages on a site you can use the PageLanguages property of a page, for example the start page:

// Returns a list of strings such as "en", "fr", "de"
var availableLanguages = DataFactory.Instance.GetPage(PageReference.StartPage)
.PageLanguages;