In this tutorial, you will learn how to work with Episerver languages using code. When you work on a multiple language website, you will need to enable (and potentially disable) additional languages that your Episerverr powered website supports. If you have read my guide on enabling languages within Episerver, How To Enable Multiple Languages In Your Episerver Website, you will know how to manually enable and disable the languages your website can support. Manually configuring and managing languages between environments is not ideal. A manual process may lead to inconsistent environments. This inconsistent set-up can be a cause of failing unit tests, can cause odd bugs, and can lead to inconsistencies throughout the value stream. One developer might have their local site set to support English and French, while another might use Swedish. If they try and import content using import/export, it's very common that one developer's import works, while another developer's website doesn't. Trust me when I say this. When you are building a website and there are lots of content amends taking place, having to constantly stop and figure this stuff out is a pain in the bum. A better solution is simply writing code to ensure everyone is working from the same songbook. Luckily it's fairly simple to write some code to do everything for you. If you want to take the next step to Episerver mastery, read on!
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:
One useful thing to note is that the language definitions returned from the API are all set as read-only by default. This ILanguageBranch
API follows the same principle as the page data API. If you want to modify language data (like enabling/disabling) you need to create and modify a clone. It is only possible to update language data via clones! The code to do this looks like this:
After you clone your language branch, you can then modify it, how you see fit and then save it. You can save a clone using this code:
Following this approach, you can then modify, create new language definitions, or, even delete language. To delete a language you can use this snippet:
The code above covers the basics of working with Episerver languages in code. If you want a start-up script that enables/disables the languages on your site, I suggest you create an initialisation module that contains the code found above. Initialisation modules will be loaded whenever the site is first to run. Putting code into one of these bad boys, means that whenever anyone loads the site, the correct languages will be set, regardless of which environment the code is run. This will make your life a lot easier. Happy Coding 🤘