Understanding Internationalization in ASP.NET MVC

Shailendra Chauhan   Source Code  Print 
08 Mar 2015
13 Sep 2018
Intermediate
48.1K

Internationalization is abbreviated to i18n, where 18 stands for the number of letters in word Internationalization between the first i and last n. Internationalization is the process of developing product/software in such a way that they can be localized for languages and cultures easily. It involves Globalization and Localization.

Globalization and Localization

Globalization is abbreviated to G11n, where 11 stands for the number of letters in word Globalization between the first G and last n. It is the process of developing product/software in such a way that they can support different cultures.

Localization is abbreviated to L10n, where 10 stands for the number of letters in word Localization between the first L and last n. It is the process of developing product/software in such a way that they can be customized for a specific culture.

Culture in ASP.NET Framework

ASP.NET framework have two culture - Culture and UICulture. Typially, these cultures values are composed of two lower-case letters defining the language and two upper case letters defining the locale (region).

For example, "en" represents English language and "GB", "US" represent Britain and American respectively. In this way, British English is defined as "en-GB", while American English is defined as "en-US".

The Culture determines the results of culture-dependent functions like as date, number, and currency.

The UICulture is used to locate correct resource file and to be render it for a webpage by the ResourceManager.

Every thread in .NET has CurrentCulture and CurrentUICulture properties. These properties are used by ASP.NET globalization framework while rendering culture-dependent functions and values.

Internationalization of Validation Messages

To make you validation messages in different languages, you need to add translated messages with a key for every culture, which your application will support. In this article, I am going to add transalted messages for English and Hindi. Here, default culture is "en".

In this example, I am storing resource files for English and Hindi culture in a separate assembly, so that you can add reference of that assembly in other project as well.

Using Resources within ViewModel class

public class UserViewModel
{
 [Display(Name = "Username", ResourceType = typeof(ViewResources.Resource))]
 [Required(ErrorMessageResourceName = "UsernameRequired", ErrorMessageResourceType = typeof(ViewResources.Resource))]
 public string Username { get; set; }

 [Display(Name = "Name", ResourceType = typeof(ViewResources.Resource))]
 [Required(ErrorMessageResourceName = "NameRequired", ErrorMessageResourceType = typeof(ViewResources.Resource))]
 public string Name { get; set; }

 [Display(Name = "Password", ResourceType = typeof(ViewResources.Resource))]
 [Required(ErrorMessageResourceName = "PasswordRequired", ErrorMessageResourceType = typeof(ViewResources.Resource))]
 public string Password { get; set; }

 [Display(Name = "ConfirmPassword", ResourceType = typeof(ViewResources.Resource))]
 [Required(ErrorMessageResourceName = "ConfirmPasswordRequired", ErrorMessageResourceType = typeof(ViewResources.Resource))]
 [Compare("Password", ErrorMessageResourceName = "ConfirmPasswordCompare", ErrorMessageResourceType = typeof(ViewResources.Resource))]
 public string ConfirmPassword { get; set; }

 [Display(Name = "Address", ResourceType = typeof(ViewResources.Resource))]
 [Required(ErrorMessageResourceName = "AddressRequired", ErrorMessageResourceType = typeof(ViewResources.Resource))]
 public string Address { get; set; }
}

Using Resources within View

@model MVC_Internationalization.Models.UserViewModel

@{
 ViewBag.Title = ViewResources.Resource.Title;
}
<div class="row">
 <div class="col-md-8">
 <h2>@ViewResources.Resource.Title</h2>
 </div>
 <div class="col-md-4">
 @using (Html.BeginForm("ChangeCulture", "Home"))
 {
 <p>
 @ViewResources.Resource.SelectLanguage : @Html.DropDownList("ddlCulture", new SelectList(new[]
 {
 new{value="en",text=ViewResources.Resource.English},
 new{value="hi",text=ViewResources.Resource.Hindi}
 }, "value", "text", Session["CurrentCulture"]), new { onchange = "this.form.submit();" })
 </p>
 }
 </div>
</div>
<br />
@using (Html.BeginForm("Index", "Home"))
{
 @Html.AntiForgeryToken()

 <div class="form-horizontal">

 @Html.ValidationSummary(true, "", new { @class = "text-danger" })
 <div class="form-group">
 @Html.LabelFor(model => model.Username, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.Username, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.Username, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.PasswordFor(model => model.Password, new { @class = "form-control" })
 @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.ConfirmPassword, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.PasswordFor(model => model.ConfirmPassword, new { @class = "form-control" })
 @Html.ValidationMessageFor(model => model.ConfirmPassword, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.TextAreaFor(model => model.Address, new { @class = "form-control" })
 @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 <div class="col-md-offset-2 col-md-10">
 <input type="submit" value="@ViewResources.Resource.Save" class="btn btn-default" />
 </div>
 </div>
 </div>
}

Setting Culture within Controller

public class HomeController : Controller
{
 //initilizing culture on controller initialization
 protected override void Initialize(System.Web.Routing.RequestContext requestContext)
 {
 base.Initialize(requestContext);
 if (Session["CurrentCulture"] != null)
 {
 Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["CurrentCulture"].ToString());
 Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["CurrentCulture"].ToString());
 }
 }

 // changing culture
 public ActionResult ChangeCulture(string ddlCulture)
 {
 Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlCulture);
 Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlCulture);

 Session["CurrentCulture"] = ddlCulture;
 return View("Index");
 }

 public ActionResult Index()
 {
 return View();
 }

 [HttpPost]
 public ActionResult Index(UserViewModel user)
 {
 if (ModelState.IsValid)
 {
 //TO DO:
 }
 return View();
 }
}

How it works...

What do you think?

I hope you will enjoy this article while working with ASP.NET MVC. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Hands-on Learning
Free Interview Books
 
+