e-Zest members share technology ideas to foster digital transformation.

Globalization and Localization in ASP.NET MVC application

Written by Snehal Patil | Dec 29, 2017 10:27:16 AM

Localization comes into play when you want the implemented application to run in a local language.

If your website targets users from different parts of the world then your website should support different languages because people are more likely use websites in their local language.

Globalization is the process of designing the application in such a way that it can be used by users from multiple cultures across the globe.

Localization, on the other hand, is the process of customization to make our application behave as per the local culture.

Let's start.

Step 1: Open Visual Studio.

Step 2: Click on File menu – select new project- enter an appropriate project name.

Step 3: Add the resource file to your project.

  • Right-click on the Root Folder, click Add New Item.
  • Select Resource File from the given Templates.
  • Name it "Resource.resx".
  • You need other Resource Files for storing data depending on the language.
  • Add another file with the following names for each resource " Resource.gu-IN.resx”.

Resource.mr-IN.resx, Resource.te-IN.resx, Resource.en-US.resx, Resource.hi-IN.resx". You can also give the file a different name but after the (Dot) because they are sub-files of "Resource.resx".

Step 4: Now create a model. Add a model to your application name as DataModel and specify properties for it as below:

public class DataModel

    {

        public string FirstName { get; set; }

        public string LastName { get; set; }

    }

Step 5: Add the controller and specify global variables for ResourceManager and CultureInfo classes like below:

ResourceManager rm;
CultureInfo ci;

Controller action should be defined as below:

public ActionResult Index(DataModel model)

        {

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            rm = new ResourceManager("Resources.Strings", System.Reflection.Assembly.Load("App_GlobalResources"));

           // model.Header = rm.GetString("Header", Thread.CurrentThread.CurrentCulture);

            model.FirstName = rm.GetString("FirstName", Thread.CurrentThread.CurrentCulture);

            model.LastName = rm.GetString("LastName", Thread.CurrentThread.CurrentCulture);

            return View(model);

        }

Step 6: Add the view for Index controller action as below:

<fieldset>

    <legend>DataModel</legend>

    <div class="display-label">Fname</div>

    <div class="display-label">Lname</div>

   </fieldset>

<p>

    </br>

    </br>

    @Html.ActionLink("Convert To English", "English", "Home")

    </br>

    @Html.ActionLink("Convert To Hindi", "Hindi", "Home")

    </br>

    @Html.ActionLink("Convert To Marathi", "Marathi", "Home")

    </br>

    @Html.ActionLink("Convert To Telugu", "Telugu", "Home")

    </br>

    @Html.ActionLink("Convert To Gujrati", "Gujrati", "Home")

</p>

Step 7: To add actions for other languages, refer to step 5

Note: To refer to an Index action you need to mention corresponding language parameter in the  CultureInfo step.

Controller Action content for Hindi language.

public ActionResult Hindi(DataModel model)

        {

            rm = new ResourceManager("Resources.Strings", System.Reflection.Assembly.Load("App_GlobalResources"));

            Thread.CurrentThread.CurrentCulture = new CultureInfo("hi-IN");

           // model.Header = rm.GetString("Header", Thread.CurrentThread.CurrentCulture);

            model.FirstName = rm.GetString("FirstName", Thread.CurrentThread.CurrentCulture);

            model.LastName = rm.GetString("LastName", Thread.CurrentThread.CurrentCulture);

            return View(model);

        }

Step 8: Add view for Hindi language.

 

Step 9: Now when you run the application you will get ASP.NET MVC localization in Hindi, English, Marathi, Telugu, Gujrati.

You are now ready to implement MVC application in a language of your choice.