Click here to Skip to main content
Click here to Skip to main content

Creating ASP.NET MVC Multilingual Web Application

By , 10 Dec 2010
 

Introduction

In this article, we are going to see how we can show localized content to your ASP.NET MVC web application.

Background

We will see mainly two approaches to resolve this issue:

  • Approach 1: Using Static Pages
  • Approach 2: Using Dynamic page with localized data at runtime

Using the Code

Before we start with Approach 1, I would like to request you to download the attached source code.

Approach 1: Using Static Pages

We can go for this approach only when we have few/limited static localized pages.

UI.png

Image 1:UI Looks like this

In order to demonstrate Approach 1, I have placed tree Html Anchor tags in Site.Master and in logindisplay div placed “<h2>CultureName: [<%:ViewData["CultureName"]%>]</h2>” which will show the Culture Name (e.g. US-English :en-US):

<div id="logindisplay">

       <% Html.RenderPartial("LogOnUserControl"); %> 
       <h2> CultureName: [<%: ViewData["CultureName"]%>]</h2>

     </div>      

     <div id="menucontainer">      

       <ul id="menu">       

         <li><%: Html.ActionLink("About Page Dynamic Redirection based on locale", 
             "AboutLocaleRedirect", "Home")%></li>

         <li><%: Html.ActionLink("Static About en-US", "About_en_US/About", "Home")%></li>

         <li><%: Html.ActionLink("Static About zh-CN", "About_zh_CN/About", "Home")%></li>

       </ul>      

     </div>
Code 1: Inside Site.Master

For demo purposes, we will use only two static pages as shown in Image 3: View Folder Structure.

ViewStructure.png

Image 2: View Folder Structure

As shown in Image:Inside Site.Master, we will be using three HTML anchor tags to illustrate Approach 1. Let us see each of these HTML Anchor tags.

<li><%: Html.ActionLink("About Page Dynamic Redirection based on locale", 
    "AboutLocaleRedirect", "Home")%></li>

In order to do this, we will check for CultureName as shown in Image 4: Inside Controller and on action, we are dynamically changing views.

This will check the culture code and accordingly switch to either ‘About_en-US.aspx’ or ‘About_zh-CN.aspx’.

  <li><%: Html.ActionLink("Static About en-US", "About_en_US/About", "Home")%></li>

This link will be used to fetch directly data in US English language.

  <li><%: Html.ActionLink("Static About zh-CN", "About_zh_CN/About", "Home")%></li>

This link will be used to fetch data in Chinese language.

public ActionResult AboutLocaleRedirect()
   {
     // It is advisible not to wite any logic in controller. Only for demo purpose
     // I am using here. You can put this code in model

     ViewData["CultureName"] = System.Globalization.CultureInfo.CurrentUICulture.Name;
     return View("About_" + System.Globalization.CultureInfo.CurrentUICulture.Name); 
   }    

   public ActionResult About_en_US()
   {
     ViewData["CultureName"] = "en-US";
     return View("About_en-US");
   }

   public ActionResult About_zh_CN()
   {
     ViewData["CultureName"] = "zh-CN";
     return View("About_zh-CN");
   }
Code 2: Inside Controller

Approach 2: Using Dynamic Page with Localized Data at Runtime

We should go for this approach if we have a large number of pages to show a data in localized format.

In this approach, we can either use resource file or directly data from database.

As shown in Image 2: Site.Master, we have HTML anchor tag:

<li><%:Html.ActionLink("About Page Dynamic Content Based on locale","AboutLocalization",
    "Home")%></li>

On click of this anchor tag, we are going to call action ‘AboutLocalization’ in HomeController as shown in Code 5: Inside Controller.

public ActionResult AboutLocalization()
   2{
     //It is advisable not to write any logic in controller. Only for demo purposes,
     //I am using here. You can put this code in model

     //Here either you can take localized data from resource file or from database

     ViewData["CultureName"] = System.Globalization.CultureInfo.CurrentUICulture.Name;

     ViewData["message"] = (
         System.Globalization.CultureInfo.CurrentUICulture.Name == "en-US") ?

       "Content: Welcome to MVC localization demo" : "內容:歡迎到MVC演示的位置";

     return View("About");
   }
Code 3: Inside Controller

Here we are going to check for Culture and accordingly, we going to either get data from Database or from Resource file. For demo purposes, I have used constant string.

According to locale, we are setting data to ViewData[‘Message’].

And as shown in Image 5: Inside About.aspx, we are displaying dynamic data.

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
 <h22>About</h2>
 <p>
   <h2><%: ViewData["Message"] %></h2>
 </p>
</asp:Content>
Code 4: Inside About.aspx

Thank you so much...

For any queries, please get back to me.

History

  • 10th December, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pradip_Bobhate
Web Developer
United States United States
Member
:::I m a lover of life, very curious, a good listener. when i meet people i learn, i evaluate, i marvel, i absorb, i remember and that helps me grasp and process it back at home:::
 
I am very passionated .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, JavaScript, SharePoint 2007, Office Communication Server, IIS and related technologies.I am having experience in design patterns and N-Tier Architecture.
 
I am Certified Technologies Specialist in ASP.NET and SharePoint 2007 Technologies Specialist.
 
Learning new technologies and sharing knowledge excites me most. Blogging, solving problems at various forums, helping people, keeps me busy entire day.
 
Visit My Blog @ http://pradipbobhate.com

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory,Microsoft Communication Server 2007 (MOCS), Microsoft Office SharePoint Server 2007(MOSS) , Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008
 

::: technology is astounding & has changed itself & our life -significantly.you’ll find blog-posts, screen casts and a lot more other stuff, which can assist you to kick start your development OR understand the concept behind it :::

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberMember 263493528 Jan '13 - 17:44 
GeneralMy vote of 1memberKamyar2 Oct '12 - 21:51 
GeneralMy vote of 1memberPradeepk from New Delhi21 Jun '12 - 17:42 
GeneralMy vote of 1memberPiyush K Goriya25 Apr '12 - 20:46 
GeneralMy vote of 1memberjduf8 Nov '11 - 2:37 
GeneralHow to make website support multi languagesmembersandhyabhavani18 Jun '11 - 2:18 
GeneralMy vote of 1membergautam.munish@gamil.com22 Feb '11 - 21:04 
Generalfor bigginer its goodmemberPranay Rana30 Dec '10 - 16:52 
GeneralMy vote of 1membermarisks13 Dec '10 - 18:25 
QuestionWhy?memberstevenlauwers2213 Dec '10 - 4:55 
GeneralWell Explained and Usefull articlememberpnbhoyar13 Dec '10 - 3:44 
GeneralMy vote of 3memberAndrey Mazoulnitsyn13 Dec '10 - 3:21 
QuestionHow to download the code zip filememberjohnchen8810 Dec '10 - 7:57 
AnswerRe: How to download the code zip filememberPradip_Bobhate21 Dec '10 - 19:59 
GeneralRe: How to download the code zip filemembergautam.munish@gamil.com22 Feb '11 - 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 10 Dec 2010
Article Copyright 2010 by Pradip_Bobhate
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid