Step 1. Open your Visual Studio and click File=> New=>Project, as shown below.
Step 2. Now, select the ASP.NET Web Application template and give a name to your project. Here, our project name is “MultiLanMVC” and click the OK button.
Step 3. Select “Empty MVC template” and click the OK button.
Step 4. You have to go to Solution Explorer and right-click on your Project name=> Add=>New item.
Step 5. In this section, you have to add “Resources File”.
Step 6. In this section, I have used the Field name and its value declaration. You can click Access Modifier then select Public Access Modifier. Don’t forget to change all the resource files from Access Modifier to Public.
Step 7. Here, I have added four resource files for four different languages. I’m discussing here four resource files, which are.
- Resource.resx: It is used for the default resource file associated with English. Here “.resx” is an extension file name in the Resource file.
- Resource.Hi.resx: This resource file is used for the Hindi language.
- Resource.Ja.resx: This resource file is used for the Japanese language.
- Resource.Ar.resx: This resource file is used for the Arabic language.
Step 8. Go to Solution Explorer and right-click on Models=>Add=>Class.
Step 9. In the following figure, give a name to your model class. Here, our class name is “Registration”.
Our coding is given below.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace MultiLanMVC.Models
{
public class Registration
{
[Display(Name = "FirstName", ResourceType = typeof(Resource))]
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "FirstNameRequired")]
public string FirstName { get; set; }
[Display(Name = "LastName", ResourceType = typeof(Resource))]
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "LastNameRequired")]
public string LastName { get; set; }
[Display(Name = "Email", ResourceType = typeof(Resource))]
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "EmailRequired")]
[RegularExpression(@"^([0-9a-zA-Z]([\+\-_\.][0-9a-zA-Z]+)*)+@(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]*\.)+[a-zA-Z0-9]{2,3})$", ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "EmailInvalid")]
public string Email { get; set; }
[Display(Name = "Country", ResourceType = typeof(Resource))]
[Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "CountryNameRequired")]
public string Country { get; set; }
}
}
Step 10. Go to Solution Explorer= > Right click on Project name= > Add= > Class => Enter Class name= > Add.
Coding
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
namespace MultiLanMVC
{
public class LanguageMang
{
public static List<Languages> AvailableLanguages = new List<Languages>
{
new Languages { LanguageFullName = "English", LanguageCultureName = "en" },
new Languages { LanguageFullName = "Hindi", LanguageCultureName = "Hi" },
new Languages { LanguageFullName = "japanese", LanguageCultureName = "Ja" },
new Languages { LanguageFullName = "arabic", LanguageCultureName = "Ar" },
};
public static bool IsLanguageAvailable(string lang)
{
return AvailableLanguages.Any(a => a.LanguageCultureName.Equals(lang));
}
public static string GetDefaultLanguage()
{
return AvailableLanguages[0].LanguageCultureName;
}
public void SetLanguage(string lang)
{
try
{
if (!IsLanguageAvailable(lang))
lang = GetDefaultLanguage();
var cultureInfo = new CultureInfo(lang);
Thread.CurrentThread.CurrentUICulture = cultureInfo;
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);
HttpCookie langCookie = new HttpCookie("culture", lang);
langCookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(langCookie);
}
catch (Exception) { }
}
}
public class Languages
{
public string LanguageFullName { get; set; }
public string LanguageCultureName { get; set; }
}
}
Step 11. Here, add another class. Go to Solution Explorer= > Right-click on Project name= > Add= > Class => Enter class name= > Add. Here, our Class name is “MyController”.
Our coding is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MultiLanMVC
{
public class MyController : Controller
{
protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
{
string lang = null;
HttpCookie langCookie = Request.Cookies["culture"];
if (langCookie != null)
{
lang = langCookie.Value;
}
else
{
var userLanguage = Request.UserLanguages;
var userLang = userLanguage != null ? userLanguage[0] : "";
if (userLang != "")
{
lang = userLang;
}
else
{
lang = LanguageMang.GetDefaultLanguage();
}
}
new LanguageMang().SetLanguage(lang);
return base.BeginExecuteCore(callback, state);
}
}
}
Step 12. Now, right-click on your Controller folder and add=>Controller=>Empty Controller and add Controller name. I have a controller named “HomeController”.
Code
using MultiLanMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MultiLanMVC.Controllers
{
public class HomeController : MyController
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Registration r)
{
return View(r);
}
public ActionResult ChangeLanguage(string lang)
{
new LanguageMang().SetLanguage(lang);
return RedirectToAction("Index", "Home");
}
}
}
Step 13. Now, right-click on ActionResult and add a new View. Click to add button.
@model MultiLanMVC.Models.Registration
@{
ViewBag.Title = MultiLanMVC.Resource.Register;
}
<h2>@MultiLanMVC.Resource.Register</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Registration</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Country, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Country, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Country, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="@MultiLanMVC.Resource.Register" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Step 14. Add the given code to the Complete Layout page. (_Layout .cshtml).
<div style="padding:5px">
@{
foreach (var i in MultiLanMVC.LanguageMang.AvailableLanguages)
{
@Html.ActionLink(i.LanguageFullName, "ChangeLanguage", "Home", new { lang = i.LanguageCultureName }, null)
<text> </text>
}
}
</div>
Here, the default output is given blow.
Click the English tab at the top of the page and you will see all the required validations are in the Spanish language.
Click the Hindi tab at the top of the page and then you will see all the required validations are in Hindi language.
Click the Japanese tab at the top of the page and you will see all the required validations are in Japanese language.
Click the Arabic tab at the top of the page and then you will see all the required validations are in Arabic language.
I hope you enjoyed this multi-language tutorial. Follow C# Corner to learn new things about ASP.NET MVC. Thanks for reading this article.