In today's guide, I will cover how to set-up a website to run in a multi-lingual environment.
Step One: Creating A Default Language Cookie
In MVC we can use a global ActionFilter to ensure certain functionality is triggered when a user navigates to one of our webpages. Writing a global action filter will allow us to add in some extra logic and apply it across multiple controllers and/or actions. The first thing our Action Filter will need to do is to check if our language cookie already exists. If it doesn't then set a default language of the page the visitor is browsing.
Step Two: Getting A Reference in ActionFilterAttribute to the Current Page
The next part of the dilemma is then switching the language of the current request if it doesn't match the language specified in the cookie. The first thing we need to do is get access to the page object. As we're in an Action Filter, we do not have direct access to the API, instead, we can get it using this snippet:
Step Three: Switching The Language
The next snippet we will need is a way to switch the language. This can be done using this snippet.
Last Step: Putting It All Together
All the cookie get/set is done using this helper method
This covers everything you need to have in a site that works via a language cookie. In the next tutorial Creating A Multi Language Picker In Episerver I'll explain how to create a language picker. This picker will provide the functionality to let site visitors decide which language the site will render itself.