Please enable Javascript to correctly display the contents on Dot Net Tricks!
 
Become an Expert in C#, .NET, MVC, JAVA, PHP, AngularJS, Hadoop, Android, iphone, Testing etc.
by Joining our Training Programs and Take Your Career to the Next Level! To know more make a call on +91-9871-74-9695

Understanding Internationalization in ASP.NET MVC

Posted By : Shailendra Chauhan, 08 Mar 2015
Updated On : 09 Mar 2015
Total Views : 16,560   
 
Keywords : localization and globalization in asp.net mvc, localization of validation messages, multilingual website in asp.net mvc

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.

Download Code

 
Further Reading
 
About the Author
Hey! I'm Shailendra Chauhan full-time author, consultant & trainer. I have more than 6 years of hand over Microsoft .NET technologies and other web technologies like JavaScript, AngularJS, NodeJS etc. I am an entrepreneur, the founder & chief editor of www.dotnet-tricks.com and www.dotnettricks.com. I am author of most popular e-books for technical Interview on ASP.NET MVC Interview Questions and Answers & AngularJS Interview Questions and Answers & LINQ Interview Questions and Answers.
I have delivered 100+ training sessions to professional world-wide over Microsoft .NET technologies such C#, ASP.NET MVC, WCF, Entity Framework and other mobile technologies such Ionic, PhoneGap, Corodva. Read more...
 
Free Interview Books
 
SUBSCRIBE & FOLLOW US
 
Browse By Category
 
 
Like us on Facebook