An Easier Approach To Working With Model Builder And Vorto Within Umbraco

In this tutorial, you will learn how to access multi-language data in your controllers using Vorto. 

For those of you who are new to architecting multi-language websites within Umbraco, when it comes to building your site, you really have two main options and it's usually the way the content is structured is the deciding factor. 

In one approach, you create separate sub-sites for each language the site supports.  If your website has a different page structure for each different language, this approach makes sense.  If the content of your website follows the same page hierarchy and content structure, creating loads of subsites and copying loads of content is painful and takes forever.  If your site structure is the same regardless of language, then a better approach is to use Vorto. 

What Is Vorto?

Vorto is an Umbraco package that allows content editors to manage multi-language switching on a per property basis. 

One issue with Vorto is that it's not part of the core product.  If you use Vorto with the Umbraco Model binder and you also want to do some additional work with the data entered within Umbraco before the page is rendered, then you are going to need to write some code.  Keep reading to find out how.

How Can I Use Vorto Within A Controller

When you create a property using Vorto within Umbraco and generate a strongly typed class, out of the box Umbraco will generate a property of type Our.Umbraco.Vorto.Models.VortoValue. 

If you look at the class you'll see that it uses a collection of key-value pairs to store its data. If you want you can manually get the data out of Vorto yourself - read this information to see how - luckily there is an easier way to work with it, namely using property value converters to make the property return the correct type.  Thanks to Dave Woestenborghs for pointing this out to me :)

If you look at the code Umbraco generates, you will see that all the classes are partial.  In the example below, let's say I've got a page-type with a property called sections, that is a Vorto wrapped nested content property.  The model Umbraco will generate will look like this:

public partial class ExamDatesAndFeesPage : BaseDocument
		public Our.Umbraco.Vorto.Models.VortoValue Sections
			get { return this.GetPropertyValue<Our.Umbraco.Vorto.Models.VortoValue>("sections"); }

As I've mentioned working with VortoValue  is kinda of a pain.  It will be much easier to work with the underlining IEnumerable<IPublishedContent> instead.

Instead, you can write some code to do it yourself.  If you create a new class, name it the same as the document type and ensure you use the partial modifier, it will magically hook-up and be available whenever you access your Model.

public partial class ExamDatesAndFeesPage
  public IEnumerable<IPublishedContent> SectionsConverted
return this.GetVortoValue<IEnumerable<IPublishedContent>>("sections");
} } }

You can then create a property implementing the 'ImplementPropertyType' attribute and using the Vorto extension method, GetVortoValue().  In your code, you will now magically get the correct values for the correct language.  This approach is a lot easier and nicer than manually having to serialize and deserialize things yourself, so I recommend you give it a go.

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