Please enable Javascript to correctly display the contents on Dot Net Tricks!

Understanding Internationalization in ASP.NET MVC

  Author : Shailendra Chauhan
Updated On : 23 Nov 2016
Total Views : 32,966   
 

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 products/softwares 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 products/softwares 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 products/softwares in such a way that they can be customize 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.


ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

YOU MIGHT LIKE
Free Interview Books
 
COMMENTS (0)
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

Know More
15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

14 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

3 JAN
ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

5 NOV
ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

4 SEP
MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.

BROWSE BY CATEGORY
 
RECENT ARTICLES
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 

Professional Speaks

+