If you find this article useful, consider making a small donation to show your support for this web site and its content.
DiscountASP
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.

Localizing ASP.NET MVC

by Farooq Kaiser 6. September 2009 13:23

Localization is the process of customizing your application for a given culture and locale. The .NET framework offers the following namespaces and classes to facilitate the localization.

  1. System.Globalization (The System.Globalization namespace contains classes that define culture-related information, including the language, the country/region, the calendars in use, the format patterns for dates, currency, and numbers, and the sort order for strings)
  2. CutureInfo Provides information about a specific culture.
  3. The System.Resources namespace provides classes and interfaces that allow developers to create, store, and manage various culture-specific resources used in an application.

In this article, I will examine how to localize ASP.net MVC application using existing .net framework. To create a new MVC project, see ASP.net MVC application structure. Now, i will create a App_GlobalResources folder that will have the application resource files. To create a resource file in App_GlobalResources folder, follow these steps.

  1. Right-click your project –> Add –> Add ASP.NET Folder –> App_GlobalResources
  2. Right-click App_GlobalResources –> Add –> New Item  –> Resources file

I will create two resource files English and French respectively. Now, I will change the "Access Modifier" settings to "Public" as shown below.

localize1

Now i can access the resource setting in a strongly typed as shown below in my view.

local2

The easiest way to determine the visitor's preferred culture is to add culture="auto" and uiCulture="auto" in web.config as shown below.

<system.web>
    <globalization culture="auto" uiCulture="auto"/>

However, this approach will not work when rendering a view using different view engine or client setting is invalid. so i will assign the given culture in a global.asax as shown below.

 protected void Application_BeginRequest(object sender, EventArgs e)
 {
      CultureInfo culture = new CultureInfo("fr-CA");
      Thread.CurrentThread.CurrentCulture = culture;
      Thread.CurrentThread.CurrentUICulture = culture;
 }

This will make sure that the current thread will apply the given culture. Now i will run the project and it will render a view as shown below.

local3

Summary

In this article, we examined localize ASP.net MVC application using existing .net framework. To download the complete source code, please visit http://www.codeproject.com/KB/aspnet/Localizing_ASP_NET_MVC.aspx.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

.NET | asp.net | C#

Comments

Jobs Autos Real estate Videos Power by Google
awesome comments