How to Create the Multilanguage Application in ASP.Net

Look at the following image:

this is your home page

change language

We can create the Multilanguage application in ASP.NET like the following.

Step 1

Add the Master Page Site1.Master and design the menu and language selection dropdown list like this:

  1. <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="MultilanguageSample.Site1" %>  
  2.    
  3. <!DOCTYPE html>  
  4.    
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         html {  
  10.             font-family: Tahoma;  
  11.             font-size: 14px;  
  12.             font-style: normal;  
  13.             background-color: Silver;  
  14.         }  
  15.    
  16.         .Content {  
  17.             margin: auto;  
  18.             width: 700px;  
  19.             background-color: white;  
  20.             border: Solid 1px black;  
  21.         }  
  22.         .auto-style1 {  
  23.             width: 100%;  
  24.         }  
  25.     </style>  
  26.     <asp:ContentPlaceHolder ID="head" runat="server">  
  27.     </asp:ContentPlaceHolder>  
  28. </head>  
  29.    
  30. <body>  
  31.     <form id="form1" runat="server">  
  32.         <div class="Content">  
  33.         <br />  
  34.             <table class="auto-style1">  
  35.                 <tr>  
  36.                     <td style="background-color: #339966"> <a href="Default.aspx">  
  37.                 <asp:Label ID="Label1" meta:resourcekey="menuItemDefault" runat="server" Text="Home"></asp:Label>  
  38.             </a></td>  
  39.                     <td style="background-color: #339966; " ><a href="Contact.aspx">  
  40.             <asp:Label ID="Label2" meta:resourcekey="menuItemContact" runat="server" Text="ContactUs"></asp:Label>  
  41.         </a></td>  
  42.                     <td style="background-color: #339966;"><asp:DropDownList ID="DropDownList_Language" runat="server" Height="20px" Width="170px"  
  43.             OnSelectedIndexChanged="DropDownList_Language_SelectedIndexChanged" AutoPostBack="true">  
  44.             <asp:ListItem Value="en-US">English</asp:ListItem>  
  45.             <asp:ListItem Value="sv-SE">Swedish</asp:ListItem>  
  46.         </asp:DropDownList></td>  
  47.                 </tr>  
  48.             </table>  
  49.         <br />  
  50.         <br />  
  51.    
  52.         <div>  
  53.             <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">  
  54.             </asp:ContentPlaceHolder>  
  55.         </div>  
  56.         </div>  
  57.     </form>  
  58. </body>  
  59. </html>  
Step 2

Write the code in the code behind file like this:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.    
  8. namespace MultilanguageSample  
  9. {  
  10.     public partial class Site1 : System.Web.UI.MasterPage  
  11.     {  
  12.          
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             if (Session["language"] != null && !IsPostBack)  
  16.             {  
  17.                 DropDownList_Language.ClearSelection();  
  18.                 DropDownList_Language.Items.FindByValue(Session["language"].ToString()).Selected = true;  
  19.             }  
  20.         }  
  21.         protected void DropDownList_Language_SelectedIndexChanged(object sender, EventArgs e)  
  22.         {  
  23.             switch (DropDownList_Language.SelectedValue)  
  24.             {  
  25.                 case "en-US"this.SetMyNewCulture("en-US");  
  26.                     break;  
  27.                 case "sv-SE"this.SetMyNewCulture("sv-SE");  
  28.                     break;  
  29.                 default:  
  30.                     break;  
  31.             }  
  32.             Response.Redirect(Request.Path);  
  33.         }  
  34.    
  35.         private void SetMyNewCulture(string culture)  
  36.         {  
  37.             Session["language"] = culture;  
  38.         }  
  39.     }  
  40. }  
Note: Here you can also store the user language selection in cookies or Local Storage of HTML 5 using JavaScript. This can be used to maintain the user preference language at page Load.

Step 3

Create a “App_LocalResources” folder and add the resources as page wise.

Like Site.master.resx for English and Site1.Master.sv. resx for Swedish.

Note: Keep the separate resource file for each page for better maintainability.

master page

Step 4

Add this method in the global.asax file:
  1. void Application_AcquireRequestState(object sender, EventArgs e)  
  2. {  
  3.       HttpContext context = HttpContext.Current;  
  4.       if (context.Session["language"] != null)  
  5.       {  
  6.             Thread.CurrentThread.CurrentUICulture = new CultureInfo(context.Session["language"].ToString().Trim());  
  7.             Thread.CurrentThread.CurrentCulture = new CultureInfo(context.Session["language"].ToString().Trim());  
  8.       }  
  9. }  
This is used for getting the current state of the session.

Step 5

Call the resource key on the label like this as page wise.
  1. <asp:Label ID="Label1" meta:resourcekey="menuItemDefault" runat="server" Text="Home"></asp:Label>   
You call also call the resource file in C# like this:
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3. lblMsg.Text = GetLocalResourceObject("lblMsgHome.Text").ToString();  
  4.   
  5. }  
Note: Please let me know if you have any good approach to do this. 

Up Next
    Ebook Download
    View all
    Learn
    View all