Person writing on a glass wall.

Change available languages in Episerver through code

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


Examples of how to specify or change language settings for Episerver content programmatically.

Estimated read time : 3 minutes

Jump to

Key takeaways

  • always favor API:s over database edits
  • a lot can be done using ContentLanguageSettingRepository

Introducing the bug

I recently stumbled across a bug in the "Language settings for page" interface. Checking the checkbox “Inherit settings from the parent page” resulted in a reload of the popup and I ended up with an empty checkbox again. This problem comes from when a language is active as an available language but later on is disabled in the admin view. 

After speaking to Episerver explaining the bug and how to re-create it (drop a comment below if you'd like to learn more), Episerver acknowledges that they had seen this issue before, and they wanted me to use SQL to change the settings straight in the database.

But wait, we can do it through code!

Instead of messing around directly in the database, I decided to go for a code-first solution. Definitely a safer and easier approach!
I noticed a gap in the documentation regarding this so I hope this will save some of you some research time.

Examples - Loading, deleting and saving 

Start by loading the repository and the content you want to work with. After that, we can load the settings for that content. We will then receive a list of settings for each language.

var contentReference = new ContentReference(10);
var languageSettingsService = ServiceLocator.Current.GetInstance<ContentLanguageSettingRepository>();
            
var settingsForEachLanguage = languageSettingsService.Load(contentReference);

If we delete all posts in the settings list the page will inherit all the language settings from its parent (this was the solution to get my pages in order and inherited from its parent).
Here is an example of how to delete the language settings, which will ensure that the page inherits its settings instead.

foreach (var languageSetting in settingsForEachLanguage)
{
        if (languageSetting == null)
        {
           continue;
        }

        if (languageSetting.DefinedOnContent != null)
        {
              languageSettingsService.Delete(contentReference, languageSetting.LanguageBranch);
        }
}

If you get an empty list then all the settings are inherited as described above. You can now create new settings if you'd like to set the settings yourself.
Like this:

if (!settingsForEachLanguage.Any())
{
        var contentsetting = new ContentLanguageSetting();
        contentsetting.LanguageBranch = "sv";
        contentsetting.IsActive = true;
        contentsetting.ReplacementLanguageBranch = "en";
        contentsetting.DefinedOnContent = contentReference;

        languageSettingsService.Save(contentsetting);

        contentsetting = new ContentLanguageSetting();
        contentsetting.LanguageBranch = "en";
        contentsetting.IsActive = true;
        contentsetting.DefinedOnContent = contentReference;

        languageSettingsService.Save(contentsetting);
}

I hope that this post will help you save a bit of time if coming across this bug.
Let me know in the comments below if you have any questions!