In this tutorial, you will learn how to access multi-language data within Umbraco 7 using the community package, Vorto. When it comes to architecting a multi-language website within Umbraco V7 you have two options. The option you pick will be based on how the content is structured.
In one approach, you create separate sub-sites within the Umbraco page tree for each different language. 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.
Vorto is a Umbraco package that allows content editors to manage multi-language content by switching between languages 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 builder and you also want to do perform additional logic within a controller on that data, 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 use Model Builder with Vorto, 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. This magic approach will use 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 Models Builder generates, you will see that all the classes are created as partial
. In the example below, let's say I've got a page-type with a property called sections
. This sections
data type is a Vorto property that nests a content data type. The model Umbraco will generate will look like this:
Working with VortoValue
is kind of a pain. It will be much easier to work with the underlining IEnumerable
instead. You can do this by writing some code. Create a new class in your project and name it the same as the document-types class name. Ensure you use the partial
modifier. This will magically hook up to the model's builder generated class and will be available whenever you access your model. Take this code:
You can then create a property implementing the ImplementPropertyType
attribute and using the Vorto extension method, GetVortoValue()
to get the value. In your controller code, you will now magically get a strongly-types object to work with your language variations. 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. Happy Coding 🤘