March 17, 2018

Simple data localisation with .NET core – Part 4 – Add data localisation for models using iQuarc.DataLocalization

This tutorial will take you through setting up a project from scratch using iQuarc.DataLocalization. Full source can be found on github.

  1. Set up basic .NET core MVC project
  2. Models, database and migrations with code first
  3. API Controller to view model data
  4. Data localisation for models using iQuarc.DataLocalization

We will be using a simple domain of a taking a Course and each Course consists of many Skills. Both Course and Skill data needs to be localised to different languages. We will keep the models super simple and only give them a Title and Name property.

This is the fourth part. If you need to set up a new project you can see part 1: Set up basic .NET core MVC project.

Add data localisation for models using iQuarc.DataLocalization

Finally it is time to localize our data. We do that through the following steps:

1. Add NuGet package iQuarc.DataLocalization

2. Create a Language entity that can hold the supported languages

3. Create translation entities that can hold our translated data

4. Add translation entities and configure localisation on the database by adding a static initialiser and dbsets to the DbContext

5. Add translation mappings to the original entities

6. Create a new migration with the add-migration command and a migration name of your choice and update the database

7. Add translations, for simplicity we do it in our DataInitializer. You would want a GUI for this eventually. Add this below the initialisation of courses and skills:

We are not adding translations for skill 3 and skill 4, this is to demonstrate that if no translation is defined, the default entity propery value is used.

8. We are now ready to write our LINQ queries that will do our localisation magic. Go to the CoursesApiController and change the GetCourses method to:

9. Finally we can run the application and visit /api/Courses we can see the translations in english. If you change the culture info to “sv-SE” it will render the swedish translations.