All previous topics were about Azure and how to work with that great technology. But each large project should be localized. We had a case when a project was under development for several years, had a big database and a lot of pages and needed to be localized.
In this article I will show you architecture for that localization. I will do it in simple way and will show general moments: how to save a current language? Work with localized database and localized strings in code or HTML.
First we need to open out MVC solution and database project.
Let's change our database project. In very first step we should create table “Language”:
Now let’s localize table “Gender”. For this we need to create table “GenderLanguage”:
Localized table Gender
Now let’s push this changes to our Azure database “MvcApplicationWebRoleDatabase” via “schema compare” operation in our database project:
Now we can see this changes in Azure database “MvcApplicationWebRoleDatabase”. Let’s add some languages to table “Language” and add some localization information in table “GenderLocalization”:
We finished database part. Now let’s open our “MvcApplicationWebRole” solution.
In general, we should do two things every time when change language: set current culture (needs to get correct resource file), get current language id (needs to work with a database).
But before this step we should add a reference to table “Language” and “GenderLanguage” to our database context. For this step we should update our model. Open Double click on “DatabaseContext.edmx” file. You will see the designer. Right click on empty space and select “Update Model from Database”. Go to the wizard. In the end step, select two new table:
Include new tables to EDMX
In result we receive this diagram:
Let’s change our database initializer to put default value into new tables:
Now let’s create simple buttons to change the language. I have created enum LanguageType, the extension for this enum. And add two links in _Layout.cshtml:
Language button links
This “Localization” controller will just set Language Id and Language code as Session “Key-Value”. As default value I will select English language:
Set new culture controller
Now we should override method “Application_AcquireRequestState” in Global.asax and set culture on that method:
Set current culture
Let ‘screate new Class Library “MvcApplicationWebRole.Localization”. To this class library we should add new folder “App_GlobalResources”. To this folder we should add resource files. Let's name resource file “Strings.resx”. This is default resource file. For all specific culture, file name should have language code in the name. For example, for English it should be “String.en-US.resx”. Also, we should configure each resource file. For this right-click on resource file and set two properties:
- Build Action property set to “Embedded Resource”
- Custom Tool set to “PublicResXFileCodeGenerator”
Result on next 2 images below:
“MvcApplicationWebRole.Localization” class library tree
Set properties for resource file
Now let’s create controller “MemberGenderType” and will show in this controller how localization works. In “MemberGenderType” we will use localized table “Gender” and get gender display name. In view of action from this controller we will use some localized string from our resources (next 2 images below):
Get localized gender display name in controller
Get localized string from resource
Now we can run our solution. Go to “Localization” controller, click on button “En” or “Fr”. And we will see that localization works:
Localized MVC application
Best Regards. Andrew Tsopych CTO of Diceus