QA InfoTech » Dot Net » Multilingual MVC Application

Multilingual MVC Application

Multilingual MVC Application

Introduction

In this post I will explain how to create multilingual application in MVC. This sample is developed to provide the solution to Multilanguage support for MVC project.This application currently support two languages such as es-Es and en-En

Description

In this application I have made use of resource files to store the strings from different languages. This way of implementing the Multilanguage application is called implicit localization. In implicit localization, you specify that control properties should automatically be read from a resource file, but you do not need to explicitly specify which properties are localized.

Step – 1 : Create New Project.

Go to File > New > Project > Select asp.net web application > Entre Application Name > Click OK.

Choose MVC -> Click OK.

Step -2 : Add App_GlobalResources Folder.

Right click on the Project -> Add->Add Asp.net folder -> App_GlobalResources

Step -3 . Add Resources file for save different language under “App_GlobalResources” Folder

Right Click on “App_GlobalResources” Folder form Solution Explorer > Add > New Item > Select “Resources File” under Visual C# > Enter Name (Like “Lang.es-Es.resx”) > Add.
[N:B: Please take care of the file Name.]

Now add key and value in each resource file. For ex. Add two files for Home_Index.resx and Home_index.ex.resx

  1. Home_Index.resx
  2. Home_Index.es.resx

Step-4 : Write code in Global.ascx.cs

protectedvoid Application_BeginRequest(Object source, EventArgs e)

{

var userLanguages = Request.UserLanguages;

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(userLanguages[0] .ToString());

System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;

}

Step – 5 : Use resources Keys in Views.

Replace your text in views with Resources keys. For. ex.

@{

ViewBag.Title = @Resources.Home_Index.title;

}

<divclass=”jumbotron”>

<h1>ASP.NET</h1>

<pclass=”lead”>@Resources.Home_Index.lead</p>

<p><ahref=”#”class=”btn btn-primary btn-lg”>@Resources.Home_Index.more &raquo;</a></p>

</div>

<divclass=”row”>

<divclass=”col-md-4″>

<h2>@Resources.Home_Index.con1_h2</h2>

<p>@Resources.Home_Index.con1_p</p>

<p><aclass=”btn btn-default”href=”#”>@Resources.Home_Index.more &raquo;</a></p>

</div>

<divclass=”col-md-4″>

<h2>@Resources.Home_Index.con2_h2</h2>

<p>@Resources.Home_Index.con2_p</p>

<p><aclass=”btn btn-default”href=”#”>@Resources.Home_Index.more &raquo;</a></p>

</div>

<divclass=”col-md-4″>

<h2>@Resources.Home_Index.con3_h2</h2>

<p>@Resources.Home_Index.con3_p</p>

<p><aclass=”btn btn-default”href=”#”>@Resources.Home_Index.more &raquo;</a></p>

</div>

</div>

Step- 6. Build and run your application.

 

About the Author