This article provides an overview of localization of a site in ASP.NET MVC 5 using language-specific resource files.
When a user uses an English language as his preferred language, the default UI culture is en-us. If the user uses a French-based browser, its default UI culture is fr-ca. ASP.NET MVC 5 needs to recognize this UI culture and display its own language to the browser. This MVC 5 web site will implement this localization for English and French languages or any other language as below.
In this example I will explain how to localize the site in various languages using language-specific resource files.
Use the following procedure to create a sample of localizing a site:
- Open Visual Studio and select ASP.NET Web Application.
- Next Select MVC.
- Add a Resource File In My Case named it ErrorMsg.resx.
- Set the Access Modifier of the resource file to Public.
- Add messages to the resource file as below:
- Add another Resource File having values in another language. In my case I named it ErrorMsg.fr.resx. This resource file has messages in the French language.
- Next do a change in the Web.config file as below.
- Right-click the Model and add a new item.
- Add a class file named EmployeeModel.cs.
- Modify the code as below:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
-
-
- namespace MVCDemo.Models
- {
- public class EmployeeModel
- {
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "EmpName")]
- [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$",ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpName")]
-
- public string Name { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "EmpEmail")]
- [RegularExpression(@"[\w-]+@([\w-]+\.)+[\w-]+", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpMail")]
- public string Email { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Address")]
- public string Address { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Technology")]
- public string Technology { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "Experience")]
- [RegularExpression(@"\d+(\.\d{1,2})?", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpExperience")]
-
- public decimal Experience { get; set; }
- [Required(ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "ContactNo")]
- [RegularExpression(@"\d+", ErrorMessageResourceType = typeof(ERRORMSG), ErrorMessageResourceName = "RegExEmpContactNo")]
- public string ContactNo { get; set; }
- }
- }
- Right-click the controller and add a new Controller.
- Select MVC 5 Controller - Empty.
- Add a name to the Controller as below EmployeeController.
- Modify the code of EmployeeController.cs as below:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- namespace MVCDemo.Controllers
- {
- public class EmployeeController : Controller
- {
-
- public ActionResult Employee()
- {
- MVCDemo.Models.EmployeeModel empModel = new Models.EmployeeModel();
- return View(empModel);
- }
-
- [HttpPost]
- public ActionResult Employee(MVCDemo.Models.EmployeeModel model)
- {
- if (ModelState.IsValid)
- {
-
- }
- return View(model);
- }
- }
- }
- Right-click the view and add a view.
- Name the view as Employee.cshtml.
- Modify The code of Employee.cshtml as below:
- @model MVCDemo.Models.EmployeeModel
- @{
- ViewBag.Title = "Employee";
- }
- <h2>Employee</h2>
- <form method="post">
- @using (Html.BeginForm())
- {
- @Html.AntiForgeryToken()
- @Html.ValidationSummary()
- <table>
- <tr>
- <td>Name:</td>
- <td>@Html.TextBox("Name") @Html.ValidationMessage("Name", "*")</td>
- </tr>
- <tr>
- <td>Email:</td>
- <td>@Html.TextBox("Email") @Html.ValidationMessage("Email", "*")</td>
- </tr>
- <tr>
- <td>Address:</td>
- <td>@Html.TextBox("Address")@Html.ValidationMessage("Address", "*")</td>
- </tr>
- <tr>
- <td>Technology:</td>
- <td>@Html.TextBox("Technology")@Html.ValidationMessage("Technology", "*")</td>
- </tr>
- <tr>
- <td>Experience:</td>
- <td>@Html.TextBox("Experience")@Html.ValidationMessage("Experience", "*")</td>
- </tr>
- <tr>
-
- <td>Contact No.: </td>
- <td>@Html.TextBox("ContactNo")@Html.ValidationMessage("ContactNo", "*")</td>
- </tr>
- <tr>
- <td colspan="2"><input type="submit" value="Submit" /></td>
- </tr>
-
- </table>
- }
- </form>
- @section Scripts {
- @Scripts.Render("~/Scripts/jquery.validate.js")
- @Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")
- @Scripts.Render("~/Scripts/MicrosoftAjax.debug.js")
- @Scripts.Render("~/Scripts/MicrosoftMvcAjax.debug.js")
- @Scripts.Render("~/Scripts/MicrosoftMvcValidation.debug.js")
- }
- Now click on Internet Explorer and run the program.
- The UI will open as below:
- Click the submit button without making any entry in the some field of the form.
- Now go to the browser and change the language for Tool -> Internet Options.
- Click the Add button.
- Select the language depending on your resource file language and click ok.
- Run you application and click the submit button.
Conclusion
In this article I have explained how to localize easily and properly a MVC 5 web site.