Reader Level:
ARTICLE

Localize Your ASP.NET MVC Application

Posted by Krishna Garad Articles | ASP.NET MVC January 29, 2012
In this article we will discuss how to localize your ASP.NET MVC application.
  • 1
  • 0
  • 13959
Download Files:
 

Introduction

In this article we will discuss how to localize your ASP.NET MVC application. In Multi Language Web-Site in ASP.NET we saw how to localize your ASP.Net web application. In this article we will use the simple example which will localize our MVC application. So let's start it step by step.

Step 1 : Create a new ASP.NET MVC empty Web Application. Next go to Solution Explorer and right-click on the solution and add an App_GlobalResources folder from ASP.Net_Folders.

Step 2 : In the App_GlobalResources folder, add one resource file called as Strings.resx. After opening the Strings.resx, add some string names and values. For this demo I'm using Header, Fname and Lname which values are ASP.Net MVC localization, Sam, Hobbs respectively. We will use the localized name for Sam Hobbs in Hindi.

Step 3 : Next add two more files with the culture information say for English add Strings.en-US.resx and for Hindi add Strings.hi-IN.resx and for that put the strings as in Strings.resx but with localized values eg here for Hindi you can replace the values like given in below figure.

sam.bmp

Step 4 : Still now we have our resources ready in the above resources; you can add your own localized resource also. Next we will show these resources on our View so add the controller to your application and import the namespaces given below.

using System.Resources;
using System.Globalization;
using System.Threading;
using System.Reflection;

And create a global variable for the ResourceManager and CultureInfo classes like below.

  ResourceManager rm;
        CultureInfo ci;

Step 5 : Next add one model to your application called DataModel as class and write the properties with get and set for our resource files names like below.

        public string Header { get; set; }
        public string Fname { get; set; }
        public string Lname { get; set; }

Step 6 : Next we will show English values by default when our page loads so write the Index method which returns a view containing the actionlinks for Hindi as well as English.

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.Fname = rm.GetString("Fname", Thread.CurrentThread.CurrentCulture);
            model.Lname = rm.GetString("Lname", Thread.CurrentThread.CurrentCulture);
            return View(model);
        }

In the above line of code before returning our view we are getting the values from our resource files and assigning them to the model with the default en-US culture. Next add the view by right-clicking in the Index method which contains a space to display the Header, Fname and Lname with the two action links like below.

<fieldset>
        <legend>DataModel</legend>
     <div class="display-label">Header</div>
        <div class="display-field"> </div>
            <%: Html.DisplayFor(model => model.Header) %>
        <div class="display-label">Fname</div>
        <div class="display-field">
            <%: Html.DisplayFor(model => model.Fname) %>
        </div>   
        <div class="display-label">Lname</div
>
        <div class="display-field">
            <%: Html.DisplayFor(model => model.Lname) %>
        </div>           
    </fieldset
>
    <p>
        <%=Html.ActionLink("Convert To English","English","Home") %>
<%=Html.ActionLink("Convert To Hindi","Hindi","Home") %>
    </p>

In the above markup we showed our model values with action links for English and Hindi.

Step 7 : Next add two more actions in the controller; one for English and the second For Hindi resources like below.

public ActionResult English(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.Fname = rm.GetString("Fname", Thread.CurrentThread.CurrentCulture);
            model.Lname = rm.GetString("Lname", Thread.CurrentThread.CurrentCulture);
            return View(model);
        }
        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.Fname = rm.GetString("Fname", Thread.CurrentThread.CurrentCulture);
            model.Lname = rm.GetString("Lname", Thread.CurrentThread.CurrentCulture);
            return View(model);
        }

The above two methods return the respective culture view; the next add two views, one for English and another for Hindi which will display the model values like we showed in the Index view.

Step 8 : Now run your application and see the Sam Hobbs as well as ASP.NET MVC localization in Hindi as well as in English.

Conclusion

In this way we can implement localize MVC application.

COMMENT USING

Trending up