In this article, I will show you how to globalize and localize your application in MVC 4.
IntroductionIn simple step I will show you to make you application globalize and localize in MVC 4.I am writing this article because I found that there are not good and clean example of Globalization and Localization and many developer wont get any proper output from searching on Google . If you go hunting for Globalization and Localization for ASP.NET Webforms then you will get many cool example on this.I to wrote Globalization and Localization in ASP.NET Webforms here is URL you can have a look at it: What is Globalization and Localization in ASP.Net.Before starting let's have look at definition of globalization and localization below.What is Globalization and Localization?Globalization is the process of designing the application in such a way that it can be used by users from across the globe (multiple cultures).Localization, on the other hand, is the process of customization to make our application behave depending on the current culture and locale. These two things go together.Image is used from: Globalization.Getting StartedCreate MVC applicationIn Visual Studio 2013 select "File" from the menu and inside that select Project and then a New Dialog will popup with the name New project.Inside that select Templates then select Visual C# and then inside that select web then select ASP.NET MVC 4 Web Application. Name your project "MvcLocalization" and then click OK.After this let's start with adding a resource to your application.Before adding a resource I am will just create a folder named “LocalResource” and inside this I will add a resource.Now to add the resource
For adding the resource just right-click on the LocalResource folder then for inside what we just added select Add and then New Item.A New Dialog will popup with name the Add New Item.Inside the new dialog at the right side is a search box, type resource.Then I will name my first resource file “Resource.resx”.In a similar way we will add all other resources with the given names as above.After adding all the resources your LocalResource folder will look as in the following snapshot.1. Add some data
Let us add some data to these files. Open "Resource.resx".Then just check the Access Modifier dropdownlist and make it Public.Add a name under the "Resource.resx" Filename column and keep the Value column blank as in the following:Save and close Resource.resx.2. Open "Resource.en-GB.resx"Add a name under the "Resource.en-GB.resx" filename and the value that you want to display in the value field.3. Open Resource.hi-IN.resxAdd a name under the "Resource.hi-IN.resx" filename and the value that you want to display in the value field.4. Open "Resource.mr-IN.resx"Add a name under the "Resource.mr-IN.resx" filename and the value that you want to display in the value field.5. Open "Resource.ar-SA.resx"Add a name under the "Resource.ar-SA.resx" filename and the value that you want to display in the value field.Adding Model and Attribute to Properties. Now add a model with the name “Userdetails.cs” and then add a display attribute with the name similar to the resource “Name” column and then add ResourceType = typeof(Resource).
Adding custom class with name CultureHelper.csNow after completing the model now add a class with the name “CultureHelper.cs” in the new folder “Helper”. This class sets the Culture with the use of threading.Here is a code snippet of the CultureHelper class.
After adding CultureHelper now we are moving to the Controller.Adding ControllerIn this part we will add 2 Controllers.
To add a Controller just right-click on the Controller folder and select Add then inside that select Controller.BaseControllerHere is a code snippet of BaseController.
HomeControllerHere is a code snippet of HomeController.
After this we will work on View.We will first work on _LoginPartial.cshtml because we will add a link button on this page to display it on all pages of the website / application. _LoginPartial.cshtml code snippet
Here is the link from where the user will be able to change his website Language.As we have decided for what language we will use that code.English :- 0 | Hindi:- 1 | Marathi :- 2 | Arabic :- 3When the user clicks on this link it will call the Home Controller ChangeCurrentCulture Action result and it will pass an id to it.According to that CultureHelper will get input and it will set the Culture for the page.Adding View for HomeControllerFor adding a View just right-click anywhere inside HomeController and select Add View.After clicking Add View a new dialog will popup with the name Add View. Inside that you need to select:
And click on the Add button.When adding a view using a Scaffold template all the code for the view will be generated.Index.cshtml code snippet.
Now we have completely configured the code. Now we just need to run the application and check it.Complete view of pageIt's taking the default culture, English, when it loads the first time. Here is the link we added.Let's check it by clicking on Hindi first:Now to click on Marathi.And finally click on Arabic.ConclusionFinally we have completed Globalization and Localization in ASP.NET MVC 4. I hope you have enjoyed it.You can download this code from the preceding link.Like this article if you have enjoyed reading it.
Getting Started with ASP.NET 4.5 Web Forms and Visual Studio 2013