How To Create Multiple Languages In ASP.NET MVC 4.5 Framework

Step 1 - Open your Visual Studio and click File=> New=>Project, as shown below-
 
1 
 
Step 2 - Now, select ASP.NET Web Application template and give a name to your project. Here, our project name is “MultiLanMVC” and click OK button. 
 
2 
 
Step 3 - Select “Empty MVC template” and click OK button.
 
3 
 
Step 4 - You have to go to Solution Explorer and right click on your Project name=> Add=>New item. 
 
4 
 
Step 5 - In this section, you have to add “Resources File”.
 
5 
 
Step 6 - In this section, I have used 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 Access Modifier to public.
 
6 
 
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 Resource file.
  • Resource.Hi.resx- This resource file is used for Hindi language.
  • Resource.Ja.resx- This resource file is used for Japanese language. 
  • Resource.Ar.resx- This resource file is used for Arabic language.
7
 
Step 8 - Go to Solution Explorer and right click on Models=>Add=>Class.
 
8 
 
Step 9 - In the following figure, give a name to your model class. Here, our class name is “Registration”.
 
9 
 
Our coding is given below-
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.ComponentModel.DataAnnotations;    
  4. using System.Linq;    
  5. using System.Web;    
  6. namespace MultiLanMVC.Models    
  7. {    
  8.     public class Registration {    
  9.         [Display(Name = "FirstName", ResourceType = typeof(Resource))]    
  10.         [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "FirstNameRequired")]    
  11.         public string FirstName {    
  12.             get;    
  13.             set;    
  14.         }    
  15.         [Display(Name = "LastName", ResourceType = typeof(Resource))]    
  16.         [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "LastNameRequired")]    
  17.         public string LastName {    
  18.             get;    
  19.             set;    
  20.         }    
  21.         [Display(Name = "Email", ResourceType = typeof(Resource))]    
  22.         [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "EmailRequired")]    
  23.         [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")]    
  24.         public string Email {    
  25.             get;    
  26.             set;    
  27.         }    
  28.         [Display(Name = "Country", ResourceType = typeof(Resource))]    
  29.         [Required(ErrorMessageResourceType = typeof(Resource), ErrorMessageResourceName = "CountryNameRequired")]    
  30.         public string Country {    
  31.             get;    
  32.             set;    
  33.         }    
  34.     }    
  35. } 
Step 10 - Go to Solution Explorer= > Right click on Project name= > Add= > Class => Enter Class name= > Add.
 
10 
 
Coding
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Globalization;    
  4. using System.Linq;    
  5. using System.Threading;    
  6. using System.Web;    
  7. namespace MultiLanMVC    
  8. {    
  9.     public class LanguageMang {    
  10.         public static List < Languages > AvailableLanguages = new List < Languages > {    
  11.             new Languages {    
  12.                 LanguageFullName = "English", LanguageCultureName = "en"    
  13.             },    
  14.             new Languages {    
  15.                 LanguageFullName = "Hindi", LanguageCultureName = "Hi"    
  16.             },    
  17.             new Languages {    
  18.                 LanguageFullName = "japanese", LanguageCultureName = "Ja"    
  19.             },    
  20.             new Languages {    
  21.                 LanguageFullName = "arabic", LanguageCultureName = "Ar"    
  22.             },    
  23.         };    
  24.         public static bool IsLanguageAvailable(string lang) {    
  25.             return AvailableLanguages.Where(a => a.LanguageCultureName.Equals(lang)).FirstOrDefault() != null ? true : false;    
  26.         }    
  27.         public static string GetDefaultLanguage() {    
  28.             return AvailableLanguages[0].LanguageCultureName;    
  29.         }    
  30.         public void SetLanguage(string lang) {    
  31.             try {    
  32.                 if (!IsLanguageAvailable(lang)) lang = GetDefaultLanguage();    
  33.                 var cultureInfo = new CultureInfo(lang);    
  34.                 Thread.CurrentThread.CurrentUICulture = cultureInfo;    
  35.                 Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureInfo.Name);    
  36.                 HttpCookie langCookie = new HttpCookie("culture", lang);    
  37.                 langCookie.Expires = DateTime.Now.AddYears(1);    
  38.                 HttpContext.Current.Response.Cookies.Add(langCookie);    
  39.             } catch (Exception) {}    
  40.         }    
  41.     }    
  42.     public class Languages {    
  43.         public string LanguageFullName {    
  44.             get;    
  45.             set;    
  46.         }    
  47.         public string LanguageCultureName {    
  48.             get;    
  49.             set;    
  50.         }    
  51.     }    
  52. } 
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”.
 
11 
 
Our coding is given below-
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Web;    
  5. using System.Web.Mvc;    
  6. namespace MultiLanMVC    
  7. {    
  8.     public class MyController: Controller     
  9.     {    
  10.         protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state) {    
  11.             string lang = null;    
  12.             HttpCookie langCookie = Request.Cookies["culture"];    
  13.             if (langCookie != null) {    
  14.                 lang = langCookie.Value;    
  15.             } else {    
  16.                 var userLanguage = Request.UserLanguages;    
  17.                 var userLang = userLanguage != null ? userLanguage[0] : "";    
  18.                 if (userLang != "") {    
  19.                     lang = userLang;    
  20.                 } else {    
  21.                     lang = LanguageMang.GetDefaultLanguage();    
  22.                 }    
  23.             }    
  24.             new LanguageMang().SetLanguage(lang);    
  25.             return base.BeginExecuteCore(callback, state);    
  26.         }    
  27.     }    
  28. } 
Step 12 - Now, right click on your Controller folder and add=>Controller=>Empty Controller and add Controller name. I have a controller name as “HomeController”.
 
12 
 
Code
  1. using MultiLanMVC.Models;    
  2. using System;    
  3. using System.Collections.Generic;    
  4. using System.Linq;    
  5. using System.Web;    
  6. using System.Web.Mvc;    
  7. namespace MultiLanMVC.Controllers    
  8. {    
  9.     public class HomeController: MyController {    
  10.         // GET: Home      
  11.         public ActionResult Index() {    
  12.                 return View();    
  13.             }    
  14.             [HttpPost]    
  15.         public ActionResult Index(Registration r) {    
  16.             return View(r);    
  17.         }    
  18.         public ActionResult ChangeLanguage(string lang) {    
  19.             new LanguageMang().SetLanguage(lang);    
  20.             return RedirectToAction("Index""Home");    
  21.         }    
  22.     }    
  23.  
Step 13 - Now, right click on ActionResult and add a new View. Click to add button.
 
13
  1. @model MultiLanMVC.Models.Registration    
  2. @ {    
  3.     ViewBag.Title = MultiLanMVC.Resource.Register;    
  4. } < h2 > @MultiLanMVC.Resource.Register < /h2>      
  5. @using(Html.BeginForm()) {    
  6.     @Html.AntiForgeryToken() < div class = "form-horizontal" > < h4 > Registration < /h4>   < hr / > @Html.ValidationSummary(true""new {    
  7.         @class = "text-danger"    
  8.     }) < div class = "form-group" > @Html.LabelFor(model => model.FirstName, htmlAttributes: new {    
  9.         @class = "control-label col-md-2"    
  10.     }) < div class = "col-md-10" > @Html.EditorFor(model => model.FirstName, new {    
  11.         htmlAttributes = new {    
  12.             @class = "form-control"    
  13.         }    
  14.     })    
  15.     @Html.ValidationMessageFor(model => model.FirstName, ""new {    
  16.         @class = "text-danger"    
  17.     }) < /div>   < /div>   < div class = "form-group" > @Html.LabelFor(model => model.LastName, htmlAttributes: new {    
  18.         @class = "control-label col-md-2"    
  19.     }) < div class = "col-md-10" > @Html.EditorFor(model => model.LastName, new {    
  20.         htmlAttributes = new {    
  21.             @class = "form-control"    
  22.         }    
  23.     })    
  24.     @Html.ValidationMessageFor(model => model.LastName, ""new {    
  25.         @class = "text-danger"    
  26.     }) < /div>   < /div>   < div class = "form-group" > @Html.LabelFor(model => model.Email, htmlAttributes: new {    
  27.         @class = "control-label col-md-2"    
  28.     }) < div class = "col-md-10" > @Html.EditorFor(model => model.Email, new {    
  29.         htmlAttributes = new {    
  30.             @class = "form-control"    
  31.         }    
  32.     })    
  33.     @Html.ValidationMessageFor(model => model.Email, ""new {    
  34.         @class = "text-danger"    
  35.     }) < /div>   < /div>   < div class = "form-group" > @Html.LabelFor(model => model.Country, htmlAttributes: new {    
  36.         @class = "control-label col-md-2"    
  37.     }) < div class = "col-md-10" > @Html.EditorFor(model => model.Country, new {    
  38.         htmlAttributes = new {    
  39.             @class = "form-control"    
  40.         }    
  41.     })    
  42.     @Html.ValidationMessageFor(model => model.Country, ""new {    
  43.         @class = "text-danger"    
  44.     }) < /div>   < /div>   < div class = "form-group" > < div class = "col-md-offset-2 col-md-10" > < input type = "submit"    
  45.     value = "@MultiLanMVC.Resource.Register"    
  46.     class = "btn btn-default" / > < /div>   < /div>   < /div>      
  47. } < 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 in Complete Layout page. (_Layout .cshtml).
  1. <div style="padding:5px">  
  2.   
  3. @{  
  4. foreach (var i in MultiLanMVC.LanguageMang.AvailableLanguages)  
  5. {  
  6. @Html.ActionLink(i.LanguageFullName, "ChangeLanguage""Home"new { lang = i.LanguageCultureName }, null) <text> </text>  
  7. }  
  8. }  
  9. </div> 
Here, the default output is given blow-
 
out1 
 
Click English tab at the top of the page and you will see all the required validations are in Spanish language.
 
out2 
 
Click Hindi tab at the top of the page and then you will see all the required validations are in Hindi language.
 
out3 
 
Click Japanese tab at the top of the page and you will see all the required validations are in Japanese language.
 
out4 
 
Click Arabic tab at the top of the page and then you will see all the required validations are in Arabic language.
 
out5 
 
 
 
I hope you enjoyed this multi-language tutorial. Follow C# Corner to learn new things about ASP.NET MVC. Thanks for reading this article.


Recommended Ebook

Diving Into ASP.NET WebAPI

Download Now!
Similar Articles