How To Use The LanguageBranchRepository To Enable/Disable Your Episervers Website In Code

When you work on a multiple language website, you will have to enable/disable the languages that your website supports.  If you have read my guide on enabling languages within Episerver, How To Enable Multiple Languages In Your Episerver Website then you will know how to manually enable and disable the languages your website can support.

Some of the problems with developers manually configuring the supported languages include failing unit tests, odd bugs, inconsistent environments which can lead to inconsistent bugs throughout the value-stream. One developer might have their local site to support English and French, while another might use Swedish. If they try and import content using import/export then it's very common that one person's machine works, while another developer's website doesn't.  Trust me when I say this, it's a pain in the bum when things can't be done by simply running some code.  

These issues are the reason why I recommend, that on every Epsierver project you work on, you create some set-up code that will create some website pages for you, set some default project configuration settings so all developers, QA are all configured consistently.  Enabling/disabling languages is no different and luckily it's fairly simple to write some code to do everything for you.  In today's guide I'm going to cover all the code you'll need, so let's begin

The ILanguageBranchRepository

To get/set your website's language settings, you'll need to use the ILanguageBranchRepository. To get a complete list of all the languages defined in your site, you can use this snippet:

var repository = ServiceLocator.Current.GetInstance<ILanguageBranchRepository>();
var languages = repository.ListAll()  

One thing you should notice about the language definitions returned is that they are all set as read-only. This means that if you want to modify it (like enabling/disabling), just like you would when you modify an existing Episerver page in code, you need to create a clone, modify that clone and then write it. Which looks like this:

        public LanguageBranch CreateWritableLanguageClone(LanguageBranch language, bool enabled)
            return new LanguageBranch
                ACL = language.ACL,
                Culture = language.Culture,
                Enabled = enabled,
                ID = language.ID,
                Name = language.Name,
                RawIconPath = language.RawIconPath,
                SortIndex = language.SortIndex,
                URLSegment = language.URLSegment

After you clone your language you can then modify it, how you see fit and then save it.

var repository = ServiceLocator.Current.GetInstance<ILanguageBranchRepository>();            
foreach (var language in repository.ListAll())
     var clone = CreateWritableLanguageClone(language, false);

 Following this approach, you can then modify, create new market definitions, or, even delete markets. To delete a language you can use this snippet:

var repository = ServiceLocator.Current.GetInstance<ILanguageBranchRepository>();            
repository .Delete(language.ID);


submit to reddit

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top